|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码1:
products = \
[["iphone", 6888], ["MacPro", 14800], ["coffee", 32], ["abc", 2499],
["Book", 60], ["Nike", 699], ["MacPro", 45600], ["coffee", 432],
["abc", 244499], ["Book", 6230], ["Nike", 61299], ["MacPro", 14800],
["coffee", 32], ["abc", 2499], ["Book", 60], ["Nike", 699]]
print("-" * 10 + "商品列表" + "-" * 10)
i = 0
for product in products:
print("{:<6}\t{:<10}\t{:<10}".format(str(i), product[0], str(product[1])))
i += 1
代码2:
products = \
[["iphone", 6888], ["MacPro", 14800], ["coffee", 32], ["abc", 2499],
["Book", 60], ["Nike", 699], ["MacPro", 45600], ["coffee", 432],
["abc", 244499], ["Book", 6230], ["Nike", 61299], ["MacPro", 14800],
["coffee", 32], ["abc", 2499], ["Book", 60], ["Nike", 699]]
print("-" * 10 + "商品列表" + "-" * 10)
i = 0
for product in products:
print("{:<6}\t{:<10}\t{:<10}".format(str(i), product[0], product[1]))
i += 1
两段代码输出的都是:
----------商品列表----------
0 iphone 6888
1 MacPro 14800
2 coffee 32
3 abc 2499
4 Book 60
5 Nike 699
6 MacPro 45600
7 coffee 432
8 abc 244499
9 Book 6230
10 Nike 61299
11 MacPro 14800
12 coffee 32
13 abc 2499
14 Book 60
15 Nike 699
我的问题:
两段代码输出的结果是一样的,那红色部分有何区别或意义?正常应该用哪段代码呢?
set(product[1])的意思是把product下标为1的数据转化为str格式
也就是["MacPro", 14800]现在应该是str格式
既然都一样,那用哪个都没区别
|
|