{"id":1643,"date":"2020-07-31T02:19:19","date_gmt":"2020-07-31T02:19:19","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1643"},"modified":"2022-07-31T02:19:47","modified_gmt":"2022-07-31T02:19:47","slug":"python-list-dict","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2020\/07\/python-list-dict\/","title":{"rendered":"Python\u6570\u7ec4\u7684list\u5217\u8868\u548cdict\u5b57\u5178\u7684\u904d\u5386"},"content":{"rendered":"

List\u5217\u8868\u904d\u5386<\/h2>\n
\n
a = ['a', 'b', 'c', 'd', 'e']\n\n# simple iterate\nfor i in a:\n    print(i)\n\n# iterate with index\nfor i, el in enumerate(a):\n    print(i, el)\n\n# iterate with custom index\nfor i, el in enumerate(a, 1):\n    print(i, el)<\/code><\/pre>\n<\/div>\n

\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b<\/p>\n

\n
a\nb\nc\nd\ne\n0 a\n1 b\n2 c\n3 d\n4 e\n1 a\n2 b\n3 c\n4 d\n5 e<\/code><\/pre>\n<\/div>\n

\u6ce8\u610f\uff1aenumerate() \u51fd\u6570\u7528\u4e8e\u5c06\u4e00\u4e2a\u53ef\u904d\u5386\u7684\u6570\u636e\u5bf9\u8c61(\u5982\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32)\u7ec4\u5408\u4e3a\u4e00\u4e2a\u7d22\u5f15\u5e8f\u5217\uff0c\u540c\u65f6\u5217\u51fa\u6570\u636e\u548c\u6570\u636e\u4e0b\u6807\uff0c\u4e00\u822c\u7528\u5728 for \u5faa\u73af\u5f53\u4e2d\u3002<\/p>\n

Dict\u5b57\u5178\u904d\u5386<\/h2>\n

dict\u6700\u7b80\u5355\u7684\u904d\u5386\u65b9\u5f0f<\/p>\n

\n
d = {'a': 1, 'c': 3, 'b': 2, 'd': 4}\n\nfor k in d:\n    print(k)\n\nfor k in d:\n    print(k, d[k])<\/code><\/pre>\n<\/div>\n

\u8fd0\u884c\u7ed3\u679c<\/p>\n

\n
a\nc\nb\nd\na 1\nc 3\nb 2\nd 4<\/code><\/pre>\n<\/div>\n

\u662f\u4e0d\u662f\u6709\u70b9\u4f4e\u7ea7\uff1f\u5176\u5b9e\u4eba\u5bb6\u63d0\u4f9b\u4e86items()\u3001keys()\u548cvalues() \u65b9\u6cd5\uff0c\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b<\/p>\n

\n
for k, v in d.items():\n    print(k, v)\n\nfor v in d.values():\n    print(v)\n\nfor k in d.keys():\n    print(k)<\/code><\/pre>\n<\/div>\n

\u6211\u4eec\u6765\u8bd5\u4e00\u4e0benumerate() \u51fd\u6570<\/p>\n

\n
d = {'a': 1, 'c': 3, 'b': 2, 'd': 4}\nfor key, value in enumerate(d):\n    print(key, value)<\/code><\/pre>\n<\/div>\n

\u7ed3\u679c\u5982\u4e0b<\/p>\n

\n
0 a\n1 c\n2 b\n3 d<\/code><\/pre>\n<\/div>\n

\u989d\uff0c\u5e76\u6ca1\u6709\u8fbe\u6210\u6211\u4eec\u7684\u9884\u671f\u3002\u9700\u8981\u6539\u4e00\u4e0b<\/p>\n

\n
d={'a': 1, 'c': 3, 'b': 2, 'd': 4}\n# Iterate over all key-value pairs of dictionary by index\nforindex, (key, value) inenumerate(d.items()):\n    print('Index:: ', index, ' :: ', key, '-', value)<\/code><\/pre>\n<\/div>\n

\u7ed3\u679c\u5982\u4e0b<\/p>\n

\n
Index::  0  ::  a - 1\nIndex::  1  ::  c - 3\nIndex::  2  ::  b - 2\nIndex::  3  ::  d - 4<\/code><\/pre>\n<\/div>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Python\u6570\u7ec4\u7684list\u5217\u8868\u548cdict\u5b57\u5178\u7684\u904d\u5386<\/a><\/p>","protected":false},"excerpt":{"rendered":"

List\u5217\u8868\u904d\u5386 a = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] # simple iterate for i in a: print(i) # iterate with index for i, el in enumerate(a): print(i, el) # iterate with custom index for i, el in enumerate(a, 1): print(i, el) \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b a b c d e 0 a 1 b 2 c 3 d 4 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[59],"_links":{"self":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1643"}],"collection":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/comments?post=1643"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1643\/revisions"}],"predecessor-version":[{"id":1644,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1643\/revisions\/1644"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}