1.输出列表中内容以及下标
product_list=['伊利酸奶','TF口红','水杯','咖啡','牙刷']
for index,x in enumerate(product_list):
print index,x
结果:
2.输出元祖中内容以及下标
product_list1=('伊利酸奶','TF口红','水杯','咖啡','牙刷')
for index,x in enumerate(product_list1):
print index,x
3.列表中元素为元祖
product_list2=[('TF口红',256)]
print product_list2
#解决列表中中文显示问题
print json.dumps(product_list2, encoding='UTF-8', ensure_ascii=False)
使用json.dumps()方法解决,需要导入import json
4.打印列表中下标和值
import json
productlist=[('伊利酸奶',7),('TF口红',256),('日本老虎吸管杯',550),('娇韵诗双萃精华',550),('奥利奥饼干',15),('儿童平衡车',1500)]
for index,x in enumerate(productlist):
print index,json.dumps(x,encoding='utf-8',ensure_ascii=False)