words = [['hey', 'hey you'], ['ok', 'ok no', 'boy', 'hey ma']]
我有一个包含字符串的列表列表.我理解如何从列表中删除特定元素,但不知道如何删除只有一个单词的元素.我想要的输出是:
final = [['hey you'], ['ok no', 'hey ma']]
我正在尝试但我认为这是完全错误的….
remove = [' ']
check_list = []
for i in words:
tmp = []
for v in i:
a = v.split()
j = ' '.join([i for i in a if i not in remove])
tmp.append(j)
check_list.append(tmp)
print check_list
最佳答案 你可以做:
words = [['hey', 'hey you'], ['ok', 'ok no', 'boy', 'hey ma']]
final = [[x for x in sub if ' ' in x.strip()] for sub in words]
# [['hey you'], ['ok no', 'hey ma']]
我只是在所有字符串中搜索空格.