python dict.fromkeys()研究
def unique(seq):
#return [x for x in my_list if x not in locals()['_[1]']]
return {}.fromkeys(seq).keys() dict.fromkeys(seq,val=None) #创建并返回一个新字典,以序列seq中元素做字典的键,val为字典所有键对应的初始值(默认为None)
例子:
1,
>>> l = [1,2,3]
>>> a3 = {}.fromkeys(l)
>>> print a3
{1: None, 2: None, 3: None}
>>> len(a3)
3 2,>>> d = {}.fromkeys(l).keys()
>>> print d
[1, 2, 3]
>>> len(d)
3
还可对list和string去重
python dict.fromkeys()研究的更多相关文章
- Python dict operation introduce
字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示: d = ...
- Python 字典 fromkeys()方法
Python 字典 fromkeys() 方法用于创建一个新的字典,并以可迭代对象中的元素分别作为字典中的键,且所有键对应同一个值,默认为None. fromkeys() 方法语法: 1 dict.f ...
- Python:fromkeys()方法
简介 Python 字典(Dictionary) fromkeys() 函数用于创建一个新字典,以序列seq中元素做字典的键,value为字典所有键对应的初始值. 语法 fromkeys()方法语法: ...
- Python——Dict
Python字典(Dictionary) 字典是一种可变容器模型,可存储任意类型对象. 字典的每个键值(key => value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花 ...
- Python dict(或对象)与json之间的互相转化
Python dict(或对象)与json之间的互相转化 原文转载自 1.JSON:JavaScript 对象表示法,是轻量级的文本数据交换格式,独立于语言,平台 2.JSON 语法规则 数据在名称/ ...
- python dict的函数
1. dict.clear() 删除字典内所有元素 2. dict.copy() 返回一个字典的浅复制 3. dict.fromkeys(seq[, val]) 创建一个新字典,以序列 seq 中元素 ...
- python & dict & switch
python & dict & switch python 中是没用switch语句的,这应该是体现python大道至简的思想,python中一般多用字典来代替switch来实现. # ...
- Python dict() 函数
Python dict() 函数 Python 内置函数 描述 dict() 函数用于创建一个字典. 语法 dict 语法: class dict(**kwarg) class dict(mappi ...
- python dict操作
d1 = {'one': 1, 'two': 2} d2 = {'one': 1, 'two': 2} d3 = {'one': 1, 'two': 2} print(dir(d1)) # 1.con ...
随机推荐
- NovaException: Unexpected vif_type=binding_failed
nova/virt/libvirt/vif.py: _("Unexpected vif_type=%s") % vif_type) NovaException: Unexpecte ...
- Springboot- Caused by: org.hibernate.AnnotationException: No identifier specified for entity:
错误与异常: Caused by: org.hibernate.AnnotationException: No identifier specified for entity: 原因:引用了不对的包, ...
- compile to 32-bit elf file
nasm -f elf -o a.o a.asm gcc -c -m32 -o b.o b.c ld -s -m elf_i386 -Ttext 0x30400 -o b.bin b.o a.o
- 去除sql中不可见字符的n种方法
CREATE TABLE [ASCII0127] ( Bin INT, Dec INT, Hex VARCHAR(128), Abbr ...
- 184. Department Highest Salary
问题描述 解决方案 select b.Name Department,a.Name Employee,a.Salary from ( select e1.Salary,e1.Name,e1.Depar ...
- Announcing the Release of ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 for VS2012
The NuGet packages for ASP.NET MVC 5.1, ASP.NET Web API 2.1 and ASP.NET Web Pages 3.1 are now live o ...
- HDU 2203 kmp
http://acm.hdu.edu.cn/showproblem.php?pid=2203 亲和串 Time Limit: 3000/1000 MS (Java/Others) Memory ...
- ElasticSearch_学习_00_资源帖
一.官方资料 1.Elasticsearch 权威指南(中文版) 二.精选资料 1. 全文搜索引擎 Elasticsearch 入门教程-阮一峰 2. 三.参考资料
- Git_学习_07_ 推送修改到远端
一.操作流程 多人协作时,若自己的本地代码有了修改,想提交自己的代码,就需要按照以下步骤操作: 1.确认修改正确 使用以下命令,查看有哪些是自己未提交的代码 git status 2.拉取远程最新代码 ...
- group by 和 聚合函数
1.在oracle中 select * from Table group by id 会报错. 会报不是group by 表达式.为什么一定不能是 * ,而必须是分组的列或者某个列的聚合函数. 在my ...