1. 将两个数组合并成一个对象
- 将两个一维数组合并成一个对象
例如:key= ['name','age'],value= ['小明',18]合并成new_data = {'name':'小明','age':18}
values = ['小明', 18]
keys = ['name', 'age']
new_data = [{key: value} for key, value in zip(keys, values)]
print(new_data) # [{'name': '小明'}, {'age': 18}]
- 将二维数组和一位数组合并成一个对象
values = [['小明', 18], ['小红', 19]]
keys = ['name', 'age']
new_data = [{k: v for k, v in zip(keys, sublist)} for sublist in values]
print(new_data) # [{'name': '小明', 'age': 18}, {'name': '小红', 'age': 19}]
- 将二维数组进行转置
data = [[1, 2, 3], [3, 4, 5]]
result = list(map(list, zip(*data)))
print(result) # [[1, 3], [2, 4], [3, 5]]