python dict.fromkeys()研究
生活随笔
收集整理的這篇文章主要介紹了
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去重轉載于:https://www.cnblogs.com/chenjianhong/p/4144381.html
總結
以上是生活随笔為你收集整理的python dict.fromkeys()研究的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在php中使用sockets:从新闻组中
- 下一篇: 使用python远程登录