很多python的小技巧,实际调试和工程应用中都可以用到
1、print打印分割符
直接用print中参数sep进行换行,分割线的快速打印。
>>> print('This is a pig','This is a dog','This is a cat')
This is a pig This is a dog This is a cat
>>> print('This is a pig','This is a dog','This is a cat',sep='\n')
This is a pig
This is a dog
This is a cat
>>> print('This is a pig','This is a dog','This is a cat',sep='\n-----------\n')
This is a pig
-----------
This is a dog
-----------
This is a cat
2、zip压缩tuple,逐个元素提取打包
zip命令可以对tuple等可以迭代的元素进行分类打包
>>> name = ['pig','dog','cat']
>>> price = ['100','150','200']
>>> for name,price in zip(name,price):
data={
'name':name,
'price':price,
}
print(data)
{'name': 'c', 'price': '2'}
{'name': 'a', 'price': '0'}
{'name': 't', 'price': '0'}