首页 > 解决方案 > 从列表中提取字符串

问题描述

我有一个清单:

my_list = ['A70-11370; reprint; rolled; 2000; 26.5 x 38.5',
 'A70-713; reprint; rolled; 1980; 26.5 x 38.5',
 'b70-7814; reprint; Style A; rolled; 1939; 22.5 x 34.5',
 'A70-7600; reprint; rolled; 1986; 26.5 x 38.5',
 'A70-6912; reprint; style C; rolled; 1977; 26.5 x 38.5',
 'A70-8692; reprint; regular; rolled; 1995; 26.5 x 38.5',
 'A70-2978; reprint; rolled; 1991; 26.5 x 38.5',
 'A70-4902; reprint; Style A; rolled; 1999; 26.5 x 38.5',
 'A70-6300; reprint; regular; rolled; 1983; 26.5 x 38.5',
 'MPW-6725; reprint; rolled; 1966; 26.5 x 38']

我想提取包含“x”的字符串(例如 26.5 x 38.5)。我努力了:

string = [i if 'x' in i else np.nan for i in str(my_string).split(';')]

将 nan 放置在不满足条件的地方,但我只是其中的一部分。有没有办法在有和没有 nan 占位符的情况下获得我想要的字符串?

标签: pythonstring

解决方案


您需要一个嵌套列表推导来获取列表中的每个子字符串。

[x for s in my_list for x in s.split('; ') if 'x' in x]

结果:

['26.5 x 38.5', '26.5 x 38.5', '22.5 x 34.5', '26.5 x 38.5', '26.5 x 38.5', '26.5 x 38.5', '26.5 x 38.5', '26.5 x 38.5', '26.5 x 38.5', '26.5 x 38']

虽然using可能会返回不需要的结果re,但 using 更适合于此:if 'x' in x

p = re.compile("\d+\.\d+ x \d+\.\d+")
[m.group(0) for m in map(p.search, my_list) if m]

推荐阅读