last day (16 days later) » 

2:10 PM
2
A: How to assign words to values in a list?

Daniel MesejoYou could create a lookup dictionary: my_list = [72, 50, 3, 50, 16, 72, 3, 72, 3, 50] colors = ['red', 'blue', 'green', 'black', 'yellow', 'purple'] lookup = dict(zip(set(my_list), colors)) output = [lookup[number] for number in my_list] print(output) Output ['red', 'green', 'black', 'green'...

 
Omg, this is perfect! Just what I was looking for. ;-)
One more question. Now, in this example, the look-up selects the first four colors and randomly maps them with the four unique numbers and applies that color-guide for the other numbers. How can you make sure the first unique element in the list of numbers is mapped with the first element from the list of colors? (without manually setting the dictionary like you showed in the last example; suppose the values itself are unknown in the beginning). So that when you run the code, you always get the same output, now the order in which it picks unique colors is variable.
 
What would be the first unique element?
You could filter the list in order using a set
 
Well let's say in this list the first unique number is the first one, then the second, third and then the fifth is the last unique number. Is is possible to map them fixed with the elements from the colors-list. first unique number with first color; second unique number with second color etc. in that order? It's just a question, if it's too complex that's ok, but it would make the output fixed (instead of variable as it is now) ;-)
Do you mean: lookup = dict(zip(set(my_list), set(colors))) ? so adding set to colors?
 
Hi yeah, still looking at the link.
Is my question clear actually?
So unique elements if you start from the left it's: 72, 50, 3, 16.
 
2:13 PM
Yes, I think so
 
The code then assigns them to a color
 
What I mean is something like this:
result = [72, 50, 3, 16]
so the elements are unique but the order of occurrence is preserved
 
but one time(72=red, 50=blue, 3=green, 16=black)
but the second time(72=blue, 50=red, 3=black, 16=green)
and the third time is then again different
so there is variability in the output
 
See the new code
it should preserve order
 
I'll take a look ;-)
Do you mean the code in your elaborate answer yesterday? I don't see the edits yet I think.
 
2:22 PM
No
Did you get a message here with the code?
I will added to the quesiton
to the answer i mean
 
no I didn't get the message with the code
Perfect, I'm curious
 
just added it
 
yeah I see, but hmm that does not completely solve my question yet actually
What I want is that when you run the code (your answer to my question of yesterday), that you get the same output each time you run it:
output1: ['red', 'green', 'black', 'green', 'blue', 'red', 'black', 'red', 'black', 'green']

But now the 2nd time you run it, it can be:
output2: ['blue', 'green', 'black', 'green', 'red', 'blue', 'black', 'blue', 'black', 'green']

and the third time again differnt:
output3: ['red', 'black', 'green', 'black', 'blue', 'red', 'green', 'red', 'green', 'black']
 

last day (16 days later) »