+ 2

[Python] How can I print biggest one, which are randomly choosen from a list ?

my_list=[1a,1b,2a,2b,3a,3b] Im going to choose 2 items in this list with random module as x and y.The difficult part for me 1a=1b>2a=2b>3a=3b is my rule I need to print x or y which is higher. Ex. if randomly x=1a , y=3b and my code going to tell me x is higher than y.Thanx in advance for your helps

26th Sep 2018, 8:15 PM
Art
12 odpowiedzi
+ 2
Hi Art ! Is this the type of thing you're looking for? -------- from random import choice my_list = ["v7", "a5", "d9", "c3"] weight = {"d9": 1, "c3": 2, "v7": 3, "a5": 4} x = choice(my_list) y = choice(my_list) print("Two random elements:", x, y) print("Bigger: ", max(x, y, key = lambda t: weight[t])) -------- I used the dictionary "weight" to define an order among the elements. Then I used the key argument in max() function to use that ordering. Since 4 > 3 > 2 > 1, for our max function, "a5" > "v7" > "c3" > "d9".
9th Oct 2018, 3:53 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 4
I'm not sure if I understand what you mean, but you can get the highest of two numbers by doing `max(x, y)`. Also you can get the highest number in the list by doing `max(my_list)`.
26th Sep 2018, 8:49 PM
Eduardo Petry
Eduardo Petry - avatar
+ 3
Can you explain this pattern? Two items are equal if they start with the same number, but if they don't have the same number, the one with the "bigger" letter is bigger even if its number is smaller? 🤔
27th Sep 2018, 6:19 AM
Anna
Anna - avatar
+ 2
thanx for your answers but I need to write a code about my rule 1a=1b>2a=2b>3a=3b for it can choose which is big,how can I do that?
27th Sep 2018, 3:42 AM
Art
+ 2
Anna , list=[v7,a5,,d9,c3] and Im going to tell my code a5>v7>c3> d9 then Im going to have 2 items from list with random and what I need to do is my code going to tell me that 2 items ( randomly chosen) which is bigger.Sorry for my English.
27th Sep 2018, 6:40 AM
Art
+ 2
Hi Kishalaya Saha I think it will work for me its a small part of a long code I should arrange that for a huge list. Thank you very much for your help .I really need that 😀
9th Oct 2018, 5:04 PM
Art
+ 2
Yes Anna was right about that, I wrote it like Im following a rule but not,thats why I changed it to "a5"> "v7" >"c3"> "d9" for I can explain that clearly, she is so kind too but I think nowadays she is busy😁
9th Oct 2018, 6:01 PM
Art
+ 2
Okay, thanks for the clarification 😊
9th Oct 2018, 6:31 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Art I'm glad I could help. 😊 Anna has asked you this before, but does your odering have any pattern? The rule "a5" > "v7" > "c3" > "d9" that you mentioned didn't seemed to follow a pattern. That's why I suggested the dictionary. But if you have a specific rule for ordering them, there are much better options, especially for huge lists.
9th Oct 2018, 5:25 PM
Kishalaya Saha
Kishalaya Saha - avatar
0
for checking numbers you could use a for loop greatest = my_list[0] for(i=0;i<my_list.length-1;i++){ if(my_list[i]>my_list[i+1]){ greatest = my_list[i+1] } }
26th Sep 2018, 8:50 PM
~Just Another Brick In The Wall~
~Just Another Brick In The Wall~ - avatar
0
if that's what you ment, sorry if I didn't understand you right
26th Sep 2018, 8:50 PM
~Just Another Brick In The Wall~
~Just Another Brick In The Wall~ - avatar
0
I still need some help about this?
9th Oct 2018, 6:07 AM
Art