Python Forum
Evaluation of two different list in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Evaluation of two different list in python?
#1
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
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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)
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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]
Reply
#6
(Apr-17-2019, 01:35 PM)go127a Wrote: how can I save the index of 12 in below list?
l=[1,2,3,12,4,5,6,12]

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? Smile

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))
3
This is the index of first occurrence of maximum value in list. No need to exit interactive interpreter, all the answers are there.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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)
dd=[]
for num in l:
if a==num:

ind=l.index(num)
dd.append(ind)

print(dd)
and the output is: [3, 3]
Reply
#8
I have used 'enumerate'

I can reach my goal now.

thanks to all
Reply
#9
(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 Wink
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)    
Output:
3 7
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,621 Oct-09-2023, 05:38 PM
Last Post: snippsat
  Conditional evaluation stsxbel 7 3,421 Jun-13-2021, 08:27 PM
Last Post: Gribouillis
  list evaluation go127a 2 2,432 Apr-12-2019, 11:13 AM
Last Post: DeaD_EyE
  operand evaluation order? insearchofanswers87 2 2,791 Sep-26-2018, 07:51 PM
Last Post: insearchofanswers87
  Recursive evaluation in parsley parser mqnc 0 2,555 Apr-13-2018, 09:02 PM
Last Post: mqnc
  Learning Python, newbie question about strings and evaluation new_learner_999 13 5,864 Feb-18-2018, 03:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020