简体   繁体   English

为什么我的清单不遍历每个清单?

[英]Why won't my list iterate over each one?

Currently working through tutorial "Python the hard way". 当前正在通过教程“ Python的艰难方法”进行工作。

I'm learning about lists and loops (ex32). 我正在学习列表和循环(ex32)。

At the end of the exercise Zed (Tutorial author) tells us to play around, which I have done. 练习结束时,Zed(教程作者)告诉我们,我已经完成了游戏。

# we can also build lists, first start with an empty one
elements = []
elements.append(range(0,6))

# then use the range function to do 0 to 5 counts
for element in elements:
    print "Adding %s to elements" % element


# now we can print them out too
for element in elements:
    print"Element was: %s" % element

This produces output like so: 这样产生的输出如下:

Adding [0, 1, 2, 3, 4, 5] to elements
Element was: [0, 1, 2, 3, 4, 5]

I had expected to see something like this: 我曾期望看到这样的事情:

Adding 0 to elements
Adding 1 to elements
Adding 2 to elements
Adding 3 to elements
Adding 4 to elements
Adding 5 to elements
Element was: 0
Element was: 1
Element was: 2
Element was: 3
Element was: 4
Element was: 5

But instead Python wants to print out my script in a oner, rather than the concatenated string with each list component. 但是相反,Python希望一次打印出我的脚本,而不是将每个列表组件的连接字符串打印出来。

I know that I could change the script to reflect the authors script exactly 我知道我可以更改脚本以准确反映作者的脚本

# we can also build lists, first start with an empty one
elements = []

# then use the range function to do 0 to 5 counts
for i in range(0, 6):
    print "Adding %d to the list." % i
    # append is a function that lists understand
    elements.append(i)

# now we can print them out too
for i in elements:
    print "Element was: %d" % i

but I'd just like to know why my piece does not work as expected? 但我只想知道为什么我的作品无法按预期工作?

you are appending a list to a list! 您正在将列表追加到列表! you just want to create the list! 您只想创建列表!

all you need to do is change the following: 您所需要做的就是更改以下内容:

elements = []
elements.append(range(0,6))

into 进入

elements = range(0,6)

and you will get your expected results 您将获得预期的结果

why 为什么

when you created elements for the first time, it was a blank list. 首次创建elements时,它是一个空白列表。 Then you appended range(0,6) to your empty list. 然后将range(0,6)附加到空列表。 Now elements looks like [[0,1,2,3,4,5]] (or [range(0,6)] ), which is a list with one element, a list. 现在元素看起来像[[0,1,2,3,4,5]] (或[range(0,6)] ),它是一个包含一个元素的列表,一个列表。

This is because elements contains exactly one element , which is a list : [0, 1, 2, 3, 4, 5] . 这是因为elements包含一个element ,即一个list[0, 1, 2, 3, 4, 5] list.append() adds an item to the end of the list. list.append()将一个项目添加到列表的末尾。

In [1]: elements = []

In [2]: elements.append(range(0,6))

In [3]: elements
Out[3]: [[0, 1, 2, 3, 4, 5]]

Perhaps you meant to extend the list: 也许您打算扩展此列表:

In [1]: elements = []

In [2]: elements.extend(range(0, 6))

In [3]: elements
Out[3]: [0, 1, 2, 3, 4, 5]

Or replace it? 还是更换它?

In [4]: elements = range(0,6)

In [5]: elements
Out[5]: [0, 1, 2, 3, 4, 5]

Or even: 甚至:

In [6]: elements = [element for element in range(0,6)]

In [7]: elements
Out[7]: [0, 1, 2, 3, 4, 5]

The list comprehension is unnecessary in this example, but it demonstrates how it is easy to filter or map those elements. 在此示例中, 列表理解是不必要的,但它演示了如何轻松过滤或映射这些元素。

.append adds a single element to the list. .append将单个元素添加到列表中。 That single element is range(0, 6) , which is [0, 1, 2, 3, 4, 5] (Johnsyweb got it in before me). 那个元素是range(0, 6) 0,6 range(0, 6) ,它是[0, 1, 2, 3, 4, 5] (Johnsyweb在我之前得到了它)。 You could use .extend to append each one . 您可以使用.extend追加每个

elements = []


elements.append(range(0,6)) 
# appends range(0,6) to elements. range(0,6) creates a list in Python 2.x but only in Python 2.x. thanks to adsmith for pointing this out.

print elements

[[0, 1, 2, 3, 4, 5]] # it's clear that elements has 1 element. A list.

that's why 这就是为什么

for i in elements:
    print "adding %s to elements" % i

produces: 产生:

adding [0,1,2,3,4,5] to elements

To get the output you want: 要获得所需的输出:

elements = range(0,6)

OR 要么

elements = [i for i in range(0,6)] # list comprehension

then 然后

for i in elements:
    print "adding %s to elements" % i

outputs what you want 输出你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM