Apr-17-2019, 07:21 PM
(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 Smile](https://python-forum.io/images/smilies/smile.png)
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.
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.
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.