繁体   English   中英

从嵌套列表中删除特定字符

[英]Removing specific characters from a nested list

我有一个列表列表,看起来像[[text],[text],['', ''], ['', ''],[text]]

我试过了

list3 = list2
[x for x in list2 if not '['', '']' in x]

但清单仍然存在。 我只需要删除['', ''], ['', '']这样我就可以遍历整个列表而不会出错。

ll = [['text'],['text'],['', ''], ['', ''],['text']]
result = []

for inner_list in ll:
  if all(inner_list):
    result.append(inner_list)

print(result)

结果

[['text'], ['text'], ['text']]

您正在执行字符串eequals列表检查,该检查将始终返回false。 您可以执行完整列表相等性检查或包含检查。

[x for x in players2 if [","] != x]

要么

[x for x in players2 if "," not in x]

我通过修复您的列表选择器代码来使其工作:

[x for x in list if not ',' in x]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM