3 回答

TA贡献1951条经验 获得超3个赞
使用嵌套元组理解和isinstance
:
output = [tuple(j for j in i if not isinstance(j, str)) for i in ListTuples]
输出:
[(100,), (80,), (20,), (40,), (40,)]
请注意,元组中有尾随逗号以将它们与例如(100)
相同的100
.

TA贡献1719条经验 获得超6个赞
由于提取每个元组的第一项就足够了,您可以解包并使用列表理解。对于元组列表:
res = [(value,) for value, _ in ListTuples] # [(100,), (80,), (20,), (40,), (40,)]
如果您只需要一个整数列表:
res = [value for value, _ in ListTuples] # [100, 80, 20, 40, 40]
对于后者的功能替代品,您可以使用operator.itemgetter:
from operator import itemgetter
res = list(map(itemgetter(0), ListTuples)) # [100, 80, 20, 40, 40]

TA贡献1898条经验 获得超8个赞
尝试这个
ListTuples = [(100, 'AAA'), (80, 'BBB'), (20, 'CCC'), (40, 'DDD'), (40, 'EEE')]
ListInt = []
ListStr = []
for item in ListTuples:
strAux = ''
intAux = ''
for i in range(0, len(item)):
if(isinstance(item[i], str)):
strAux+=item[i]
else:
intAux+=str(item[i])
ListStr.append(strAux)
ListInt.append(intAux)
print(ListStr)
'''TO CONVERT THE STR LIST TO AN INT LIST'''
output= list(map(int, ListInt))
print(output)
输出是 [100, 80, 20, 40, 40]
添加回答
举报