{"id":1028,"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 \u5b57\u5178","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1028.html","title":{"rendered":"Python \u5b57\u5178"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Python \u5b57\u5178<\/h1>\n

\u641e\u61c2Python\u5b57\u5178\u7684\u57fa\u672c\u64cd\u4f5c<\/span>\n <\/div>\n

\u5b57\u5178\u662f\u65e0\u5e8f\u53ef\u53d8\u7684\u5e8f\u5217\uff0c\u5143\u7d20\u4ee5\u201c\u952e\u503c\u5bf9\uff08key-value\uff09\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u3002<\/span>
\n \u5b57\u5178\u76f8\u5bf9\u4e8e\u5217\u8868\uff08list\uff09\u548c\u5143\u7ec4\uff08tuple\uff09\u662f\u6709\u5e8f\u7684\u5e8f\u5217\u3002<\/span>
\n \u5b57\u5178\u4e2d\u7684\u5143\u7d20\u662f\u901a\u8fc7\u952e\u6765\u5b58\u53d6\u7684\uff0c\u5217\u8868\u662f\u901a\u8fc7\u4e0b\u6807\u7d22\u5f15\u5b58\u53d6\u7684\u3002<\/span>
\n \u5b57\u5178\u7528 { } \u6807\u8bc6\uff0c\u662f\u4e00\u4e2a\u952e(key) : \u503c(value) \u7684\u96c6\u5408\uff0ckey\u503c\u5fc5\u987b\u4fdd\u969c\u552f\u4e00\u3002<\/span>
\n \u5b57\u5178\u683c\u5f0f\u4e3a{'key':'value1', 'key2':'value2', ..., 'keyn':valuen}<\/span>
\n \u5b57\u5178\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u4f46\u503c\u4e0d\u9700\u8981\uff0c\u503c\u53ef\u4ee5\u53d6\u4efb\u4f55\u6570\u636e\u7c7b\u578b\u3002<\/span> <\/p>\n

\u521b\u5efa\u5b57\u5178<\/h2>\n
\n
 dict = {}<\/span>
dict['name'] = \"lidihuo\"<\/span>
dict[1] = \"Hello World\"<\/span>
dict1 = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
dict2 = {(1,2,3): 'great', 30: [1,2,3]}<\/span> # \u4f7f\u7528\u5143\u7ec4\u548c\u5217\u8868\u4f5c\u4e3akey\u548cvalue<\/span>
print(dict['name']<\/span>) # \u8f93\u51fa\u952e\u4e3a 'name' \u7684\u503c<\/span>
print(dict[1]<\/span>) # \u8f93\u51fa\u952e\u4e3a1\u7684\u503c<\/span>
print(dict1<\/span>) # \u8f93\u51fa\u5b8c\u6574\u7684\u5b57\u5178<\/span>
print(dict1.keys()<\/span>) # \u8f93\u51fa\u6240\u6709\u952e<\/span>
print(dict1.values()<\/span>) # \u8f93\u51fa\u6240\u6709\u503c<\/span>
print(dict2<\/span>) # \u8f93\u51fa\u5b8c\u6574\u7684\u5b57\u5178<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
lidihuo<\/span> \n
\nHello World<\/span> \n
\n{'name': 'lidihuo', 'course': 'python', 'site': 'www.lidihuo.com'}<\/span> \n
\ndict_keys(['name', 'course', 'site'])<\/span> \n
\ndict_values(['lidihuo', 'python', 'www.lidihuo.com'])<\/span> \n
\n{(1, 2, 3): 'great', 30: [1, 2, 3]}<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n

\u8bbf\u95ee\u5b57\u5178<\/h2>\n
\n dict\u662f\u901a\u8fc7\u952e\u6765\u8bbf\u95ee\u5bf9\u5e94\u7684\u503c\u3002\u56e0\u4e3a\u5b57\u5178\u4e2d\u7684\u5143\u7d20\u662f\u65e0\u5e8f\u7684\uff0c\u6bcf\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\u90fd\u4e0d\u56fa\u5b9a\uff0c\u6240\u4ee5\u5b57\u5178\u4e5f\u4e0d\u80fd\u50cf\u5217\u8868\u548c\u5143\u7ec4\u90a3\u6837\uff0c\u91c7\u7528\u4e0b\u6807\u6765\u8bbf\u95ee\u5143\u7d20\u3002\n <\/div>\n
\n dict\u5143\u7d20\u7684\u5177\u4f53\u683c\u5f0f\u4e3a\uff1adictname[key]\u3002dictname \u8868\u793adict\u53d8\u91cf\u7684\u540d\u5b57\uff0ckey \u8868\u793a\u952e\u540d\u3002\n <\/div>\n
\n
 dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
print(dic['name']<\/span>) # \u8f93\u51fa\u952e\u5bf9\u5e94\u7684\u503c<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
lidihuo<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Python \u4e2d\u66f4\u63a8\u8350\u4f7f\u7528get() \u65b9\u6cd5\u6765\u83b7\u53d6\u6307\u5b9a\u952e\u5bf9\u5e94\u7684\u503c\u3002\u8bed\u6cd5\u683c\u5f0f\u4e3a\uff1adictname.get(key[,default])\uff0c\u5176\u4e2d\uff0cdictname \u8868\u793a\u5b57\u5178\u53d8\u91cf\u7684\u540d\u5b57\uff1bkey \u8868\u793a\u6307\u5b9a\u7684\u952e\uff1b default \u7528\u4e8e\u6307\u5b9a\u8981\u67e5\u8be2\u7684\u952e\u4e0d\u5b58\u5728\u65f6\uff0c\u6b64\u65b9\u6cd5\u8fd4\u56de\u7684\u9ed8\u8ba4\u503c\uff0c\u5982\u679c\u4e0d\u624b\u52a8\u6307\u5b9a\uff0c\u4f1a\u8fd4\u56de None\u3002\n <\/div>\n
\n
 dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
print(dict.get('name')<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
lidihuo<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u4fee\u6539\u5b57\u5178<\/h2>\n
\n \u5b57\u5178\u4fee\u6539\u53ef\u4ee5\u7528\u65b0\u5185\u5bb9\u7684\u8986\u76d6\uff0c\u4fee\u6539\u6216\u5220\u9664\u5df2\u6709\u952e\/\u503c\u5bf9\u64cd\u4f5c\u5982\u4e0b:\n <\/div>\n
\n
 dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
dict['name'] = 'hello'<\/span> # \u66f4\u65b0 name\u503c<\/span>
dict['place'] = 'china'<\/span> # \u6dfb\u52a0\u503c<\/span>
print(dict<\/span>) # \u8f93\u51fa\u952e\u5bf9\u5e94\u7684\u503c<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
lidihuo<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5220\u9664\u5b57\u5178<\/h2>\n
\n \u5b57\u5178\u53ef\u4ee5\u8fdb\u884c\u5355\u4e00\u5143\u7d20\u5220\u9664\uff0c\u4e5f\u80fd\u6e05\u7a7a\u6240\u6709\u6570\u636e\uff0c\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
del <\/span> dict['name'] # \u5220\u9664\u5355\u4e00\u952e<\/span>
print(dict<\/span>) # \u8f93\u51fa\u952e\u5bf9\u5e94\u7684\u503c<\/span>
dict.clear()<\/span> # \u5220\u9664\u5355\u4e00\u952e<\/span>
print(dict<\/span>) # \u8f93\u51fa\u952e\u5bf9\u5e94\u7684\u503c<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
{'course': 'python', 'site': 'www.lidihuo.com'}<\/span> \n
\n{}<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5b57\u5178\u5065\u5224\u65ad<\/h2>\n
\n \u5b57\u5178\u4e2d\u952e\u503c\u5224\u65ad\uff0c\u53ef\u4ee5\u4f7f\u7528 in \u6216 not in \u8fd0\u7b97\u7b26\u3002\u9700\u8981\u6307\u51fa\u7684\u662f\uff0c\u5bf9\u4e8e dict \u800c\u8a00\uff0cin \u6216 not in \u8fd0\u7b97\u7b26\u90fd\u662f\u57fa\u4e8e key \u6765\u5224\u65ad\u7684\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
print('name' in dict<\/span>) # \u7ed3\u679c\u5e94\u4e3aTrue<\/span>
print('hello' in dict<\/span>) # \u7ed3\u679c\u5e94\u4e3aFalse<\/span> <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
True<\/span> \n
\nFalse<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5b57\u5178\u5e38\u7528\u65b9\u6cd5<\/h2>\n
\n \u5b57\u5178\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u6709keys()\u3001values()\u3001items()\u3001copy()\u3001update()\u3001pop()\u3001popitem()\uff0c\u5177\u4f53\u7528\u6cd5\u5982\u4e0b\uff1a\n <\/div>\n
\n
 dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
print(dict.keys()<\/span>) # \u8fd4\u56de\u5b57\u5178\u4e2d\u7684\u6240\u6709\u952e\uff08key\uff09<\/span>
print(dict.values()<\/span>) # \u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u952e\u5bf9\u5e94\u7684\u503c\uff08value\uff09<\/span>
print(dict.items()<\/span>) # \u8fd4\u56de\u5b57\u5178\u4e2d\u6240\u6709\u7684\u952e\u503c\u5bf9\uff08key-value\uff09<\/span>
print(dict.copy()<\/span>) # \u62f7\u8d1d\u4e00\u4e2a\u5177\u6709\u76f8\u540c\u952e\u503c\u5bf9\u7684\u65b0\u5b57\u5178<\/span>
dict.update({'name': 'Jack','hello':'world'})<\/span> # \u5982\u679c\u5df1\u5b58\u5728\u5bf9\u5e94\u7684\u952e\u503c\u5bf9\uff0c\u5219\u8986\u76d6\uff0c\u5426\u5219\u65b0\u6dfb\u52a0<\/span>
print(dict<\/span>)
dict.pop('name')<\/span> # \u5220\u9664\u6307\u5b9a\u7684\u952e\u503c\u5bf9<\/span>
print(dict<\/span>)
dict.popitem()<\/span> # \u968f\u673a\u5220\u9664\u4e00\u4e2a\u952e\u503c\u5bf9<\/span>
print(dict<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
dict_keys(['name', 'course', 'site'])<\/span> \n
\ndict_values(['lidihuo', 'python', 'www.lidihuo.com'])<\/span> \n
\ndict_items([('name', 'lidihuo'), ('course', 'python'), ('site', 'www.lidihuo.com')])<\/span> \n
\n{'name': 'lidihuo', 'course': 'python', 'site': 'www.lidihuo.com'}<\/span> \n
\n{'name': 'Jack', 'course': 'python', 'site': 'www.lidihuo.com', 'hello': 'world'}<\/span> \n
\n{'course': 'python', 'site': 'www.lidihuo.com', 'hello': 'world'}<\/span> \n
\n{'course': 'python', 'site': 'www.lidihuo.com'}<\/span> \n
<\/code><\/pre>\n<\/p><\/div>\n

\u4f7f\u7528\u5b57\u5178\u683c\u5f0f\u5316\u5b57\u7b26\u4e32<\/h2>\n
\n \u7531\u4e8e\u5b57\u5178\u662f\u952e\u503c\u5bf9\u7ed3\u6784\uff0c\u8fd9\u79cd\u7ed3\u6784\u53ef\u4ee5\u5de7\u5999\u7684\u5e94\u7528\u5230\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\u4e2d\uff0c\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 temp<\/span> = '\u6211\u662f%(name)s\u5e73\u53f0, \u672c\u7ae0\u8bb2\u89e3\u7684\u662f%(course)s\u8bfe\u7a0b, \u5177\u4f53\u5185\u5bb9\u53ef\u8bbf\u95ee%(site)s\u7f51\u7ad9\u67e5\u770b'
dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}<\/span>
print(temp % dict<\/span>) <\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u7ed3\u679c\uff1a\n <\/div>\n
\n
\u6211\u662flidihuo\u5e73\u53f0, \u672c\u7ae0\u8bb2\u89e3\u7684\u662fpython\u8bfe\u7a0b, \u5177\u4f53\u5185\u5bb9\u53ef\u8bbf\u95eewww.lidihuo.com\u7f51\u7ad9\u67e5\u770b<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5b57\u5178\u5185\u7f6e\u51fd\u6570<\/h2>\n\n\n\n\n\n\n
\u5e8f\u53f7<\/td>\n\u51fd\u6570\u53ca\u63cf\u8ff0<\/td>\n\u5b9e\u4f8b<\/td>\n<\/tr>\n
1<\/td>\nlen(dict)
\u8ba1\u7b97\u5b57\u5178\u5143\u7d20\u4e2a\u6570\uff0c\u5373\u952e\u7684\u603b\u6570\u3002 <\/td>\n
>>> dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}
>>> len(dict)
3<\/td>\n<\/tr>\n
2<\/td>\nstr(dict)
\u8f93\u51fa\u5b57\u5178\uff0c\u4ee5\u53ef\u6253\u5370\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002<\/td>\n
>>> dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}
>>> str(dict)
\"{'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}\"<\/td>\n<\/tr>\n
3<\/td>\ntype(variable)
\u8fd4\u56de\u8f93\u5165\u7684\u53d8\u91cf\u7c7b\u578b\uff0c\u5982\u679c\u53d8\u91cf\u662f\u5b57\u5178\u5c31\u8fd4\u56de\u5b57\u5178\u7c7b\u578b\u3002<\/td>\n
>>> dict = {'name': 'lidihuo','course':'python', 'site': 'www.lidihuo.com'}
>>> type(dict)
<class 'dict'><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5b57\u5178\u5185\u7f6e\u65b9\u6cd5<\/h2>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\u5e8f\u53f7<\/td>\n\u51fd\u6570\u53ca\u63cf\u8ff0<\/td>\n<\/tr>\n
1<\/td>\nradiansdict.clear()
\u5220\u9664\u5b57\u5178\u5185\u6240\u6709\u5143\u7d20 <\/td>\n<\/tr>\n
2<\/td>\nradiansdict.copy()
\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\u7684\u6d45\u590d\u5236<\/td>\n<\/tr>\n
3<\/td>\nradiansdict.fromkeys()
\u521b\u5efa\u4e00\u4e2a\u65b0\u5b57\u5178\uff0c\u4ee5\u5e8f\u5217seq\u4e2d\u5143\u7d20\u505a\u5b57\u5178\u7684\u952e\uff0cval\u4e3a\u5b57\u5178\u6240\u6709\u952e\u5bf9\u5e94\u7684\u521d\u59cb\u503c<\/td>\n<\/tr>\n
4<\/td>\nradiansdict.get(key, default=None)
\u8fd4\u56de\u6307\u5b9a\u952e\u7684\u503c\uff0c\u5982\u679c\u952e\u4e0d\u5728\u5b57\u5178\u4e2d\u8fd4\u56de default \u8bbe\u7f6e\u7684\u9ed8\u8ba4\u503c<\/td>\n<\/tr>\n
5<\/td>\nkey in dict
\u5982\u679c\u952e\u5728\u5b57\u5178dict\u91cc\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse<\/td>\n<\/tr>\n
6<\/td>\nradiansdict.items()
\u4ee5\u5217\u8868\u8fd4\u56de\u53ef\u904d\u5386\u7684(\u952e, \u503c) \u5143\u7ec4\u6570\u7ec4<\/td>\n<\/tr>\n
7<\/td>\nradiansdict.keys()
\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528 list() \u6765\u8f6c\u6362\u4e3a\u5217\u8868<\/td>\n<\/tr>\n
8<\/td>\nradiansdict.setdefault(key, default=None)
\u548cget()\u7c7b\u4f3c, \u4f46\u5982\u679c\u952e\u4e0d\u5b58\u5728\u4e8e\u5b57\u5178\u4e2d\uff0c\u5c06\u4f1a\u6dfb\u52a0\u952e\u5e76\u5c06\u503c\u8bbe\u4e3adefault<\/td>\n<\/tr>\n
9<\/td>\nradiansdict.update(dict2)
\u628a\u5b57\u5178dict2\u7684\u952e\/\u503c\u5bf9\u66f4\u65b0\u5230dict\u91cc<\/td>\n<\/tr>\n
10<\/td>\nradiansdict.values()
\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u4f7f\u7528 list() \u6765\u8f6c\u6362\u4e3a\u5217\u8868<\/td>\n<\/tr>\n
11<\/td>\npop(key[,default])
\u5220\u9664\u5b57\u5178\u7ed9\u5b9a\u952e key \u6240\u5bf9\u5e94\u7684\u503c\uff0c\u8fd4\u56de\u503c\u4e3a\u88ab\u5220\u9664\u7684\u503c\u3002key\u503c\u5fc5\u987b\u7ed9\u51fa\u3002 \u5426\u5219\uff0c\u8fd4\u56dedefault\u503c\u3002<\/td>\n<\/tr>\n
12<\/td>\n popitem()
\u968f\u673a\u8fd4\u56de\u5e76\u5220\u9664\u5b57\u5178\u4e2d\u7684\u6700\u540e\u4e00\u5bf9\u952e\u548c\u503c\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u5b57\u5178zh-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-1028","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1028"}],"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=1028"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1028\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}