cutomorrowsmile

1. dict函数

dict函数时python内置创建字典的函数,用法如下:

person1 = [\'Tom\', \'10\']
person2 = [\'Lily\', \'11\']
dict1 = dict([person1, person2])
print(dict1)

# {\'Tom\': \'10\', \'Lily\': \'11\'}

 

还有另一种直接使用赋值的用法:

dict1 = dict(tom = 10, lily = 11)
print(dict1)

# {\'tom\': 10, \'lily\': 11}

 

2. 使用fromkeys函数:

fromkeys函数在确定键的情况下可以帮助快速创建初始字典

比如:

keys = [\'name\', \'age\', \'gender\', \'contact.info\']
dict1 = {}.fromkeys(keys, None)
print(dict1)

# {\'name\': None, \'age\': None, \'gender\': None, \'contact.info\': None}

 

分类:

技术点:

相关文章:

  • 2021-10-07
  • 2021-11-22
  • 2021-10-07
  • 2021-10-07
  • 2021-12-10
  • 2021-10-07
  • 2021-10-07
  • 2021-10-07
猜你喜欢
  • 2021-10-07
  • 2021-10-07
  • 2021-10-07
  • 2021-10-07
  • 2021-10-07
  • 2021-10-07
  • 2021-10-07
相关资源
相似解决方案