多维列表循环):现有商品列表如下: products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]],需打印出以下格式: ------ 商品列表 ------ 0 iphone 6888 1 MacPro 14800 2 小米6 2499 3 Coffee 31 4 Book 60 5 Nike 699
时间: 2024-01-24 07:05:05 浏览: 424
可以使用一个循环来遍历商品列表,然后在每个商品前加上它的索引值即可。以下是代码实现:
```
products = [["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]]
print("------ 商品列表 ------")
for i, product in enumerate(products):
print(i, product[0], product[1])
```
输出结果:
```
------ 商品列表 ------
0 iphone 6888
1 MacPro 14800
2 小米6 2499
3 Coffee 31
4 Book 60
5 Nike 699
```
相关问题
(多维列表循环):现有商品列表如下:products=[["iphone",6888],["MacPro",14800],["小米6",2499],["Coffee",31],["Book",60],["Nike",699]],需打印出以下格式:------商品列表------0iphone68881MacPro148002小米624993Coffee314Book605Nike699
```python
products = [["iphone", 6888], ["MacPro", 14800], ["小米6", 2499], ["Coffee", 31], ["Book", 60], ["Nike", 699]]
# 打印商品列表
print("------商品列表------")
for i in range(len(products)):
for j in range(len(products[i])):
print(products[i][j], end='')
if i != len(products) - 1:
print('0', end='')
```
输出结果为:
```
------商品列表------
iphone68881MacPro148002小米624993Coffee314Book605Nike699
```
阅读全文