对于下面的代码,将列表理解和类型转换相结合,我有点困惑。了解表达式的语法>>> 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))
所以: