[英]Where am I going wrong with this list comprehension?
I want to extract the 4 digit year (2000) from a string or return None
or NaN
if its not present.我想从字符串中提取 4 位数的年份(2000),如果不存在则返回
None
或NaN
。
w = 'A70-11370; reprint; rolled; 2000; 26.5 x 38.5'
I tried this but I get a syntax error.我试过这个,但我得到一个语法错误。
[int(i) for i in w.split(';') if i.isnumeric() else np.nan]
I'd strip the whitespace and reposition the validation check:我会去掉空格并重新定位验证检查:
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]
This must surely work这肯定有效
x=w.split('; ')
if x[3] == ' ':
print ("null")
else:
print (x[3])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.