繁体   English   中英

这个列表理解我哪里出错了?

[英]Where am I going wrong with this list comprehension?

我想从字符串中提取 4 位数的年份(2000),如果不存在则返回NoneNaN

w = 'A70-11370; reprint; rolled; 2000; 26.5 x 38.5'

我试过这个,但我得到一个语法错误。

[int(i) for i in w.split(';') if i.isnumeric() else np.nan]

我会去掉空格并重新定位验证检查:

In[0]: [int(i.strip()) if str(i.strip()).isnumeric() else np.NaN for i in w.split(';')]
Out[0]: [nan, nan, nan, 2000, nan]

这肯定有效

x=w.split('; ')

if x[3] == ' ':
    print ("null")
else:
    print (x[3])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM