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


Messages In This Thread
RE: Evaluation of two different list in python? - by perfringo - Apr-17-2019, 07:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  UndefinedEnvironmentName: 'extra' does not exist in evaluation environment EarthAndMoon 3 1,950 Oct-09-2023, 05:38 PM
Last Post: snippsat
  Conditional evaluation stsxbel 7 3,731 Jun-13-2021, 08:27 PM
Last Post: Gribouillis
  list evaluation go127a 2 2,604 Apr-12-2019, 11:13 AM
Last Post: DeaD_EyE
  operand evaluation order? insearchofanswers87 2 2,980 Sep-26-2018, 07:51 PM
Last Post: insearchofanswers87
  Recursive evaluation in parsley parser mqnc 0 2,678 Apr-13-2018, 09:02 PM
Last Post: mqnc
  Learning Python, newbie question about strings and evaluation new_learner_999 13 6,514 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