我有以下列表列表,我希望包含“|”的列表列表中的所有字符串字符。
l = [['a','b','c|','d'],['1|','|2','3|','4'],['1|','2','3|','4','']]
结果:
l = ['c|', '1|', '3|', '1|', '3|']
答案 0 :(得分:3)
无需使用正则表达式:
>>> l = [['a','b','c|','d'],['1|','2','3|','4'],['1|','2','3|','4','']]
>>> [x for xs in l for x in xs if x.endswith('|')]
['c|', '1|', '3|', '1|', '3|']
>>> [x for xs in l for x in xs if '|' in x]
['c|', '1|', '3|', '1|', '3|']
答案 1 :(得分:0)
使用过滤器:
filter(lambda x: "|" in x, sum(l, []))
(sum(l, [])
将子列表合并为一个大列表)