+ 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
12 Respostas
+ 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".
+ 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)`.
+ 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? đ€
+ 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?
+ 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.
+ 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 đ
+ 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đ
+ 2
Okay, thanks for the clarification đ
+ 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.
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]
}
}
0
if that's what you ment, sorry if I didn't understand you right
0
I still need some help about this?