我拥有的是
brand = ['Apple','Oppo','Huawei']
model = ['IPhone SE', 'K10 5G', 'Honor X7']
price = ['400','380','160']我在寻找输出
brand : Apple
model : Iphone SE
price : '400'
brand : Oppo
model : K10 5G
price : '380'
brand : Huawei
model : Honor X7
price : '160'我试过了
brand = ['Apple','Oppo','Huawei']
model = ['IPhone SE', 'K10 5G', 'Honor X7']
price = ['400','380','160']
for i in brand:
for j in model:
for k in price:
print('brand : ' + i + '\nmodel : ' + i + '\nprice : ' + k) 错误说明它只能从str not list连接。
如果有任何帮助的话,我不擅长处理字符串:/
发布于 2022-05-23 16:41:23
zip()内置函数正是为此任务设计的:
brands = ['Apple','Oppo','Huawei']
models = ['IPhone SE', 'K10 5G', 'Honor X7']
prices = ['400','380','160']
for brand, model, price in zip(brands, models, prices):
print('brand :', brand)
print('model :', model)
print('price :', price)
print()发布于 2022-05-23 16:41:37
要迭代不同列表中的项的对应对/三元组/等等,可以使用zip()。
此外,在这里使用f字符串比使用级联更容易。
for (i, j, k) in zip(brand, model, price):
print(f'brand : {i}\nmodel : {j}\nprice : {k}')https://stackoverflow.com/questions/72351988
复制相似问题