Difference between a list comprehension and a for loop

前端 未结 5 1930
佛祖请我去吃肉
佛祖请我去吃肉 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)
    
    0 讨论(0)
  • 2021-01-23 09:33

    In the first code block, you create the list and after that you name it as a.
    In the second code block, you change the value of a for every iteration through loop. For the second code to work, you have to do this :

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

    EDIT

    n=input()
    a=[]
    
    y=n.split()
       for x in y:
          a.append(int(x)) # Remove brackets [] that means that you append a list in a list
    
    for i in a:
       print(i) # or print(i,end='') if you want to print it in one line
    
    0 讨论(0)
  • 2021-01-23 09:34

    This is indentation error:

    n=input()
    y=n.split()
    for x in y:
        a=([int(x)])
        print(a,end=' ')
    
    0 讨论(0)
  • 2021-01-23 09:46

    The first example is what you call a list comprehension. It creates a list based on your values in a sequence and in this case your input y. List comprehensions are encouraged because of its simplicity and explicit purpose which returns you a list.

    a = [int(x) for x in y]
    

    Your second example is a for loop, loops, unlike the list comprehension, can be used for iterating through your sequence and perform multiple complex tasks. However, in your example variable a constantly gets updated with the latest value in y.

    for i in y:
        print(i)
        a = [i]
    

    To achieve the same result as a list comprehension:

    a = []
    for i in y:
        print(i)
        a.append(i)
    

    To keep it short, if you are planning to return a list, do it with a list comprehension when possible. For Python as for any language, there is no hard-and-fast rule, so use your discretion with keeping efficiency and readability in mind.

    0 讨论(0)
  • 2021-01-23 09:54

    The main difference is in appearance and runtime speed. List comprehension is shorter, easier to understand and faster in execution time.

    In your code if you want to make it same as list comprehension you need to append each element to the list:

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

    this will give you the same result as list comprehension one. In addition n.split() method itself returning the input as a list of elements. So:

    n=input()
    y=n.split()
    print(y)
    

    this is the same as above one. Just a last comment, when you use list comprehension or another list assigning you don't need to wrap the element to the brackets like: ()

    n=input()
    y=n.split()
    a=[int(x) for x in y]
    print(a)
    
    0 讨论(0)
提交回复
热议问题