| | |
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 |
accessdenied advanced aliased apache application argv beginner bits calling casino change clear command convert corners count csv cturtle cursor def definedlines dynamic dynamically edit event events file float format frange function google homework i/o iframe inches input jaunty keyboard lapse line linux list lists loop matching microphone mouse movingimageswithpygame multiple newb number numbers numeric obexftp output parameters parsing path prime programming projects py py2exe pygame pyopengl python random rational raw_input recursion remote return reverse session signal software sprite statictext string strings syntax tails text threading time tlapse tuple ubuntu unicode unit urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython






