I'm trying to get the maximum even number from the Python list with the code below, but I get an error.
I understand that the code below else is bad, but I don't know how to write it.
I would appreciate it if you could teach me.
print (max ([1,5,81,41, -54,2], key = lambda x: x if x% 2 == 0 else continue))
Answer # 1
If there are no even numbers (the list is empty)
defaultYou can specify.
lst = [1, 2, 3, 4, 5] print (max ([n for n in lst if n% 2 == 0], default = 0)) # Four lst = [1, 3, 5] print (max ([n for n in lst if n% 2 == 0], default = 0)) # 0
↑ Returns 0 if there is no even number
Answer # 2
If you want to take advantage of the original shape, use "negative infinity"
import math print (max ([1,5,81,41, -54,2], key = lambda x: x if x% 2 == 0 else -math.inf))
-9999I think it's okay.
If you use it for general purposes, you also need to decide what you want to do when there are no even numbers.
Answer # 3
If you want to write in one line, is it here?
[x for x in [1,5,81,41, -54,2] if x% 2 == 0]A list of even numbers is generated in the part of, and its max is taken.
print (max ([x for x in [1,5,81,41, -54,2] if x% 2 == 0]))
Answer # 4
print (([None] + sorted (filter (lambda x: x% 2 == 0, a))) [-1])
- i want to set the maximum value of the slider in python to the number entered in the text box
- python - sumy by specifying the maximum number of characters with sumy
- python: calculate the maximum partial sum of lists
- python style i want to know the background color + maximum value
- python - when you want to judge by the number of characters from the back with a regular expression
- python - how to get the number of searches for a specific word within the period
- python - i want to add a line number to the data frame
- number of python combinations
- c - the maximum value will be a negative number
- python 3x - i want to output the name corresponding to the number entered in the excel file
- python - valueerror: field'id' expected a number but got'suzukitadashi'
- python - i want to divide a binary number into two, store it in a list, and represent it in a different decimal number
- analysis from python stock price data, maximum and minimum
- i want to invert the number 12345 in python like 54321 and display it
- get the number of characters from the python text box
- get the total number and name of python grouping
- how to align the number of elements with overlaplengt of python etc
- set python excel serial number to date and time
- how to multiply each element by a number in a python 2d list
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- xcode - pod install [!] no `podfile 'found in the project directory