{"id":1519,"date":"2021-08-10T09:26:19","date_gmt":"2021-08-10T09:26:19","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1519"},"modified":"2021-09-10T09:27:28","modified_gmt":"2021-09-10T09:27:28","slug":"python-list-remove","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2021\/08\/python-list-remove\/","title":{"rendered":"Python\u5bf9\u5217\u8868list\u8fdb\u884cfor\u5faa\u73af\u904d\u5386\u65f6\u4f7f\u7528remove()\u5220\u9664\u5143\u7d20\u7684\u5751"},"content":{"rendered":"

Python\u5bf9\u5217\u8868list\u8fdb\u884cfor\u5faa\u73af\u904d\u5386\u65f6\u4f7f\u7528remove()\u5220\u9664\u5143\u7d20\u7684\u5751.md<\/p>\n

\u73b0\u8c61<\/h2>\n
\n
list_of_char = ['a', 'b', 'b', 'c', 'c', 'd', 'd' ]\n\nfor elem in list_of_char:\n    if elem == 'b' or elem == 'c':\n        list_of_char.remove(elem)\nprint(list_of_char)<\/code><\/pre>\n<\/div>\n

\u5982\u4e0a\uff0c\u8bed\u53e5\uff0c\u6211\u4eec\u671f\u671b\u5f97\u5230\u7684\u662f['a', 'd', 'd']\uff0c\u4f46\u5b9e\u9645\u5f97\u5230\u7ed3\u679c\u5374\u662f['a', 'b', 'c', 'd', 'd']<\/p>\n

\u539f\u56e0\u5206\u6790<\/h2>\n

\u6211\u4eec\u6765\u5206\u6790\u4e0b\u539f\u56e0\uff0c
\n
\n\u7b2c\u4e00\u6b65\uff0ca\u8fdb\u5165\u5faa\u73af\uff0c\u4e0d\u5408\u6761\u4ef6\uff0c\u6ca1\u6709\u6539\u52a8\uff0c\u5f80\u4e0b\u8d70
\n\u7b2c\u4e8c\u6b65\uff0cb\u8fdb\u5165\u5faa\u73af\uff0c\u7b26\u5408\u6761\u4ef6\uff0c\u6267\u884clist_of_char.remove('b') \u6267\u884c\u4ee5\u540e\uff0c\u5217\u8868\u6570\u636e\u53d1\u751f\u53d8\u5316\uff08remove\u65b9\u6cd5\u53ea\u80fd\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\uff0c\u8fd9\u4e2a\u8981\u6ce8\u610f\u4e0b\uff09
\n\u7b2c\u4e09\u6b65\uff0c\u5217\u8868\u6570\u636e\u53d1\u751f\u53d8\u5316\uff0c\u8fdb\u5165\u5faa\u73af\u7684\u662fc\uff0c\u7b26\u5408\u6761\u4ef6\u6267\u884clist_of_char.remove('c') \u6267\u884c\u4ee5\u540e\uff0c\u5217\u8868\u6570\u636e\u53d1\u751f\u53d8\u5316
\n\u7b2c\u56db\u6b65\uff0c\u5217\u8868\u6570\u636e\u53d1\u751f\u53d8\u5316\uff0c\u8fdb\u5165\u5faa\u73af\u7684\u662fd\uff0c\u4e0d\u548c\u6761\u4ef6\u6ca1\u6709\u6539\u52a8\uff0c\u5f80\u4e0b\u8d70
\n\u7b2c\u4e94\u6b65\uff0c\u8fdb\u5165\u5faa\u73af\u7684\u662fd\uff0c\u4e0d\u548c\u6761\u4ef6\u6ca1\u6709\u6539\u52a8\uff0c\u5faa\u73af\u7ed3\u675f<\/p>\n

\u5982\u4f55\u89c4\u907f<\/h2>\n

\u65b9\u6cd5\u4e00\uff1a\u590d\u5236\u4e2a\u65b0\u7684\u5217\u8868\u51fa\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528reversed()\u6216\u8005list()<\/p>\n

\n
for elem in reversed(list_of_char):\n    if elem == 'b' or elem == 'c':\n        list_of_char.remove(elem)\n\nfor elem in list(list_of_char):\n    if elem == 'b' or elem == 'c':\n        list_of_char.remove(elem)<\/code><\/pre>\n<\/div>\n

\u65b9\u6cd5\u4e8c\uff1a\u5217\u8868\u63a8\u5bfc\u5f0f<\/p>\n

\n
list_of_char[:] = [x for x in list_of_char if x !='b' and x != 'c']<\/code><\/pre>\n<\/div>\n

\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528filter()\u65b9\u6cd5<\/p>\n

\n
list_of_char = list(filter(lambda elem: elem != 'b' and elem !='c',list_of_char))<\/code><\/pre>\n<\/div>\n

\u53c2\u8003\u8d44\u6599<\/h2>\n

https:\/\/blog.finxter.com\/how-to-remove-items-from-a-list-while-iterating\/<\/a>
\n
https:\/\/thispointer.com\/python-remove-elements-from-a-list-while-iterating\/<\/a><\/p>\n

\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Python\u5bf9\u5217\u8868list\u8fdb\u884cfor\u5faa\u73af\u904d\u5386\u65f6\u4f7f\u7528remove()\u5220\u9664\u5143\u7d20\u7684\u5751<\/a><\/p>","protected":false},"excerpt":{"rendered":"

Python\u5bf9\u5217\u8868list\u8fdb\u884cfor\u5faa\u73af\u904d\u5386\u65f6\u4f7f\u7528remove()\u5220\u9664\u5143\u7d20\u7684\u5751.md \u73b0\u8c61 list_of_char = [‘a’, ‘b’, ‘b’, ‘c’, ‘c’, ‘d’, ‘d’ ] for elem in list_of_char: if elem == ‘b’ or elem == ‘c’: list_of_char.remove(elem) print(list_of_char) \u5982\u4e0a\uff0c\u8bed\u53e5\uff0c\u6211\u4eec\u671f\u671b\u5f97\u5230\u7684\u662f[‘a’, ‘d’, ‘d’]\uff0c\u4f46\u5b9e\u9645\u5f97\u5230\u7ed3\u679c\u5374\u662f[‘a’, ‘b’, ‘c’, ‘d’, ‘d’] \u539f\u56e0\u5206\u6790 \u6211\u4eec\u6765\u5206\u6790\u4e0b\u539f\u56e0\uff0c \u7b2c\u4e00\u6b65\uff0ca\u8fdb\u5165\u5faa\u73af\uff0c\u4e0d\u5408\u6761\u4ef6\uff0c\u6ca1\u6709\u6539\u52a8\uff0c\u5f80\u4e0b\u8d70 \u7b2c\u4e8c\u6b65\uff0cb\u8fdb\u5165\u5faa\u73af\uff0c\u7b26\u5408\u6761\u4ef6\uff0c\u6267\u884clist_of_char.remove(‘b’) \u6267\u884c\u4ee5\u540e\uff0c\u5217\u8868\u6570\u636e\u53d1\u751f\u53d8\u5316\uff08remove\u65b9\u6cd5\u53ea\u80fd\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u5143\u7d20\uff0c\u8fd9\u4e2a\u8981\u6ce8\u610f\u4e0b\uff09 \u7b2c\u4e09\u6b65\uff0c\u5217\u8868\u6570\u636e\u53d1\u751f\u53d8\u5316\uff0c\u8fdb\u5165\u5faa\u73af\u7684\u662fc\uff0c\u7b26\u5408\u6761\u4ef6\u6267\u884clist_of_char.remove(‘c’) \u6267\u884c\u4ee5\u540e\uff0c\u5217\u8868\u6570\u636e\u53d1\u751f\u53d8\u5316 \u7b2c\u56db\u6b65\uff0c\u5217\u8868\u6570\u636e\u53d1\u751f\u53d8\u5316\uff0c\u8fdb\u5165\u5faa\u73af\u7684\u662fd\uff0c\u4e0d\u548c\u6761\u4ef6\u6ca1\u6709\u6539\u52a8\uff0c\u5f80\u4e0b\u8d70 \u7b2c\u4e94\u6b65\uff0c\u8fdb\u5165\u5faa\u73af\u7684\u662fd\uff0c\u4e0d\u548c\u6761\u4ef6\u6ca1\u6709\u6539\u52a8\uff0c\u5faa\u73af\u7ed3\u675f \u5982\u4f55\u89c4\u907f \u65b9\u6cd5\u4e00\uff1a\u590d\u5236\u4e2a\u65b0\u7684\u5217\u8868\u51fa\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528reversed()\u6216\u8005list() for elem in reversed(list_of_char): if elem == ‘b’ or elem == […]<\/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\/1519"}],"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=1519"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1519\/revisions"}],"predecessor-version":[{"id":1520,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1519\/revisions\/1520"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}