简体   繁体   中英

Assign the result of a loop to a variable in Python

Consider a list I want to parse using a for :

friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
for i in friends:
   print i

will return :

"Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"

However, if I want to put it to a (str) variable, like :

friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
for i in friends:
    var=i

first I have to declare another var variable, which is silly but whatever. then,

print var

will return the last element of the list, which is "Paris", because the variable is overwritten for each iteration right.

So my question is : how can I assign the output of my loop "i", for each iteration, to a variable in Python ?

Sorry for the sillyness of this question but this is a concept I can't seem to figure out clearly.

If I understand well, you'd like to dynamically create variables. Here it is.

from collections import OrderedDict

friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
d = OrderedDict()
for idx, value in enumerate(friends):
    key = 'var' + str(idx)
    d[key] = value 

print(d)
# Output
OrderedDict([('var0', 'Joe'), ('var1', 'Zoe'), ('var2', 'Brad'), ('var3', 'Angelina'), ('var4', 'Zuki'), ('var5', 'Thandi'), ('var6', 'Paris')])

I also have this question, this is how I managed to solve it somewhat:

    friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]

    new_friends = ' '.join([x for x in friends])

    print(new_friends)

Will return:

    Joe Zoe Brad Angelina Zuki Thandi Paris
var = ''
friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
for i in friends:
    var=i 

if list and loop are in function then declare var as global

global var

in starting of function

If you want to join the values in friends into a comma-separated string, that would be

s = ','.join(friends)

If you want to include quotes around the names, maybe something like

s = ','.join(['"{0}"'.format(x) for x in friends])

Try this at the end of the loop:

the_variable = the_variable + i

However, if you are to do this, you should add a space to the end of every item in the dictionary, otherwise it will output:

JoeZoeBradAngelinaZukiThandiParis

I would use a dictionary instead, as I too spent a while looking into this, and determined that a dictionary would be easy enough.

    friends = ["Joe", "Zoe", "Brad", "Angelina", "Zuki", "Thandi", "Paris"]
    dict= {}
    for i in friends:
       dict[i] = i

    print(dict)
    print(dict['Zuki'])
    dict['Zuki'] = "Tim Smith"

    print(dict['Zuki'])

The Other option would be to just call the number:

    print(friends[0])

As for automatic assignment I haven't found a way to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM