Python – 更新字典列表的值
在本文中,我们将更新字典列表的值。
方法一:使用 append()函数
append函数用于在字典列表中插入一个新值,我们将使用 pop()函数来消除重复数据。
Syntax:
- dictionary[row][‘key’].append(‘value’)
- dictionary[row][‘key’].pop(position)
Where:
- dictionary is the input list of dictionaries
- row is the row that we want to update
- value is the new value to be updated
- key is the column to be updated
- position is the place where the old value is there
用于创建字典列表的Python程序
Python3
# create a list of dictionaries
# with student data
data = [
{'name': 'sravan', 'subjects': ['java', 'python']},
{'name': 'bobby', 'subjects': ['c/cpp', 'java']},
{'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
{'name': 'rohith', 'subjects': ['php', 'os']},
{'name': 'gnanesh', 'subjects': ['html', 'sql']}
]
# display first student
print(data[0])
# display all student
data
Python3
# update first student python subject
# to html
data[0]['subjects'].append('html')
data[0]['subjects'].pop(1)
# update third student java subject
# to dbms
data[2]['subjects'].append('dbms')
data[2]['subjects'].pop(1)
# update forth student php subject
# to php-mysql
data[3]['subjects'].append('php-mysql')
data[3]['subjects'].pop(0)
# display updated list
data
Python3
# update first student python subject
# to html
data[0]['subjects'].insert(0, 'html')
data[0]['subjects'].pop(1)
# update third student java subject
# to dbms
data[2]['subjects'].insert(0, 'dbms')
data[2]['subjects'].pop(1)
# update forth student php subject
# to php-mysql
data[3]['subjects'].insert(1, 'php-mysql')
data[3]['subjects'].pop(0)
# display updated list
data
输出:
{'name': 'sravan', 'subjects': ['java', 'python']}
[{'name': 'sravan', 'subjects': ['java', 'python']},
{'name': 'bobby', 'subjects': ['c/cpp', 'java']},
{'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
{'name': 'rohith', 'subjects': ['php', 'os']},
{'name': 'gnanesh', 'subjects': ['html', 'sql']}]
更新上述字典列表中的值
Python3
# update first student python subject
# to html
data[0]['subjects'].append('html')
data[0]['subjects'].pop(1)
# update third student java subject
# to dbms
data[2]['subjects'].append('dbms')
data[2]['subjects'].pop(1)
# update forth student php subject
# to php-mysql
data[3]['subjects'].append('php-mysql')
data[3]['subjects'].pop(0)
# display updated list
data
输出:
[{'name': 'sravan', 'subjects': ['java', 'html']},
{'name': 'bobby', 'subjects': ['c/cpp', 'java']},
{'name': 'ojsawi', 'subjects': ['iot', 'dbms']},
{'name': 'rohith', 'subjects': ['os', 'php-mysql']},
{'name': 'gnanesh', 'subjects': ['html', 'sql']}]
方法二:使用 insert()函数
该函数用于根据索引插入更新的数据。
Syntax:
- dictionary[row][‘key’].insert(index,’value’)
- dictionary[row][‘key’].pop(position)
Where,
- dictionary is the input list of dictionaries
- row is the row that we want to update
- value is the new value to be updated
- index is the position to be updated
- key is the column to be updated
- position is the place where the old value is there
示例:用于更新字典列表中的值的Python程序
Python3
# update first student python subject
# to html
data[0]['subjects'].insert(0, 'html')
data[0]['subjects'].pop(1)
# update third student java subject
# to dbms
data[2]['subjects'].insert(0, 'dbms')
data[2]['subjects'].pop(1)
# update forth student php subject
# to php-mysql
data[3]['subjects'].insert(1, 'php-mysql')
data[3]['subjects'].pop(0)
# display updated list
data
输出:
[{'name': 'sravan', 'subjects': ['html', 'python']},
{'name': 'bobby', 'subjects': ['c/cpp', 'java']},
{'name': 'ojsawi', 'subjects': ['dbms', 'cloud']},
{'name': 'rohith', 'subjects': ['php-mysql', 'os']},
{'name': 'gnanesh', 'subjects': ['html', 'sql']}]