2017-08-17 60 views
1

对于下面的代码,将列表理解和类型转换相结合,我有点困惑。了解表达式的语法>>> new = [int(i)for my old.split('')]

# Statement 1 
list1 = input('enter 10 integers separated by spaces: ') 
# type(list1) --> str 

# Statement 2 
list2 = [int(i) for i in list1.split(' ')] 
# type(list2) --> lst 

如果我正在试图做到上述,我会用两个步骤。 (1)list1.split() (2)遍历字符串列表将它们转换为整数

有人可以更详细地解释Python 3.6如何解释语句二吗? Python如何知道首先拆分字符串,然后使这些碎片整数?举一个例子,你为什么不把它写成这样?

# Statement 2 
list2 = [i for int(i) in list1.split(' ')] 

谢谢!你的情况

result = [] 
for x in sequence: # for each element: take it, name it x and do the following 
    result.append(f(x)) 

所以:

回答

3

result = [f(x) for x in sequence]可以被理解为以下的缩写形式(严格地说,这些形式可以表现不同如果f(x)一些调用将引发异常。)评估值list1.split(' ')sequence),然后处理每个元素。

[i for int(i) ...]是不可能的,因为int(i)不能是一个名字。

0

在Python中,for循环的语法是这样的:

for VAR in ITERABLE: 
    # Do stuff with VAR. 

注意,VAR是一个变量名。这是不允许的表达 像int(VAR)

列表解析语法如下所示类似的规则:

xs = [EXPRESSION for VAR in INTERABLE] 

同样,VAR允许是变量名,而不是一个完整的表达。 终极列表(xs)将推出为ITERABLE中的每一个项目而推出的表达式为 。

目前尚不清楚您的替代声明2对于 非常有用。试想一下:

# Alternative Statement 2. 
list2 = [i for int(i) in list1.split(' ')] 

# More generically. 
list2 = [EXPRESSION2 for EXPRESSION1 in ITERABLE] 

在这种情况下,程序员怎么会是指如果 她想使用该值作为expression2的部分表达式的值?你需要一个 值的名字,所以你可以用它做东西。

0

其实,在你的代码清单理解语法不正确:

>>> list2 = [i for int(i) in list1.split(' ')] 
SyntaxError: can't assign to function call 
>>> 

正确的形式是[int(i) for i in list1.split(' ')]。在整个答案的其余部分我会假设你的意思是后者。

有人可以更详细地解释Python 3.6如何解释语句二吗?

列表理解:

list2 = [int(i) for i in list1.split(' ')] 

就等于(在功能方面)for循环:

list2 = [] 
for i in list1.split(' '): 
    list2.append(int(i)) 

的Python如何知道第一分割字符串分开,那么使这些碎片整数?举一个例子,你为什么不把它写成这样?

Python知道如何执行列表理解,因为它知道理解的每个部分,并且能够以正确的顺序执行每个部分。有助于清除一些混淆的是查看由列表理解产生的字节码。查看字节码必需让我们看到每一步Python解释器需要在执行您的代码:

1   0 LOAD_CONST    0 (<code object <listcomp> at 0x7f1ebce63390, file "<dis>", line 1>) 
       2 LOAD_CONST    1 ('<listcomp>') 
       4 MAKE_FUNCTION   0 
       6 LOAD_NAME    0 (list1) 
       8 LOAD_ATTR    1 (split) 
      10 LOAD_CONST    2 (' ') 
      12 CALL_FUNCTION   1 
      14 GET_ITER 
      16 CALL_FUNCTION   1 
      18 STORE_NAME    2 (list2) 
      20 LOAD_CONST    3 (None) 
      22 RETURN_VALUE 
None 
0

这就是所谓的list comprehension一个极好的功能,可用于编写行为像immutables代码。 (它是如此方便,我几乎倾向于过度使用它。)

>>> [s for s in 'abcd'] 
['a', 'b', 'c', 'd'] 
>>> [s.strip() for s in ('a ', ' b ', ' c\n')] 
['a', 'b', 'c'] 
>>> [s+' under' for s in 'something over here'.split()] 
['something under', 'over under', 'here under'] 

在你的情况有一个类型转换正在进行,与此类似:

>>> [float(s) for s in '1 2 4.5'.split()] 
[1.0, 2.0, 4.5] 

您还可以使用此通过走可迭代的,比如范围:

>>> [random.random()*i/2 for i in range(3)] 
[0.0, 0.21324127757409878, 0.9616595926178124] 

列表理解也支持if语句(东西我还没有看到任何其他语言,但我不知道太多),这使得它更加强大:

>>> [n+' here' for n in 'joe bob elsie jane'.split() if 'o' not in n] 
['elsie here', 'jane here'] 

以上是更好的性能,但在其他方面等价的:

>>> s = 'joe bob elsie jane' 
>>> l = s.split() 
>>> r = [] 
>>> for n in l: 
...  if 'o' not in n: 
...    r.append(n+' here') 
... 
>>> r 
['elsie here', 'jane here'] 

...这有点让你知道他们是什么。以同样的方式你可以用for环太:

>>> ['o'*x+'h' for x in range(1,5) for y in range(2)] 
['oh', 'oh', 'ooh', 'ooh', 'oooh', 'oooh', 'ooooh', 'ooooh'] 

然后你当然可以嵌套列表理解的,所以你可以在一行中写你的整个程序。疯狂,但很有趣。

>>> [print(s) for s in ['X'*(10-j)+'-'*j for j in [random.randint(2,7) for _ in range(6)]]] 
XXXXXXX--- 
XXXXXXX--- 
XXXXXXXX-- 
XXXXXXX--- 
XXX------- 
XXXXXXXX-- 
相关问题