现在的位置: 首页 > 综合 > 正文

005_012 Python 检查序列的成员 in影响性能,而且不能转换为字典或者集合,需要保留原来的顺序

2017年11月02日 ⁄ 综合 ⁄ 共 385字 ⁄ 字号 评论关闭

代码如下:

#encoding=utf-8


print '中国'


#检查序列的成员 in影响性能,而且不能转换为字典或者集合,需要保留原来的顺序


def addUnique(baselist,otherlist):
    auxDict=dict.fromkeys(baselist) #唯一化
    for item in otherlist: 
        if item not in auxDict:
            baselist.append(item)
            auxDict[item] = None  #加入key列表


alist=[1,2,3,3,2,1]
print dict.fromkeys(alist)




print alist
addUnique(alist,[1,4])


print alist

打印如下:

中国
{1: None, 2: None, 3: None}
[1, 2, 3, 3, 2, 1]
[1, 2, 3, 3, 2, 1, 4]

抱歉!评论已关闭.