Difference between a list comprehension and a for loop

前端 未结 5 1939
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-23 08:57
n=input()
y=n.split()
a=([int(x) for x in y])
print(a)

output

python .\\input_into_list.py
1 2 3
[1, 2, 3]

The above

5条回答
  •  有刺的猬
    2021-01-23 09:33

    in the first script, you are adding all the values to a in a single line.

    in the second script, you are reassigning a on each loop.

    try to append to a instead.

    n=input()
    y=n.split()
    a = []
    for x in y:
        a.append(int(x))
    print(a)
    

提交回复
热议问题