python使用建议技巧分享(三)
作者:songleo 时间:2021-07-07 06:58:16
这是一个系列文章,主要分享python的使用建议和技巧,每次分享3点,希望你能有所收获。
1 如何去掉list中重复元素
my_list = [3, 2, 1, 1, 2, 3]
print my_list
# [3, 2, 1, 1, 2, 3]
unique_list = list(set(my_list))
print unique_list
# [1, 2, 3]
或者
from collections import OrderedDict
my_list = [3, 2, 1, 1, 2, 3]
print my_list
# [3, 2, 1, 1, 2, 3]
unique_list = list(OrderedDict.fromkeys(my_list))
print unique_list
# [3, 2, 1]
前一种方式不会保留list的元素顺序,后一种方式会保留list的元素顺序。
2 如何读取dict中的值
不推荐方式
url_dict = {
'google': 'https://www.google.com/',
'github': 'https://github.com/',
'facebook': 'https://www.facebook.com/',
}
print url_dict['facebook']
print url_dict['google']
print url_dict['github']
# print url_dict['baidu']
# KeyError: 'baidu'
# https://www.facebook.com/
# https://www.google.com/
# https://github.com/
推荐方式
url_dict = {
'google': 'https://www.google.com/',
'github': 'https://github.com/',
'facebook': 'https://www.facebook.com/',
}
print url_dict.get('facebook', 'https://www.google.com/')
print url_dict.get('google', 'https://www.google.com/')
print url_dict.get('github', 'https://www.google.com/')
print url_dict.get('baidu', 'https://www.google.com/')
# https://www.facebook.com/
# https://www.google.com/
# https://github.com/
# https://www.google.com/
前一种方式读取一个不存在的key时,会导致KeyError,例如print url_dict[‘baidu'],因为字典中不存在baidu,所以会导致KeyError。后一种方式使用字典的get方法,如果key不存在,不会产生KeyError,如果给了默认值,会返回默认值,否则返回None。
3 如何排序字典
unordered_dict = {'c': 1, 'b': 2, 'a': 3}
print sorted(unordered_dict.items(), key=lambda e: e[1])
# [('c', 1), ('b', 2), ('a', 3)]
print sorted(unordered_dict.items(), key=lambda e: e[0])
# [('a', 3), ('b', 2), ('c', 1)]
print sorted(unordered_dict.items(), key=lambda e: e[1], reverse=True)
# [('a', 3), ('b', 2), ('c', 1)]
第一种方式是按字典的value升序排序,第二种方式是按字典的key升序排序,第三种方式是按字典的value降序排序,和第一种方式相反,因为指定了参数reverse为True。sorted函数功能挺强大,不止可以排序字典,任何iterable对象都可以排序,如果想深入了解请戳https://docs.python.org/2.7/howto/sorting.html#sortinghowto。
来源:https://cloud.tencent.com/developer/article/1123061
标签:建议,python,技巧
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python SMTP发送电子邮件的示例
2023-09-26 17:57:24
asp如何制作一个股票滚屏显示面板?
2010-07-07 12:27:00
PHP中怎样防止SQL注入分析
2023-08-16 04:45:35
使用python实现三维图可视化
2021-07-31 02:28:57
![](https://img.aspxhome.com/file/2023/8/64278_0s.png)
Python全栈之列表数据类型详解
2023-05-05 15:27:10
如何随机显示图片计数器?
2010-05-16 15:21:00
请注意页面head区域的编码是不是规范
2008-08-06 13:14:00
flash与asp/php/asp.net通信的方法第1/3页
2023-11-15 03:43:03
MySQL 连接与管理
2011-09-10 15:52:42
《色彩解答》系列之一 色彩层次
2008-02-17 14:26:00
![](https://img.aspxhome.com/file/UploadPic/20082/17/2008217143752778s.gif)
关联的 script 标签
2009-11-02 10:40:00
新手入门:防范SQL注入攻击的新办法
2009-03-11 15:14:00
Python实现获取nginx服务器ip及流量统计信息功能示例
2023-08-03 02:03:26
PHP中的traits实现代码复用使用实例
2023-11-22 04:27:55
asp.net微信开发(永久素材管理)
2023-07-21 13:02:45
![](https://img.aspxhome.com/file/2023/9/78999_0s.png)
正则表达式结合数组提取文章中的文件名
2007-10-12 13:59:00
你喜欢篮球吗?Python实现篮球游戏
2023-10-26 00:28:36
![](https://img.aspxhome.com/file/2023/6/68456_0s.png)
浅淡BANNER设计
2010-09-25 18:37:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/02-33s.jpg)
asp上传文件自动重命名方法
2007-08-24 09:46:00
SpringBoot 中使用JSP的方法示例
2023-06-16 22:35:09
![](https://img.aspxhome.com/file/2023/5/82045_0s.png)