在列表理解中将 str 转换为 int

Sai*_*mar 0 python list-comprehension python-3.x

我有一个以年份作为字符串的列表,但有一些缺失的年份被表示为空字符串。

我正在尝试将这些字符串转换为整数并跳过无法使用列表理解和 try 和 except 子句转换的值?

birth_years = ['1993','1994', '' ,'1996', '1997', '', '2000', '2002']
Run Code Online (Sandbox Code Playgroud)

我尝试了这段代码,但它不起作用。

try:
    converted_years = [int(year) for year  in birth_years]
except ValueError:
    pass

required output:
converted_years = ['1993','1994','1996', '1997', '2000', '2002']
Run Code Online (Sandbox Code Playgroud)

小智 6

[int(year) for year in birth_years if year.isdigit()]