| | |
Numeric elements of a mixed list
Thread Solved |
Right now I have a mixed list of different types of elements:
How can I find the maximum numeric element of the list?
python Syntax (Toggle Plain Text)
mixed_list = [1, 0.5, 0, 3, 'a', 'z'] print max(mixed_list) # shows z, but would like it to be 3
The easiest way is to extablish a temporary list of numeric values ...
You can shorten this with a list comprehension ...
python Syntax (Toggle Plain Text)
mixed_list = [1, 0.5, 0, 3, 'a', 'z'] print max(mixed_list) # shows z, but would like it to be 3 number_list = [] for x in mixed_list: if type(x) in (int, float): number_list.append(x) print number_list print max(number_list) # 3
python Syntax (Toggle Plain Text)
mixed_list = [1, 0.5, 0, 3, 'a', 'z'] print max([x for x in mixed_list if type(x) in (int, float)]) # 3
May 'the Google' be with you!
![]() |
Similar Threads
- Starting Python (Python)
- Quicksorting linked list - simple algorithm (C)
- [req]How to move elements in the list box up and down using command button? (Visual Basic 4 / 5 / 6)
- min/max of a mixed type list (Python)
- Unsorted List ADT Client Code Help (C++)
- how to generate subsets from a list? (Python)
- problem with a list!! (C++)
- WHERE field *has a numeric value?* (MS SQL)
- remove method linked list (C)
- Shifting Elements in an Array k places to the right. (Java)
Other Threads in the Python Forum
- Previous Thread: Animated Images
- Next Thread: play sound help
| Thread Tools | Search this Thread |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






