+ 2
Can i affect all items of a list into multiples variables ?
I have a list like [1,2,3,4] and i Want to have all values into variables like a=1 b=2 c=3 d=4 But the lenght of my list can change
32 Antworten
+ 6
Dorian here is your answer.this code create a multiple variable by separating the each value of list without knowing the length of the list.
ll = ['xx','yy','zz','pp','jj']
for n, val in enumerate(ll):
globals()["var%d"%n] = val
print(var4) #output=jj
print(var3) #output=pp
print(var) #output=xx
#etc.....
But this is not a good practice if you are creating multiple variables in these manners.let suppose if you have 1000 variables what will you do with it.
+ 5
Dorian
My be function ord(char) help you?
+ 5
Dorian
Ord is ASCII code of char
+ 5
Maninder $ingh I didn't know you could do it without exec(), good one!
+ 4
Dorian
Try so:
s=[1,5,8,9,11]
for i in range(len(s)):
print(str(s[i])+" - "+chr(ord("a")+i))
+ 4
Dorian
You can connect my answer and from HonFu. Create a new dictionary
+ 4
Aymane Boukrouh with the help of globals function.
+ 3
Yes HonFu but i don't know how in avance the lenght of the list or dictionnary
+ 3
rodwynnejones my code can very easily be tweaked to as many variables as you want, with less than 5 lines of code
+ 3
Dorian, what sort of operation do you need to perform on the user-provided values?
Depending on what you want to do, there might be different good options.
Also, why do you need the values to have names in the first place?
+ 2
Have you thought about using a dictionary? It is not exactly the same, but you can give names to your values.
d = {'a': 5; 'b': 7}
print(d['a'])
Output: 5
+ 2
Thank you Petr, but Indeed values are not assigned
+ 2
Dorian here is the code:
https://code.sololearn.com/cBVhBfHv3o63/?ref=app
+ 2
global a
global b
global c
global d
mylist = [1,2,3,4]
if len(mylist) == 2:
a, b = mylist
elif len(mylist) == 3:
a, b, c = mylist
elif len(mylist) == 4:
a, b, c, d = mylist
# and so on.
print(a, b, c, d)
#note: it does work without the global declaration, but my IDE gives me a warning..so I've included it.
+ 2
rodwynnejones that's never a good stategy, what about a list of 1000 items ? Also, why use global variables ?
+ 2
Thank you Aymane your programme work, it's because we actually have a project to do. In the first question, we have defined a function in which we had already put our lists. However, for the second one, we need to modify these lists. So we actually decided to ask the user to put his lists. But we don’t know how can we use the same program as before because we had defined a variable for each item of the list. Consequently, do we need to change the program or do we have something to help us to define the variables.
+ 2
Dorian well I hope this was what you needed. In naming, it is better to use a1, a2, a3... instead of a, b, c... because it would be more readable
+ 2
@Aymane Boukrouh
I think I miss interpreted the warning my IDE was giving me (I though it was telling me that the variables were local to the "if elif" statements).
1000 items?..try your code with more than 26 items.
+ 2
What's with this one:
https://code.sololearn.com/cafQM33Xo6rv/?ref=app