Evaluation of two different list in python? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Evaluation of two different list in python? (/thread-17599.html) |
Evaluation of two different list in python? - go127a - Apr-17-2019 I reach two below list in my program. and l2 is derived from l1. l1=[767, 665, 999, 895, 907, 796, 561, 914, 719, 819, 555, 529, 672, 933, 882, 869, 801, 660, 879, 985] l2=[4, 8, 8, 4, 2, 6, 8, 4, 2, 12, 8, 3, 24, 4, 18, 4, 6, 24, 4, 4] now I am going to print out the maximum from l2, and the same indexing number in l1 as an output. and if I have same max in l2, I shoud print out the max l2. plus the bigger number in l1 as an output. for example in my two list the correct output would be: 672 24 RE: Evaluation of two different list in python? - perfringo - Apr-17-2019 If you have trouble defining expected result in spoken language you can't put it into code. Quote:now I am going to print out the maximum from l2, and the same indexing number in l1 as an output. I have no idea what the second part means, but first number should be maximum from l2 . Which is:In [1]: l2=[4, 8, 8, 4, 2, 6, 8, 4, 2, 12, 8, 3, 24, 4, 18, 4, 6, 24, 4, 4] In [2]: max(l2) Out[2]: 24 However, you stated that correct output will start with number 672 RE: Evaluation of two different list in python? - go127a - Apr-17-2019 thanks, but there are two 24 in l2. so I should check the index of them. and print the bigger number in l1 and max(l2) RE: Evaluation of two different list in python? - perfringo - Apr-17-2019 You should define your objective. Whatever way I interpret your task in spoken language one must always print maximum value from l2 first. Clearly this is not the case with expected result. So - either wording of task or expected result is wrong. RE: Evaluation of two different list in python? - go127a - Apr-17-2019 I have tried to explain it easier here: how can I save the index of 12 in below list? l=[1,2,3,12,4,5,6,12] RE: Evaluation of two different list in python? - perfringo - Apr-17-2019 (Apr-17-2019, 01:35 PM)go127a Wrote: how can I save the index of 12 in below list? Python Zen states: "In the face of ambiguity, refuse the temptation to guess." You ask how to save index of 12. If this list observed following questions arise: what do you mean by index of 12, is it: - index of first 12 in list? - index of second 12 in list? - index of both 12-s in list? - index of maximum value in list? As you can see, this 'easy' explanation makes things pretty complicated. Should I refuse temptation to guess? So I just define objective myself: 'find index of list element which has maximum value'. This objective can be decomposed into following: - find list element with maximum value - find index of element Then I think: is this task unique and I am the first person who faces this challenge or is it something that programmers face every day? I incline towards latter. So I start to look for Python built-in functions and methods for keywords maximum and index. Firs I scan names of all built-in functions: >>> dir(__builtins__)[80:] ['all', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']No index but there is promising max. Let's check it out: >>> help(max) Help on built-in function max in module builtins: max(...) max(iterable, *[, default=obj, key=func]) -> value max(arg1, arg2, *args, *[, key=func]) -> value With a single iterable argument, return its biggest item. The default keyword-only argument specifies an object to return if the provided iterable is empty. With two or more arguments, return the largest argument. (END)Yep, 'return biggest item' is way to go. Now - how do I find index of element? As I have list I go for list methods: >>> list. # two times TAB list.append( list.count( list.insert( list.remove( list.clear( list.extend( list.mro( list.reverse( list.copy( list.index( list.pop( list.sort(Lets see what list.index does, shall we? >>> help(list.index) Help on method_descriptor: index(self, value, start=0, stop=9223372036854775807, /) Return first index of value. Raises ValueError if the value is not present. (END)Seems like it. Now I have to combine those: >>> l = [1,2,3,12,4,5,6,12] >>> l.index(max(l)) 3This is the index of first occurrence of maximum value in list. No need to exit interactive interpreter, all the answers are there. RE: Evaluation of two different list in python? - go127a - Apr-22-2019 ok thanks a lot for your answer i have tried to solve it in this way, but the program output. just find the first maximum number. Quote:a=max(l)and the output is: [3, 3] RE: Evaluation of two different list in python? - go127a - Apr-22-2019 I have used 'enumerate' I can reach my goal now. thanks to all RE: Evaluation of two different list in python? - snippsat - Apr-22-2019 (Apr-22-2019, 12:30 PM)go127a Wrote: I have used 'enumerate'Just to show a example of this if someone wonder about the same,as you did not lst = [1, 2, 3, 12, 4, 5, 6, 12] max_value = max(lst) for index, item in enumerate(lst): if item == max_value: print(index)
|