a.r*_*and 6 python tuples list
我试图从元组列表中删除所有字符串
ListTuples = [(100, 'AAA'), (80, 'BBB'), (20, 'CCC'), (40, 'DDD'), (40, 'EEE')]
Run Code Online (Sandbox Code Playgroud)
我已经开始尝试找到解决方案:
output = [i for i in ListTuples if i[0] == str]
print(output)
Run Code Online (Sandbox Code Playgroud)
但我似乎无法理解如何获得如下输出:
[(100), (80), (20), (40), (40)]
Run Code Online (Sandbox Code Playgroud)
格式始终是(int,str).
使用嵌套的元组理解和isinstance:
output = [tuple(j for j in i if not isinstance(j, str)) for i in ListTuples]
Run Code Online (Sandbox Code Playgroud)
输出:
[(100,), (80,), (20,), (40,), (40,)]
Run Code Online (Sandbox Code Playgroud)
请注意,元组中有尾随逗号区分它们,例如(100)它们是相同的100.