理解Python列表推导的问题

时间:2023-02-12 09:56:42

What does the last line mean in the following code?

最后一行在以下代码中的含义是什么?

import pickle, urllib                                                                                                                                                     

  handle = urllib.urlopen("http://www.pythonchallenge.com/pc/def/banner.p")
  data = pickle.load(handle)
  handle.close()

  for elt in data:
         print "".join([e[1] * e[0] for e in elt])

My attempt to the problem:

我尝试解决这个问题:

  • "".join... uses join -method to empty text
  • “”。join ...使用join -method来清空文本

  • e[1] * e[0] multiplies two subsequent values in the sequence, e
  • e [1] * e [0]乘以序列中的两个后续值,例如e

  • I am not sure what is e
  • 我不确定是什么意思

  • I am not sure, what it means, when you have something before for -loop, like: e[1] * e[0] for e in elt
  • 我不确定,这意味着什么,当你有什么事情之前为-loop,如:e [1] * e [0] for e in elt

6 个解决方案

#1


Maybe best explained with an example:

也许最好用一个例子来解释:

print "".join([e[1] * e[0] for e in elt])

is the short form of

是简短的形式

x = []
for e in elt:
  x.append(e[1] * e[0])
print "".join(x)

List comprehensions are simply syntactic sugar for for loops, which make an expression out of a sequence of statements.

列表推导只是for循环的语法糖,它从一系列语句中创建表达式。

elt can be an arbitrary object, since you load it from pickles, and e likewise. The usage suggests that is it a sequence type, but it could just be anything that implements the sequence protocol.

elt可以是一个任意对象,因为你从pickles加载它,e同样如此。用法表明它是一种序列类型,但它可能只是实现序列协议的任何东西。

#2


Firstly, you need to put http:// in front of the URL, ie: handle = urllib.urlopen("http://www.pythonchallenge.com/pc/def/banner.p")

首先,您需要将http://放在URL前面,即:handle = urllib.urlopen(“http://www.pythonchallenge.com/pc/def/banner.p”)

An expression [e for e in aList] is a list comprehension which generates a list of values.

表达式[e表示e中的e]是列表推导,其生成值列表。

With Python strings, the * operator is used to repeat a string. Try typing in the commands one by one into an interpreter then look at data:

使用Python字符串,*运算符用于重复字符串。尝试将命令逐个输入解释器,然后查看数据:

>>> data[0]
[(' ', 95)]

This shows us each line of data is a tuple containing two elements.

这表明我们每行数据都是一个包含两个元素的元组。

Thus the expression e1 * e[0] is effectively the string in e[0] repeated e1 times.

因此,表达式e1 * e [0]实际上是e [0]重复e1次的字符串。

Hence the program draws a banner.

因此,该计划绘制了一面旗帜。

#3


[e[1] * e[0] for e in elt] is a list comprehension, which evaluates to a list itself by looping through another list, in this case elt. Each element in the new list is e[1]*e[0], where e is the corresponding element in elt.

[e]中的[e [1] * e [0]]是列表推导,它通过循环遍历另一个列表来评估列表本身,在本例中为elt。新列表中的每个元素都是e [1] * e [0],其中e是elt中的对应元素。

#4


The question itself has already been fully answered but I'd like to add that a list comprehension also supports filtering. Your original line

问题本身已经得到了充分的回答,但我想补充一点,列表理解也支持过滤。你原来的路线

print "".join([e[1] * e[0] for e in elt])

could, as an example, become

例如,可以成为

print "".join([e[1] * e[0] for e in elt if len(e) == 2])

to only operate on items in elt that have two elements.

仅对具有两个元素的elt中的项目进行操作。

#5


join() is a string method, that works on a separator in new string

join()是一个字符串方法,适用于新字符串中的分隔符

>>> ':'.join(['ab', 'cd'])
>>> 'ab:cd'

and list comprehension is not necessary there, generator would suffice

并且列表理解在那里是不必要的,生成器就足够了

#6


Andy's is a great answer!

安迪是一个很好的答案!

If you want to see every step of the loop (with line-breaks) try this out:

如果你想看到循环的每一步(使用换行符)试试这个:

for elt in data:
    for e in elt:
        print "e[0] == %s, e[1] == %d, which gives us:  '%s'" % (e[0], e[1], ''.join(e[1] * e[0]))

#1


Maybe best explained with an example:

也许最好用一个例子来解释:

print "".join([e[1] * e[0] for e in elt])

is the short form of

是简短的形式

x = []
for e in elt:
  x.append(e[1] * e[0])
print "".join(x)

List comprehensions are simply syntactic sugar for for loops, which make an expression out of a sequence of statements.

列表推导只是for循环的语法糖,它从一系列语句中创建表达式。

elt can be an arbitrary object, since you load it from pickles, and e likewise. The usage suggests that is it a sequence type, but it could just be anything that implements the sequence protocol.

elt可以是一个任意对象,因为你从pickles加载它,e同样如此。用法表明它是一种序列类型,但它可能只是实现序列协议的任何东西。

#2


Firstly, you need to put http:// in front of the URL, ie: handle = urllib.urlopen("http://www.pythonchallenge.com/pc/def/banner.p")

首先,您需要将http://放在URL前面,即:handle = urllib.urlopen(“http://www.pythonchallenge.com/pc/def/banner.p”)

An expression [e for e in aList] is a list comprehension which generates a list of values.

表达式[e表示e中的e]是列表推导,其生成值列表。

With Python strings, the * operator is used to repeat a string. Try typing in the commands one by one into an interpreter then look at data:

使用Python字符串,*运算符用于重复字符串。尝试将命令逐个输入解释器,然后查看数据:

>>> data[0]
[(' ', 95)]

This shows us each line of data is a tuple containing two elements.

这表明我们每行数据都是一个包含两个元素的元组。

Thus the expression e1 * e[0] is effectively the string in e[0] repeated e1 times.

因此,表达式e1 * e [0]实际上是e [0]重复e1次的字符串。

Hence the program draws a banner.

因此,该计划绘制了一面旗帜。

#3


[e[1] * e[0] for e in elt] is a list comprehension, which evaluates to a list itself by looping through another list, in this case elt. Each element in the new list is e[1]*e[0], where e is the corresponding element in elt.

[e]中的[e [1] * e [0]]是列表推导,它通过循环遍历另一个列表来评估列表本身,在本例中为elt。新列表中的每个元素都是e [1] * e [0],其中e是elt中的对应元素。

#4


The question itself has already been fully answered but I'd like to add that a list comprehension also supports filtering. Your original line

问题本身已经得到了充分的回答,但我想补充一点,列表理解也支持过滤。你原来的路线

print "".join([e[1] * e[0] for e in elt])

could, as an example, become

例如,可以成为

print "".join([e[1] * e[0] for e in elt if len(e) == 2])

to only operate on items in elt that have two elements.

仅对具有两个元素的elt中的项目进行操作。

#5


join() is a string method, that works on a separator in new string

join()是一个字符串方法,适用于新字符串中的分隔符

>>> ':'.join(['ab', 'cd'])
>>> 'ab:cd'

and list comprehension is not necessary there, generator would suffice

并且列表理解在那里是不必要的,生成器就足够了

#6


Andy's is a great answer!

安迪是一个很好的答案!

If you want to see every step of the loop (with line-breaks) try this out:

如果你想看到循环的每一步(使用换行符)试试这个:

for elt in data:
    for e in elt:
        print "e[0] == %s, e[1] == %d, which gives us:  '%s'" % (e[0], e[1], ''.join(e[1] * e[0]))