{"id":1066,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u54c8\u5e0c\u8868","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1066.html","title":{"rendered":"Python \u54c8\u5e0c\u8868"},"content":{"rendered":"
\n
Python \u54c8\u5e0c\u8868\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n \u5b57\u5178\u7684\u952e\u662f\u53ef\u54c8\u5e0c\u7684\uff0c\u5373\u7531\u54c8\u5e0c\u51fd\u6570\u751f\u6210\uff0c\u54c8\u5e0c\u51fd\u6570\u4e3a\u63d0\u4f9b\u7ed9\u54c8\u5e0c\u51fd\u6570\u7684\u6bcf\u4e2a\u552f\u4e00\u503c\u751f\u6210\u552f\u4e00\u7ed3\u679c\u3002<\/span> <\/body>
\n
\u56e0\u6b64\uff0c\u968f\u7740\u952e\u503c\u672c\u8eab\u6210\u4e3a\u5b58\u50a8\u6570\u636e\u7684\u6570\u7ec4\u7684\u7d22\u5f15\uff0c\u6570\u636e\u5143\u7d20\u7684\u641c\u7d22\u548c\u63d2\u5165\u529f\u80fd\u53d8\u5f97\u66f4\u5feb\u3002
\n
\u5728Python\u4e2d\uff0cDictionary\u6570\u636e\u7c7b\u578b\u8868\u793a\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u3002\u8bcd\u5178\u4e2d\u7684\u952e\u6ee1\u8db3\u4ee5\u4e0b\u8981\u6c42\u3002\n <\/div>\n
\n \u5b57\u5178\u4e2d\u6570\u636e\u5143\u7d20\u7684\u987a\u5e8f\u4e0d\u56fa\u5b9a\u3002<\/span> <\/p>\n\u5728\u5b57\u5178\u4e2d\u8bbf\u95ee\u503c<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
# Declare a dictionary
<\/span> dict = {'Name'<\/span><\/span>: 'Zara'<\/span>, 'Age'<\/span><\/span>: 7, 'Class'<\/span>: 'First'<\/span>}
# Accessing the dictionary with <\/span>its key
<\/span> print <\/span>\"dict['<\/span><\/span>Name']: \"<\/span><\/span>, dict['Name']
print <\/span>\"dict['Age']: \", dict['Age']
<\/span><\/code><\/pre>\n<\/p><\/div>\n dict['Name'<\/span>]: Zara
dict['Age'<\/span>]: 7
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u66f4\u65b0\u5b57\u5178<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
# Declare a dictionary
<\/span> dict = {'Name'<\/span>: 'Zara'<\/span>, 'Age'<\/span><\/span><\/span>: 7, 'Class'<\/span>: 'First'<\/span>}
dict['Age'] = 8; # update existing entry
<\/span> dict['School'<\/span><\/span>] = \"DPS School\"<\/span>; # Add new entry
<\/span> print <\/span>\"dict['<\/span><\/span>Age']: \"<\/span><\/span>, dict['Age']
print <\/span>\"dict['School']: \", dict['School']
<\/span><\/code><\/pre>\n<\/p><\/div>\n When the above code is <\/span>executed, it produces the following result \u2212
dict['Age'<\/span>]: 8
dict['School'<\/span>]: DPS School
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5220\u9664\u5b57\u5178\u5143\u7d20<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
dict = {'Name'<\/span><\/span><\/span>: 'Zara'<\/span>, 'Age'<\/span><\/span>: 7, 'Class'<\/span>: 'First'<\/span>}
del <\/span>dict['Name']; # remove entry <\/span>with <\/span>key 'Name'
<\/span> dict.clear<\/span>(); # remove all entries in <\/span>dict
<\/span> del <\/span>dict; # delete entire dictionary
<\/span> print <\/span>\"dict['<\/span><\/span>Age']: \"<\/span><\/span>, dict['Age']
print <\/span>\"dict['School'<\/span>]: \", dict['School']
<\/span><\/code><\/pre>\n<\/p><\/div>\n dict['Age'<\/span><\/span>]:
Traceback (most recent call last):
File \"test.py\"<\/span>, line 8, in <\/span>
\n
\n print <\/span> \n \"dict['<\/span>Age \n ']: \"<\/span>, dict['Age']; \n
TypeError: \n 'type'<\/span> object \n is <\/span>unsubscriptable \n
\n <\/module> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u54c8\u5e0c\u8868zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1066","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1066"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1066"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1066\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}