{"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 <\/head>
\n <\/p>\n

\n

Python \u54c8\u5e0c\u8868<\/h1>\n

Python \u54c8\u5e0c\u8868\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u54c8\u5e0c\u8868\u662f\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5176\u4e2d\u6570\u636e\u5143\u7d20\u7684\u5730\u5740\u6216\u7d22\u5f15\u503c\u662f\u4ece\u54c8\u5e0c\u51fd\u6570\u751f\u6210\u7684\u3002\u7531\u4e8e\u7d22\u5f15\u503c\u5145\u5f53\u6570\u636e\u503c\u7684\u952e\uff0c\u56e0\u6b64\u53ef\u4ee5\u66f4\u5feb\u5730\u8bbf\u95ee\u6570\u636e\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u54c8\u5e0c\u8868\u5b58\u50a8\u952e\u503c\u5bf9\uff0c\u4f46\u662f\u952e\u662f\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u751f\u6210\u7684\u3002
\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

\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>
\n \u5b57\u5178\u4e2d\u6570\u636e\u5143\u7d20\u7684\u987a\u5e8f\u4e0d\u56fa\u5b9a\u3002<\/span> <\/p>\n

\n \u8981\u8bbf\u95ee\u5b57\u5178\u5143\u7d20\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u719f\u6089\u7684\u65b9\u62ec\u53f7\u548c\u952e\u6765\u83b7\u53d6\u5176\u503c\u3002\n <\/div>\n

\u5728\u5b57\u5178\u4e2d\u8bbf\u95ee\u503c<\/h2>\n
\n \u8981\u8bbf\u95ee\u5b57\u5178\u5143\u7d20\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u719f\u6089\u7684\u65b9\u62ec\u53f7\u548c\u952e\u6765\u83b7\u53d6\u5176\u503c\u3002\n <\/div>\n
\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
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 dict['Name'<\/span>]: Zara
dict['Age'<\/span>]: 7
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u66f4\u65b0\u5b57\u5178<\/h2>\n
\n \u60a8\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u65b0\u6761\u76ee\u6216\u952e\u503c\u5bf9\uff0c\u4fee\u6539\u73b0\u6709\u6761\u76ee\u6216\u5220\u9664\u73b0\u6709\u6761\u76ee\u6765\u66f4\u65b0\u5b57\u5178\uff0c\u5982\u4e0b\u9762\u7684\u7b80\u5355\u793a\u4f8b\u6240\u793a\uff1a\n <\/div>\n
\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
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\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
\n \u60a8\u53ef\u4ee5\u5220\u9664\u5355\u4e2a\u8bcd\u5178\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u6e05\u9664\u8bcd\u5178\u7684\u5168\u90e8\u5185\u5bb9\u3002\u60a8\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5355\u4e2a\u64cd\u4f5c\u5220\u9664\u6574\u4e2a\u8bcd\u5178\u3002\u8981\u663e\u5f0f\u5220\u9664\u6574\u4e2a\u5b57\u5178\uff0c\u53ea\u9700\u4f7f\u7528del\u8bed\u53e5\u3002-\n <\/div>\n
\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
\n \u8fd9\u5c06\u4ea7\u751f\u4ee5\u4e0b\u7ed3\u679c\u3002\u8bf7\u6ce8\u610f\uff0c\u5f15\u53d1\u5f02\u5e38\u662f\u56e0\u4e3a\u5728del dict\u5b57\u5178\u4e4b\u540e\u4e0d\u518d\u5b58\u5728-\n <\/div>\n
\n
 dict['Age'<\/span><\/span>]:
Traceback (most recent call last):
   File \"test.py\"<\/span>, line 8, in <\/span>
     \n \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

<\/body>
\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}]}}