3
 a = ['in 1978 by', 'History', 'members', 'albums', 'June 4th, 1979', 'October 7,1986): "The Lounge', 'In 1984 the', 'early 1990s; prominent']

上面的列表有历史之类的词,成员中没有数字,所以我想删除它们

 # output would be
 a = ['in 1978 by', 'June 4th, 1979', 'October 7, 1986', 'In 1984 the', 'early 1990s; prominent']
4

3 回答 3

8

保留你想要的:

a = ['in 1978 by', 'History', 'members', 'albums', 'June 4th, 1979', 'October 7,1986): "The Lounge', 'In 1984 the', 'early 1990s; prominent']

new = [el for el in a if any(ch.isdigit() for ch in el)]
# ['in 1978 by', 'June 4th, 1979', 'October 7,1986): "The Lounge', 'In 1984 the', 'early 1990s; prominent']
于 2013-06-18T00:14:52.603 回答
1

这是一个较短的替代方案,使用any()and string.digits

from string import digits

a = ['in 1978 by', 'History', 'members', 'albums', 'June 4th, 1979', 
     'October 7,1986): "The Lounge', 'In 1984 the', 'early 1990s; prominent']

[x for x in a if any(y in x for y in digits)]

=> ['in 1978 by', 'June 4th, 1979', 'October 7,1986): "The Lounge',
    'In 1984 the', 'early 1990s; prominent']
于 2013-06-18T00:26:59.303 回答
0

使用正则表达式和列表推导,这是一个单行:

import re
[i for i in a if re.search('\d', i) is not None]
于 2013-06-18T00:27:00.960 回答