如何在同一个列表中添加和连接两个变量


array = [[1676, 196, 159, 29, 'invoice'], [1857, 198, 108, 28, 'date:']]
width = 159+108 = 267
height = 29+28 = 57
label = invoice date:
Required solution: [1676, 196, 267, 57, 'invoice date:']

在同一个列表中连接字符串和添加数字是否有解决方案

假设您的其他行用于您自己的笔记/测试,以及您的"所需解决方案";如果是一个错别字,您可以像这样使用zip:

array = [[1676, 196, 159, 29, 'invoice'], [1857, 198, 108, 28, 'date:']]
res = []
for x, y in zip(array[0], array[1]):    # compare values at each index
if isinstance(x, str):              # check to see if x is a string
res.append(x + ' ' + y)         # add a space if x is a string
else:
res.append(x + y)               # add the two integers if x is not a string
print(res)
[3533, 394, 267, 57, 'invoice date:']

请注意,只有当您确定相同的索引将有字符串和整数时,上面的连接才会起作用。