python 3.x - Isolating lists based on value in python3 -


I have a set of lists that I want to compare to the values ​​before the lists, and then in that situation, give the individual element Or more than two lists are of the same value

  my_list1 = [2, 3, 2, 4, 5] my_list2 = [1, 3, 2, 3, 2] my_list3 = [1, 1, 2, 2, 2] my_list4 = [3, 2, 2, 4, 5]  

Logic test is fine for a full winner, but the problem I am facing is one Separates the list in the status of the draw - then my_list1 and my_list4 The scenario above will further isolate the logic test because their totals fall on both 16 .

This is what I have done so far

  my_list1 = [1,1,2,2,2] my_list2 = [1,1,1,1, 2] my_list3 = [2,2,1, 1 2] my_list1 total = amount (my_list1) my_list2 total = sum (my_list2) my_list3 total = sum (my_list3) my_list1 total & gt; My_list2 total and my_list1 total & gt; My_list3 Total: Print ("The list is the highest point") elif my_list2Total & gt; My_list1Total and my_list2Total & gt; My_list3Total: Print ("The list is the highest point in two") elif my_list3Total & gt; My_list2 total and my_list3Total & gt; My_list1 Total: Print ("The list is the highest point in three") and: Print ("Draw") # # So now I want to compare the list with the same total number but this time the first element in the list in this case This will be my_list1 [0] and my_list3 [0], which will be compared to the next, the winner of the highest value in position 0 of drawing lists  

I have a single list that keeps all your lists, then you can find the largest element The new list max . Or, if you want to index an index and not just its value, then you can type a maximum of method and use it instead.

  Built-in function like "Max", # indicates the largest element in the position # of the largest element DEF index_of_max (seq, key = lambda item: item): return The maximum (range (lane (CEC)) key = lambda idx: key (cc [idx]) lists = [[2, 3, 2, 4, 5], [1, 3, 2, 3, 2], [ 1, 2, 2, 2], [3, 2, 2, 4, 5]] idx = index_of_max (lists, key = lambda item: (sum (items), items [0])) # Add one because Python lists NOR is indexed, # but the original numbering scheme started on one.Printing "list # {} is the largest." Format (IDX + 1)  

Result:

  List # 4 is the largest. A small explanation about the   

key : This is an action that you can call The maximum passes through, that it uses comparisons to determine the value of two objects in the sequence of keys (some items) on both objects The maximum number of calls between the two is considered as the maximum thing between the two items, the main function I used here gives a tuple, by the way, comparison is done with the first amount, then a tie breaker By using the first element of each list in the form.

If you are thinking, "But what if the first elements are the same? I want to use each item as a tie breaker", then you in return to compare them all to the key Can modify.

  idx = index_of_max (lists, key = lambda item: [sum]) + item)  

Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -