简体   繁体   English

循环/列表理解的python基本

[英]python basic for loop/list comprehension

Say I have: 说我有:

list=[('mike', 22.0), ('john', 33.0)]

I want to print a list of just the numbers, smallest to largest. 我只想打印一个数字列表,从最小到最大。 I tried doing this: 我尝试这样做:

for s in list:
    print(sorted(s[1]))

But it said 'int object is not iterable'? 但是它说“ int对象是不可迭代的”?

If i do this, it works ( output is: [22,33]) 如果我这样做,它将起作用(输出为:[22,33])

print (sorted(s[1] for s in list))

How does the 2nd one work and what's the difference between the 1st and 2nd attempt? 第二次尝试如何工作,第一次尝试与第二次尝试之间有什么区别? Hard time understanding the list comprehension technique. 很难理解列表理解技术。

BTW, the final goal is to print the NAME of the person will the lowest mark. 顺便说一句,最终的目标是将人的名字印在最低的标记上。 But I'm trying to understand this part first before I associate the name with the mark. 但是在将名称与商标相关联之前,我先尝试理解此部分。 Dict would make it so much easier to assign key-values though 使用Dict可以更轻松地分配键值

When you do: 当您这样做时:

for s in list:
    print(sorted(s[1]))

You're trying to sort an int . 您正在尝试对int进行排序。 The sorted function requires an iterable (for example, a list). sorted函数需要iterable (例如,列表)。 Hence your error. 因此,您的错误。 This is noticeable since: 这很明显,因为:

for s in list:
    print(s[1])

prints: 打印:

22.0
33.0

On the other hand, when you do: Sorted(s[1] for s in myList) , you're sorting a list (actually you're sorting a generator, but there's a somewhat fine line). 另一方面,当您执行以下操作时: Sorted(s[1] for s in myList) ,您正在对列表进行排序(实际上,您正在对生成器进行排序,但是有些细线)。 Try: 尝试:

newList = [s[1] for s in myList]
newList

and you'll see that you're essentially sorting a list. 并且您会看到您实际上是在对列表进行排序。 Sorting a list makes sense, but sorting a single number doesn't. 对列表进行排序很有意义,但是对单个数字进行排序则没有意义。 Sorted is a function that returns a sorted list. Sorted是一个返回排序列表的函数。 To illustrate the difference between generators and iterables (like lists, tuples and dicts), compare the last snippet to the below: 为了说明生成器和可迭代器(如列表,元组和字典)之间的区别,请将最后一个代码段与以下内容进行比较:

newGenerator = (s[1] for s in myList)
print(newGenerator) #probably won't give what you ever need
for item in newGenerator:
    print item  #this will iterate through the items in the generator and print them

I had assumed you wanted the group sorted, but someone else answered that already and got downvoted so I didn't add it. 我以为您希望对小组进行排序,但是其他人已经回答了这个问题,因此被否决了,所以我没有添加它。 Here is how you would do that: 这是您要执行的操作:

myList.sort(key = lambda x: x[1])

Then: 然后:

myList[0][0]

Will be the name of the lowest person. 将是最低人的名字。

In this case, sort is a method, which operates on myList and changes it. 在这种情况下, sort是一种对myList进行操作并对其进行更改的方法。 If you pass it the key = lambda x: x[1] argument, it will sort the list based on the position 1 . 如果将key = lambda x: x[1]参数传递给它,它将根据位置1对列表进行排序。 So, if your number was in a different position in your tuples you would use a different number there. 因此,如果您的数字在元组中的位置不同,则可以在其中使用其他数字。 lambda is just a way to make a function on the fly, in this case the function just grabs the 1 element (second element). lambda只是动态生成函数的一种方法,在这种情况下,该函数仅捕获1元素(第二个元素)。

Because a list comprehension will give a generator here, see: 因为列表理解将在此处提供生成器 ,所以请参见:

>>> Mylist=[('john', 33.0),('mike', 22.0)]
>>> print(s[1] for s in Mylist)
<generator object <genexpr> at 0x7fd4a005e0f8>

And however, sorted() can be use on an iterable like list or generator , and then convert it to a list. 但是, sorted()可以用于可迭代的 列表generator ,然后将其转换为列表。

For example, range() will give a generator on Python 3(if you're using Python 2, use xrange() instead): 例如, range()将为Python 3提供一个生成器 (如果您使用的是Python 2,请改用xrange() ):

>>> a = range(20)
>>> sorted(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

And about why does this list comprehension give a generator , I think you forgot [] : 关于这个列表理解为什么会产生一个生成器的原因 ,我认为您忘记了[]

>>> Mylist=[('john', 33.0),('mike', 22.0)]
>>> print(sorted([s[1] for s in Mylist]))
[22.0, 33.0]
>>> print(sorted(s[1] for s in Mylist))
[22.0, 33.0]
>>> print([s[1] for s in Mylist])
[33.0, 22.0]
>>> print(s[1] for s in Mylist)
<generator object <genexpr> at 0x7f8ab881c6d0>
>>> 

By the way, about your code without list comprehension: 顺便说一下,关于没有列表理解的代码:

>>> Mylist=[('john', 33.0),('mike', 22.0)]
>>> l = []

>>> for s in sorted(Mylist, key=lambda x:x[1]):
...     l.append(s[1])

>>> print(l)
[22.0, 33.0]

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

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