📜  如何在Python创建字典列表

📅  最后修改于: 2022-05-13 01:54:59.344000             🧑  作者: Mango

如何在Python创建字典列表

在本文中,我们将讨论在Python创建字典列表的方法。

字典列表意味着,字典作为列表中的一个元素存在。

句法:

示例:为学生数据创建字典列表的Python代码



Python3
# create a list of dictionary with student
# id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika',
         7072: 'harsha', 7075: 'deepika'}]
  
# display data
data


Python3
# create a list of dictionary with student 
# id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika', 
         7072: 'harsha', 7075: 'deepika'}]
  
# display data of key 7058
print(data[0][7058])
  
# display data of key 7059
print(data[0][7059])
  
# display data of key 7072
print(data[0][7072])
  
# display data of key 7075
print(data[0][7075])


Python3
# create a list of dictionaries with 
# student id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika', 
         7072: 'harsha', 7075: 'deepika'},
          
        {7051: 'fathima', 7089: 'mounika', 
         7012: 'thanmai', 7115: 'vasavi'},
          
        {9001: 'ojaswi', 1289: 'daksha', 
         7045: 'parvathi', 9815: 'bhavani'}]
  
print(data)


Python3
# create a list of dictionaries with 
# student id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika',
         7072: 'harsha', 7075: 'deepika'},
          
        {7051: 'fathima', 7089: 'mounika', 
         7012: 'thanmai', 7115: 'vasavi'},
          
        {9001: 'ojaswi', 1289: 'daksha', 
         7045: 'parvathi', 9815: 'bhavani'}]
  
# access third dictionary wih key 9001
print(data[2][9001])
  
# access second dictionary wih key 7012
print(data[1][7012])
  
# access second dictionary wih key 7115
print(data[1][7115])


输出:

我们可以使用索引访问

句法:

其中 index 是字典索引,key 是字典键值

示例:通过索引访问键的Python代码

蟒蛇3

# create a list of dictionary with student 
# id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika', 
         7072: 'harsha', 7075: 'deepika'}]
  
# display data of key 7058
print(data[0][7058])
  
# display data of key 7059
print(data[0][7059])
  
# display data of key 7072
print(data[0][7072])
  
# display data of key 7075
print(data[0][7075])

输出:



sravan
jyothika
harsha
deepika

多个词典列表

这与上述方法类似,不同之处在于一次将多个字典传递给列表。

示例:用于创建多个字典列表的Python程序

蟒蛇3

# create a list of dictionaries with 
# student id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika', 
         7072: 'harsha', 7075: 'deepika'},
          
        {7051: 'fathima', 7089: 'mounika', 
         7012: 'thanmai', 7115: 'vasavi'},
          
        {9001: 'ojaswi', 1289: 'daksha', 
         7045: 'parvathi', 9815: 'bhavani'}]
  
print(data)

输出:

它可以再次使用索引访问所有元素。

示例:基于索引和键访问元素的Python代码

蟒蛇3

# create a list of dictionaries with 
# student id as key and name as value
data = [{7058: 'sravan', 7059: 'jyothika',
         7072: 'harsha', 7075: 'deepika'},
          
        {7051: 'fathima', 7089: 'mounika', 
         7012: 'thanmai', 7115: 'vasavi'},
          
        {9001: 'ojaswi', 1289: 'daksha', 
         7045: 'parvathi', 9815: 'bhavani'}]
  
# access third dictionary wih key 9001
print(data[2][9001])
  
# access second dictionary wih key 7012
print(data[1][7012])
  
# access second dictionary wih key 7115
print(data[1][7115])

输出:

ojaswi
thanmai
vasavi