美文网首页
Python字典推导式

Python字典推导式

作者: 李蕴Ronnie | 来源:发表于2020-03-20 18:44 被阅读0次
dic = {'a':1,
       'b':2,
       'c':3
      }
print(dic.items())

keys = [key for key,value in dic.items()]
print(keys)

values = [value for key,value in dic.items()]
print(values)

keys1 = {value:key for key, value in dic.items()}
print(keys1)

keys2 = {key:value for key, value in dic.items() if key == 'a'}
print(keys2)

运行结果

dict_items([('a', 1), ('b', 2), ('c', 3)])
['a', 'b', 'c']
[1, 2, 3]
{1: 'a', 2: 'b', 3: 'c'}
{'a': 1}

相关文章

网友评论

      本文标题:Python字典推导式

      本文链接:https://www.haomeiwen.com/subject/uahsyhtx.html