{"id":1511,"date":"2021-07-21T05:58:59","date_gmt":"2021-07-21T05:58:59","guid":{"rendered":"https:\/\/www.ipcpu.com\/?p=1511"},"modified":"2021-08-30T05:59:38","modified_gmt":"2021-08-30T05:59:38","slug":"python%e7%9a%84list%e5%88%97%e8%a1%a8%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/c.ipcpu.com\/2021\/07\/python%e7%9a%84list%e5%88%97%e8%a1%a8%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Python\u7684list\u5217\u8868\u8be6\u89e3"},"content":{"rendered":"

Python list(\u5217\u8868)\uff0c\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u662f\u4e00\u7ec4\u7528\u65b9\u62ec\u53f7\u62ec\u8d77\u6765\u3001\u9017\u53f7\u5206\u9694\u7684\u6570\u636e\u3002<\/p>\n

\u5217\u8868\u7684\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684\uff0c\u4f46\u4f7f\u7528\u65f6\u901a\u5e38\u5404\u4e2a\u5143\u7d20\u7684\u7c7b\u578b\u662f\u76f8\u540c\u7684\u3002<\/p>\n

\u5217\u8868\u652f\u6301\u52a0\u5165\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5143\u7d20\uff1a\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u7b49\u3002<\/p>\n

\u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u5373\u4f7f\u5728\u521b\u5efa\u540e\u4e5f\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u66f4\u6539\u3002<\/p>\n

Python\u4e2d\u7684\u5217\u8868\u662f\u6709\u5e8f\u7684\uff0c\u5e76\u4e14\u6709\u4e00\u5b9a\u6570\u91cf\u3002 \u6839\u636e\u786e\u5b9a\u7684\u5e8f\u5217\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d22\u5f15\uff0c\u5e76\u4f7f\u75280\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u7d22\u5f15\u6765\u5b8c\u6210\u5217\u8868\u7684\u7d22\u5f15\u3002<\/p>\n

\u4e00\u3001\u521b\u5efa\u5217\u8868Creating a List<\/h4>\n
\n
list1 = ['Google', 'Runoob', 1997, 2000]\nlist2 = [1, 2, 3, 4, 5 ]\nmy_list = ['p','r','o','b','e']\nkong = [] #\u521b\u5efa\u7a7a\u767dlist\uff0c\u7528\u4e8e\u540e\u9762\u8c03\u7528<\/code><\/pre>\n<\/div>\n

\u4e8c\u3001\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20Accessing elements from the List<\/h4>\n

\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u8fd0\u7b97\u7b26[]\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u9879\u3002\u7d22\u5f15\u5fc5\u987b\u4e3a\u6574\u6570\u3002\u5d4c\u5957\u5217\u8868\u901a\u8fc7\u5d4c\u5957\u7d22\u5f15\u8bbf\u95ee\u3002<\/p>\n

\u7d22\u5f15\u7684\u6392\u5e8f\u89c4\u5219\u5982\u4e0b\u56fe\uff0c\u4ece\u96f6\u5f00\u59cb\u3002<\/p>\n

<\/p>\n

\n
List = [\"Geeks\", \"For\", \"Geeks\"] \nprint(List[0])  <\/code><\/pre>\n<\/div>\n

\u5728\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u4f7f\u7528\u7684\u7d22\u5f15(\u4e0b\u6807)\u4e0d\u53ef\u8d8a\u754c\uff0c\u5426\u5219\u4f1a\u62a5\u5982\u4e0b\u9519\u8bef
\nIndexError: list index out of range<\/p>\n

\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u4f1a\u5f15\u8d77\u8d8a\u754c\uff1a<\/p>\n

    \n
  1. \u521b\u5efa\u7a7a\u7684list\uff0c\u4f46\u4f7f\u7528list[0] \u6765\u8bbf\u95ee\u5176\u4e2d\u5143\u7d20<\/li>\n
  2. \u8bbf\u95eelist[n] \uff0c\u4f46\u662fN\u5927\u4e8elen(list)<\/li>\n
  3. \u521b\u5efa\u7a7a\u7684list\uff0c\u901a\u8fc7list[0]\u6765\u8d4b\u503c<\/li>\n<\/ol>\n

    \u4e09\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5143\u7d20Adding or Removing Elements<\/h4>\n

    \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684append()\u51fd\u6570\u5c06\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002
    \n\u901a\u8fc7\u4f7f\u7528append()\u65b9\u6cd5\uff0c\u4e00\u6b21\u53ea\u80fd\u5c06\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\uff0c\u5982\u679c\u4f7f\u7528append()\u65b9\u6cd5\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5faa\u73af\u3002<\/p>\n

    \n
    List = [] \nprint(List) \n\n# Addition of Elements  \nList.append(1) \nList.append(2) \nprint(List) \n\n\n# Adding elements to the List using Iterator \nfor i in range(1, 4): \n    List.append(i) \nprint(List) <\/code><\/pre>\n<\/div>\n

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

    \n
    []\n[1, 2]\n[1, 2, 1, 2, 3]<\/code><\/pre>\n<\/div>\n

    append()\u65b9\u6cd5\u4ec5\u9002\u7528\u4e8eList\u672b\u5c3e\u7684\u5143\u7d20\u6dfb\u52a0\uff0c\u5982\u679c\u9700\u8981\u5728\u4e2d\u95f4\u4f4d\u7f6e\u63d2\u5165\u5143\u7d20\uff0c\u5219\u4f7f\u7528insert()\u65b9\u6cd5\u3002
    \ninsert()\u65b9\u6cd5\u9700\u8981\u4e24\u4e2a\u53c2\u6570\uff0c\u4f4d\u7f6e\u548c\u503c\u3002 insert(position, value)<\/p>\n

    \n
    List = [1,2,3,4]\nprint(List)\n\nList.insert(3, 12)\nList.insert(0, 'Geeks')\nprint(List)<\/code><\/pre>\n<\/div>\n

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

    \n
    [1, 2, 3, 4]\n['Geeks', 1, 2, 3, 12, 4]<\/code><\/pre>\n<\/div>\n

    \u9664\u4e86append()\u548cinsert()\u65b9\u6cd5\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u5143\u7d20\u6dfb\u52a0\u65b9\u6cd5extend()\uff0c\u8be5\u65b9\u6cd5\u7528\u4e8e\u5728\u5217\u8868\u672b\u5c3e\u540c\u65f6\u6dfb\u52a0\u591a\u4e2a\u5143\u7d20\u3002<\/p>\n

    \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684remove()\u51fd\u6570\u4ece\u5217\u8868\u4e2d\u5220\u9664\u5143\u7d20\uff0c\u4f46\u662f\u5982\u679c\u5143\u7d20\u4e0d\u5b58\u5728\u4e8e\u96c6\u5408\u4e2d\uff0c\u5219\u4f1a\u53d1\u751f\u9519\u8bef\u3002
    \nRemove()\u65b9\u6cd5\u4e00\u6b21\u53ea\u5220\u9664\u4e00\u4e2a\u5143\u7d20\uff0c\u5982\u9700\u5220\u9664\u591a\u4e2a\u9700\u8981\u4f7f\u7528\u5faa\u73af\u8bed\u53e5\u3002
    \n\u6ce8\u610f\uff1aremove(value)\u65b9\u6cd5\u7684\u53c2\u6570\u662f\u503c\uff0c\u4e0d\u662f\u7d22\u5f15(index\uff0c\u4e0b\u6807)\uff0c\u6b64\u65b9\u6cd5\u5c06\u4ec5\u5220\u9664\u641c\u7d22\u5230\u7684\u5143\u7d20\u7684\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\u3002<\/p>\n

    \n
    List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nprint(List)\n\nList.remove(5)\nList.remove(6)\nprint(List)<\/code><\/pre>\n<\/div>\n

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

    \n
    List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nprint(List)\n\nList.remove(5)\nList.remove(6)\nprint(List)<\/code><\/pre>\n<\/div>\n

    \u56db\u3001\u5217\u8868\u5408\u5e76Merge lists<\/h2>\n

    \u524d\u9762\u6211\u4eec\u8bf4\u8fc7\uff0c\u4f7f\u7528append()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u4e00\u4e2a\u65b0\u5143\u7d20\uff0c\u6240\u4ee5\u5bf9\u4e8e\u4e24\u4e2a\u6216\u8005\u591a\u4e2a\u5217\u8868\u7684\u5408\u5e76\u4e0d\u662f\u5f88\u9002\u7528\u3002<\/p>\n

    \n
    list1 = ['Google', 'Runoob', 1997, 2000]\nlist2 = [1, 2, 3, 4, 5 ]\nlist3 = [{'name': 'Soe', 'Sex': 'Female'}, {'name': 'John', 'Sex': 'Male'}]\n\nnewlist = list1.append(list2)\nprint(newlist)<\/code><\/pre>\n<\/div>\n

    \u5927\u5bb6\u731c\u731c\u8fd4\u56de\u4ec0\u4e48\u7ed3\u679c\uff1f
    \nNone\u3002\u53ef\u89c1list\u7684\u8bb8\u591a\u65b9\u6cd5\uff0c\u90fd\u662f\u5e94\u7528\u5728list\u672c\u8eab\u7684\uff0c\u4e0d\u80fd\u8d4b\u503c\u5230\u65b0\u7684list\uff0c\u8fd9\u4e00\u70b9\u8981\u6ce8\u610f\u3002<\/p>\n

    \n
    list1.append(list2)\n# Result: ['Google', 'Runoob', 1997, 2000, [1, 2, 3, 4, 5]]\nlist1.extend(list2)\n# Result: ['Google', 'Runoob', 1997, 2000, 1, 2, 3, 4, 5]<\/code><\/pre>\n<\/div>\n

    \u4e0a\u9762\u8fd9\u4e9b\u53ea\u80fd\u5408\u5e76\u4e24\u4e2alist\uff0c\u4e5f\u4e0d\u65b9\u4fbf\uff0c\u4e0b\u9762\u624d\u662f\u771f\u6b63\u5e38\u7528\u7684\u65b9\u6cd5<\/p>\n

    \n
    newlist = list1 + list2 + list3\nprint(newlist)\n# Result: ['Google', 'Runoob', 1997, 2000, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, {'name': 'Soe', 'Sex': 'Female'}, {'name': 'John', 'Sex': 'Male'}]\n\nnewlist = [*list1, *list2, *list3]\nprint(newlist)\n# Result: ['Google', 'Runoob', 1997, 2000, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, {'name': 'Soe', 'Sex': 'Female'}, {'name': 'John', 'Sex': 'Male'}]\n<\/code><\/pre>\n<\/div>\n

    \u4e94\u3001\u5217\u8868List\u7684\u904d\u5386\u4ee5\u53ca\u5224\u65ad\u5143\u7d20\u662f\u5426\u5728\u5217\u8868List\u4e2d<\/h2>\n

    \u901a\u8fc7for\u5faa\u73af\u53ef\u4ee5\u5b9e\u73b0\u5217\u8868List\u7684\u904d\u5386<\/p>\n

    \n
    a = ['a', 'b', 'c', 'd', 'e']\n\n# simple iterate \u7b80\u5355\u904d\u5386\nfor i in a:\n    print(i)\n\n# iterate with index \u5e26\u7d22\u5f15\u7684\u904d\u5386\nfor i, el in enumerate(a):\n    print(i, el)\n\n# iterate with custom index \u81ea\u5b9a\u4e49\u7d22\u5f15\u53f7\u5417\u7684\u904d\u5386\nfor i, el in enumerate(a, 1):\n    print(i, el)<\/code><\/pre>\n<\/div>\n

    \u901a\u8fc7in\u65b9\u6cd5\u53ef\u4ee5\u5224\u65ad\u5143\u7d20\u662f\u5426\u5728\u5217\u8868\u4e2d<\/p>\n

    \n
    list1 = ['Google', 'Runoob', 1997, 2000]\nif 'Google' in list1:\n    print(True)\n# Result: True<\/code><\/pre>\n<\/div>\n

    \u516d\u3001\u5217\u8868\u6392\u5e8f\u548c\u53bb\u91cd\u53bb\u7a7aSort ,Remove duplicates, Remove NA<\/h2>\n

    \u901a\u8fc7sort\u65b9\u6cd5\u53ef\u4ee5\u8fdb\u884c\u6392\u5e8f<\/p>\n

    \n
    list1 = ['Google', 'Yahoo', 'Baidu', 'Sogou']\nlist1.sort(reverse=True)\nprint(list1)\n# Result:  ['Yahoo', 'Sogou', 'Google', 'Baidu']<\/code><\/pre>\n<\/div>\n

    sort\u51fd\u6570\u8fd8\u6709\u4e00\u4e2a\u544a\u8b66\u7528\u6cd5sort(key=Function)\uff0c\u6211\u4eec\u5728\u540e\u7eed\u5728\u63a2\u8ba8\u3002
    \n\u4f7f\u7528\u5148set\u540elist\u7684\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u5217\u8868list\u5143\u7d20\u8fdb\u884c\u53bb\u91cd<\/p>\n

    \n
    my_list = ['Google', 'Yahoo', 'Baidu', 'Sogou', 'Google']\nnewlist = list(set(my_list))\nprint(newlist)\n# Result:  ['Baidu', 'Yahoo', 'Google', 'Sogou']<\/code><\/pre>\n<\/div>\n

    set()\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\uff0c\u96c6\u5408\uff08set\uff09\u662f\u4e00\u4e2a\u65e0\u5e8f\u7684\u4e0d\u91cd\u590d\u5143\u7d20\u5e8f\u5217\u3002\u521b\u5efa\u5b8c\u6210\u96c6\u5408set\u4ee5\u540e\uff0c\u5728\u8f6c\u6362\u56de\u6765\u5c31\u8fd8\u662f\u4e00\u4e2a\u5217\u8868list\u3002
    \n\u9664\u4e86\u53bb\u91cd\uff0c\u8fd8\u6709\u7ecf\u5e38\u9047\u5230\u53bb\u9664\u7a7a\u503c\u7684\u60c5\u51b5<\/p>\n

    \n
    d = ['', '\u5267\u60c5', '\u559c\u5267', '\u6050\u6016', '', '\u4f26\u7406', '']\nd_dropna = list(filter(None, d))    #\u53bb\u9664\u5217\u8868\u7a7a\u503c\nprint(d_dropna)\n# Result:  ['\u5267\u60c5', '\u559c\u5267', '\u6050\u6016', '\u4f26\u7406']<\/code><\/pre>\n<\/div>\n

    \u4f7f\u7528filter\u5c31\u53ef\u4ee5\u8fc7\u6ee4\u6389\u7a7a\u503c\uff0c\u7136\u540e\u5728\u4f7f\u7528list\u5c06\u5176\u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u8868\u3002<\/p>\n

    \u4e03\u3001\u5217\u8868\u5207\u7247Slicing of a List<\/h2>\n

    \u5728Python\u5217\u8868\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u6765\u6253\u5370\u5305\u542b\u6240\u6709\u5143\u7d20\u7684\u6574\u4e2a\u5217\u8868\uff0c\u4f46\u662f\u8981\u6253\u5370\u5217\u8868\u4e2d\u7279\u5b9a\u8303\u56f4\u7684\u5143\u7d20\uff0c\u6211\u4eec\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u3002 <\/p>\n

    \u6ce8\u610f\uff1a\u9664\u4e86\u5217\u8868List\uff0c\u5143\u7ec4Tuple\uff0c\u5b57\u7b26\u4e32string \u4e5f\u53ef\u4ee5\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u3002<\/p>\n

    \u5207\u7247\u64cd\u4f5c\u7684\u683c\u5f0f\u5982\u4e0b<\/p>\n

    \n
    list[start_index: end_index: step]<\/code><\/pre>\n<\/div>\n
    \n

    start\uff1a\u8868\u793a\u662f\u7b2c\u4e00\u4e2a\u5143\u7d20\u5bf9\u8c61\uff0c\u6b63\u7d22\u5f15\u4f4d\u7f6e\u9ed8\u8ba4\u4e3a0\uff1b\u8d1f\u7d22\u5f15\u4f4d\u7f6e\u9ed8\u8ba4\u4e3a -\u5e8f\u5217\u957f\u5ea6
    \n end\uff1a\u8868\u793a\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5bf9\u8c61\uff0c\u6b63\u7d22\u5f15\u4f4d\u7f6e\u9ed8\u8ba4\u4e3a (\u5e8f\u5217\u957f\u5ea6\uff0d1)\uff1b\u8d1f\u7d22\u5f15\u4f4d\u7f6e\u9ed8\u8ba4\u4e3a -1
    \n step\uff1a\u6b65\u957f\uff0cend-start\uff0c\u6b65\u957f\u4e3a\u6b63\u65f6\uff0c\u4ece\u5de6\u5411\u53f3\u53d6\u503c\u3002\u6b65\u957f\u4e3a\u8d1f\u65f6\uff0c\u53cd\u5411\u53d6\u503c\uff0c\u9ed8\u8ba4\u4e3a1\uff0c\u6b65\u957f\u503c\u4e0d\u80fd\u4e3a0<\/p>\n<\/blockquote>\n

    \n
    List = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nprint(List[0:])\nprint(List[:])\nprint(List[2:])\nprint(List[2:4])\nprint(List[2:100])\n\nprint('============================')\nprint(List[:-1])\nprint(List[::-1])   \nprint(List[::-2])<\/code><\/pre>\n<\/div>\n

    \u8fd9\u91cc\u7684[start_index:end_index] \u662f\u4e00\u4e2a\u524d\u5f00\u540e\u95ed\u7684\u533a\u95f4\u53d6\u503c\uff0c\u4f8b\u5982[2:4]\u5b9e\u9645\u6570\u5b66\u8868\u793a\u662f[2:4)\uff0c\u4e5f\u5c31\u662f\u83b7\u53d6\u7b2c3\u4e2a\u548c\u7b2c4\u4e2a\u503c\u3002
    \n\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b<\/p>\n

    \n
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n[3, 4, 5, 6, 7, 8, 9, 10]\n[3, 4]\n[3, 4, 5, 6, 7, 8, 9, 10]\n============================\n[1, 2, 3, 4, 5, 6, 7, 8, 9]\n[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n[10, 8, 6, 4, 2]<\/code><\/pre>\n<\/div>\n

    \u6ce8\u610f\uff1a\u5207\u7247\u64cd\u4f5c\u4e0d\u4f1a\u51fa\u73b0\u7d22\u5f15(\u4e0b\u6807)\u8d8a\u754c\u7684\u60c5\u51b5\uff0cPython\u5185\u90e8\u6709\u5904\u7406\u673a\u5236\u81ea\u52a8\u8f6c\u6362\u4e86\uff0c\u8fd9\u91cc\u548c\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u5217\u8868list\u662f\u4e0d\u4e00\u6837\u7684\u3002<\/p>\n

    \u516b\u3001\u5217\u8868\u7684\u5e38\u7528\u65b9\u6cd5\u603b\u7ed3<\/h2>\n
    \n

    \u7528append()\u65b9\u6cd5\uff0c\u5728\u5217\u8868\u5c3e\u90e8\u6dfb\u52a0\u5355\u4e2a\u65b0\u5143\u7d20\u3002
    \n \u7528insert()\u65b9\u6cd5\uff0c\u5728\u5217\u8868\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u5143\u7d20\u3002
    \n \u7528 \u201c+\u201d \u8fd0\u7b97\u7b26\uff0c\u5c06\u4e24\u4e2a\u5217\u8868\u62fc\u63a5\u51fa\u4e00\u4e2a\u65b0\u5217\u8868\u3002
    \n \u7528extend()\u65b9\u6cd5\uff0c\u5728\u4e00\u4e2a\u5217\u8868\u540e\u9762\u62fc\u63a5\u8fdb\u53e6\u4e00\u4e2a\u5217\u8868\u3002
    \n \u7528del list[m] \u8bed\u53e5\uff0c\u5220\u9664\u6307\u5b9a\u7d22\u5f15m\u5904\u7684\u5143\u7d20\u3002
    \n \u7528remove()\u65b9\u6cd5\uff0c\u5220\u9664\u6307\u5b9a\u503c\u7684\u5143\u7d20\uff08\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\uff09\u3002
    \n \u7528pop()\u65b9\u6cd5\uff0c\u53d6\u51fa\u5e76\u5220\u9664\u5217\u8868\u672b\u5c3e\u7684\u5355\u4e2a\u5143\u7d20\u3002
    \n \u7528pop(m)\u65b9\u6cd5\uff0c\u53d6\u51fa\u5e76\u5220\u9664\u7d22\u5f15\u503c\u4e3am\u7684\u5143\u7d20\u3002
    \n \u7528clear()\u65b9\u6cd5\uff0c\u6e05\u7a7a\u5217\u8868\u7684\u5143\u7d20\u3002\uff08\u676f\u5b50\u8fd8\u5728\uff0c\u6c34\u5012\u7a7a\u4e86\uff09
    \n \u7528del list \u8bed\u53e5\uff0c\u9500\u6bc1\u6574\u4e2a\u5217\u8868\u3002\uff08\u676f\u5b50\u548c\u6c34\u90fd\u6ca1\u6709\u4e86\uff09
    \n \u7528len()\u65b9\u6cd5\uff0c\u7edf\u8ba1\u5168\u90e8\u5143\u7d20\u7684\u4e2a\u6570\u3002
    \n \u7528count()\u65b9\u6cd5\uff0c\u7edf\u8ba1\u6307\u5b9a\u503c\u7684\u5143\u7d20\u7684\u4e2a\u6570\u3002
    \n \u7528max()\u65b9\u6cd5\uff0c\u7edf\u8ba1\u5143\u7d20\u4e2d\u7684\u6700\u5927\u503c\uff08\u8981\u6c42\u5143\u7d20\u7c7b\u578b\u76f8\u540c\uff1b\u6570\u5b57\u7c7b\u578b\u76f4\u63a5\u6bd4\u8f83\uff0c\u5176\u5b83\u7c7b\u578b\u6bd4\u8f83id\uff09
    \n \u7528min()\u65b9\u6cd5\uff0c\u7edf\u8ba1\u5143\u7d20\u4e2d\u7684\u6700\u5c0f\u503c\uff08\u8981\u6c42\u5143\u7d20\u7c7b\u578b\u76f8\u540c\uff1b\u6570\u5b57\u7c7b\u578b\u76f4\u63a5\u6bd4\u8f83\uff0c\u5176\u5b83\u7c7b\u578b\u6bd4\u8f83id\uff09
    \n \u7528index()\u65b9\u6cd5\uff0c\u67e5\u627e\u6307\u5b9a\u503c\u7684\u5143\u7d20\u7684\u7d22\u5f15\u4f4d\u7f6e\uff08\u7b2c\u4e00\u4e2a\u5339\u914d\u9879\uff09\u3002
    \n \u7528reverse()\u65b9\u6cd5\uff0c\u7ffb\u8f6c\u5217\u8868\u4e2d\u7684\u5143\u7d20\u3002
    \n \u7528copy()\u65b9\u6cd5\uff0c\u6d45\u62f7\u8d1d\u5e76\u751f\u6210\u65b0\u7684\u5217\u8868\u3002
    \n \u7528deepcopy()\u65b9\u6cd5\uff0c\u6df1\u62f7\u8d1d\u5e76\u751f\u6210\u65b0\u7684\u5217\u8868\u3002
    \n \u7528sort()\u65b9\u6cd5\uff0c\u5728\u539f\u5217\u8868\u57fa\u7840\u4e0a\u8fdb\u884c\u6392\u5e8f\u3002
    \n \u7528sorted()\u65b9\u6cd5\uff0c\u5c06\u65b0\u5217\u8868\u57fa\u7840\u4e0a\u5bf9\u539f\u5217\u8868\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<\/blockquote>\n

    \u4e5d\u3001\u6269\u5c55\u4e60\u9898<\/h2>\n

    \u5207\u7247\u64cd\u4f5c()<\/p>\n

    \n
    #\u5224\u65ad\u56de\u6587\ns = 'abba'\nif s == s[::-1]:\n    print(\"True\")\nelse:\n    print(\"False\")\n\n\n#\u6570\u5b57\u7ffb\u8f6c\n#\u5bf9\u4e8e\u4e00\u4e2a\u6574\u6570X\uff0c\u5b9a\u4e49\u64cd\u4f5crev(X)\u4e3a\u5c06X\u6309\u6570\u4f4d\u7ffb\u8f6c\u8fc7\u6765\uff0c\u5e76\u4e14\u53bb\u9664\u6389\u524d\u5bfc0\ndef rev(x):\n    x = str(x)\n    return int(x[::-1])\n\nz = 10590\nprint(rev(z))<\/code><\/pre>\n<\/div>\n

    \u6765\u70b9\u96be\u5ea6\uff0cleetcode\u9898\u76ee\uff0c<\/p>\n

    \n
    Given an array of size n, find the majority element. The majority element is the element that appears more than \u230a n\/2 \u230b times.\n\nYou may assume that the array is non-empty and the majority element always exist in the array.\n\nExample 1:\n\nInput: [3,2,3]\nOutput: 3\nExample 2:\n\nInput: [2,2,1,1,1,2,2]\nOutput: 2<\/code><\/pre>\n<\/div>\n

    \u6700\u597d\u7406\u89e3\u7684\u89e3\u6cd5<\/p>\n

    \n
    class Solution:\n    def majorityElement(self, nums):\n        majority_count = len(nums)\/\/2\n        for num in nums:\n            count = sum(1 for elem in nums if elem == num)\n            if count > majority_count:\n                return num\n#\u904d\u5386\u6570\u7ec4\uff0c\u67d0\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u5927\u4e8e\u534a\u6570\uff0c\u5c31\u8fd4\u56de\u8be5\u5143\u7d20\n#\u6ce8\u610f\u8fd9\u91cc\u7528\u5230\u4e86\u751f\u6210\u5668(1 for elem in nums if elem == num)<\/code><\/pre>\n<\/div>\n

    \u6700\u5feb\u7684\u89e3\u6cd5<\/p>\n

    \n
    class Solution:\n    def majorityElement(self, nums):\n        nums.sort()\n        return nums[len(nums)\/\/2]\n#\u4f17\u6570\u5fc5\u7136\u51fa\u73b0\u5728\u4e2d\u95f4\u4f4d\u7f6e<\/code><\/pre>\n<\/div>\n

    \u5e38\u89c1\u7684\u9519\u8bef<\/h2>\n
      \n
    1. TypeError: 'list' object is not callable
      \n\u547d\u540d\u9519\u8bef\uff0c\u4f7f\u7528\u4e86\u4fdd\u7559\u5b57list\u5b9a\u4e49\u4e86\u53d8\u91cf<\/li>\n<\/ol>\n
      \n
      list = [1, 2, 3] #list\u662f\u4fdd\u7559\u5b57\uff0c\u4e0d\u80fd\u505a\u53d8\u91cf\nmy_list = ['Google', 'Yahoo', 'Baidu', 'Sogou', 'Google']\nnewlist = list(set(my_list))\nprint(newlist)<\/code><\/pre>\n<\/div>\n

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

      https:\/\/mp.weixin.qq.com\/s\/OKf7N3xxrS0tJ_K8Srw-jA<\/a>
      \n
      https:\/\/www.geeksforgeeks.org\/python-list\/<\/a>
      \n
      https:\/\/www.yuanrenxue.com\/python\/python-list.html<\/a><\/p>\n

      \u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1aIPCPU-\u7f51\u7edc\u4e4b\u8def<\/a> » Python\u7684list\u5217\u8868\u8be6\u89e3<\/a><\/p>","protected":false},"excerpt":{"rendered":"

      Python list(\u5217\u8868)\uff0c\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u4e00\u79cd\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u662f\u4e00\u7ec4\u7528\u65b9\u62ec\u53f7\u62ec\u8d77\u6765\u3001\u9017\u53f7\u5206\u9694\u7684\u6570\u636e\u3002 \u5217\u8868\u7684\u5143\u7d20\u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u7684\uff0c\u4f46\u4f7f\u7528\u65f6\u901a\u5e38\u5404\u4e2a\u5143\u7d20\u7684\u7c7b\u578b\u662f\u76f8\u540c\u7684\u3002 \u5217\u8868\u652f\u6301\u52a0\u5165\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684\u5143\u7d20\uff1a\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u5217\u8868\u3001\u5143\u7ec4\u7b49\u3002 \u5217\u8868\u662f\u53ef\u53d8\u7684\uff0c\u56e0\u6b64\u5373\u4f7f\u5728\u521b\u5efa\u540e\u4e5f\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u66f4\u6539\u3002 Python\u4e2d\u7684\u5217\u8868\u662f\u6709\u5e8f\u7684\uff0c\u5e76\u4e14\u6709\u4e00\u5b9a\u6570\u91cf\u3002 \u6839\u636e\u786e\u5b9a\u7684\u5e8f\u5217\u5bf9\u5217\u8868\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d22\u5f15\uff0c\u5e76\u4f7f\u75280\u4f5c\u4e3a\u7b2c\u4e00\u4e2a\u7d22\u5f15\u6765\u5b8c\u6210\u5217\u8868\u7684\u7d22\u5f15\u3002 \u4e00\u3001\u521b\u5efa\u5217\u8868Creating a List list1 = [‘Google’, ‘Runoob’, 1997, 2000] list2 = [1, 2, 3, 4, 5 ] my_list = [‘p’,’r’,’o’,’b’,’e’] kong = [] #\u521b\u5efa\u7a7a\u767dlist\uff0c\u7528\u4e8e\u540e\u9762\u8c03\u7528 \u4e8c\u3001\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20Accessing elements from the List \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u8fd0\u7b97\u7b26[]\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u9879\u3002\u7d22\u5f15\u5fc5\u987b\u4e3a\u6574\u6570\u3002\u5d4c\u5957\u5217\u8868\u901a\u8fc7\u5d4c\u5957\u7d22\u5f15\u8bbf\u95ee\u3002 \u7d22\u5f15\u7684\u6392\u5e8f\u89c4\u5219\u5982\u4e0b\u56fe\uff0c\u4ece\u96f6\u5f00\u59cb\u3002 List = [“Geeks”, “For”, “Geeks”] print(List[0]) \u5728\u4f7f\u7528\u7d22\u5f15\u8bbf\u95ee\u5217\u8868\u4e2d\u7684\u5143\u7d20\u65f6\uff0c\u4f7f\u7528\u7684\u7d22\u5f15(\u4e0b\u6807)\u4e0d\u53ef\u8d8a\u754c\uff0c\u5426\u5219\u4f1a\u62a5\u5982\u4e0b\u9519\u8bef IndexError: list index out of range \u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\u4f1a\u5f15\u8d77\u8d8a\u754c\uff1a \u521b\u5efa\u7a7a\u7684list\uff0c\u4f46\u4f7f\u7528list[0] \u6765\u8bbf\u95ee\u5176\u4e2d\u5143\u7d20 \u8bbf\u95eelist[n] […]<\/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\/1511"}],"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=1511"}],"version-history":[{"count":1,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1511\/revisions"}],"predecessor-version":[{"id":1512,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/posts\/1511\/revisions\/1512"}],"wp:attachment":[{"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/media?parent=1511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/categories?post=1511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c.ipcpu.com\/wp-json\/wp\/v2\/tags?post=1511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}