python实用技巧

  1. 云栖社区>
  2. 博客>
  3. 正文

python实用技巧

郭大瘦 2017-12-07 11:33:01 浏览608
展开阅读全文

很多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'}

网友评论

登录后评论
0/500
评论