a = ['12', '23', 35, '45', 89] b = [11, 34, 75, '54', '78']

new_list = [] for i in range(len(a)): if isinstance(a[i], int) and isinstance(b[i], int): new_list.append(a[i] + b[i]) elif isinstance(a[i], str) and isinstance(b[i], str): new_list.append(int(a[i] + b[i])) else: new_list.append(str(a[i]) + str(b[i]))

print(new_list) # [23, 57, '3575', '99', '7889']

Python 列表元素处理:合并相同索引元素

原文地址: https://www.cveoy.top/t/topic/nqbK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录