Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitae97908

Browse files
committed
关于生成器的创建问题
1 parent393f22b commitae97908

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

‎Readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,20 @@ http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format
320320

321321
这是中文版:http://taizilongxu.gitbooks.io/stackoverflow-about-python/content/1/README.html
322322

323+
这里有个关于生成器的创建问题面试官有考:
324+
问: 将列表生成式中[]改成() 之后数据结构是否改变?
325+
答案:是,从列表变为生成器
326+
327+
```python
328+
>>> L= [x*xfor xinrange(10)]
329+
>>> L
330+
[0,1,4,9,16,25,36,49,64,81]
331+
>>> g= (x*xfor xinrange(10))
332+
>>> g
333+
<generatorobject<genexpr> at0x0000028F8B774200>
334+
```
335+
通过列表生成式,可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含百万元素的列表,不仅是占用很大的内存空间,如:我们只需要访问前面的几个元素,后面大部分元素所占的空间都是浪费的。因此,没有必要创建完整的列表(节省大量内存空间)。在Python中,我们可以采用生成器:边循环,边计算的机制—>generator
336+
323337
##10`*args` and`**kwargs`
324338

325339
`*args``**kwargs`只是为了方便并没有强制使用它们.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp