Python 列表去重合并 - 将 a = [('pid', '', ''), ('pid', '', ''), ('', 'se_area', ''), ... ] 转换为 1,se_area,pid
首先,我们需要对列表进行去重操作。可以使用 set() 函数将列表转换为集合,再将集合转换回列表即可:
a = list(set(a))
接下来,我们需要将相同的字符串组合在一起。可以使用字典来实现这一操作:
result = {}
for item in a:
for i in range(len(item)):
if item[i] != '':
if item[i] not in result:
result[item[i]] = set()
result[item[i]].add(i)
最后,我们需要将字典转换为字符串:
output = []
for i in range(len(a[0])):
for key, value in result.items():
if i in value:
output.append(key)
output = list(set(output))
output.sort()
print(','.join(output))
output = []
原文地址: https://www.cveoy.top/t/topic/mIZY 著作权归作者所有。请勿转载和采集!