定義python
nums = [1, 5, 2, 3, 4, 5, 7, 9, 8, 2] users = ["woniu", "wd", "kk", "pc"]
從左向右依次爲 0, 1, 2, 3, ..., n-1數組
從右向左依次爲 -1, -2, -3, ..., -napp
In [5]: nums = [1, 5, 2, 3, 4, 5, 7, 9, 8, 2] In [6]: nums[0] = 100 In [7]: nums[5] = 200 In [8]: nums[9] = 500 In [9]: print(nums) [100, 5, 2, 3, 4, 200, 7, 9, 8, 500] In [10]:
In [10]: for num in nums: ...: print(num) ...: 100 5 2 3 4 200 7 9 8 500 In [11]:
能夠經過函數list將其餘可遍歷的類型轉換爲列表ssh
In [11]: list("qbcdefg") Out[11]: ['q', 'b', 'c', 'd', 'e', 'f', 'g'] In [12]:
In [12]: list(range(0, 10)) Out[12]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [13]:
提示:
從左到右依次兩兩比較,若是前面比後面大,則交換位置
第1次: 6,11比較,前面小,不交換[6, 11, 7, 9, 4, 2, 1]
第2次: 11, 7比較,前面大,交換[6, 7, 11, 9, 4, 2, 1]
第3次: 11, 9比較,前面大,交換[6, 7, 9, 11, 4, 2, 1]
第4次: 11, 4比較,前面大,交換[6, 7, 9, 4, 11, 2, 1]
第5次: 11, 2比較,前面大,交換[6, 7, 9, 4, 2, 11, 1]
第6次: 11, 1比較,前面大,交換[6, 7, 9, 4, 2, 1, 11]
交換元素
tmp= a; a=b; b= tmp;
a, b = b, a函數
In [14]: nums Out[14]: [100, 5, 2, 3, 4, 200, 7, 9, 8, 500] In [15]: len(nums) Out[15]: 10
In [16]: max(nums), min(nums) Out[16]: (500, 2) In [17]:
In [17]: 1 in nums Out[17]: False In [18]: 2 in nums Out[18]: True In [19]:
刪除列表中的元素(根據索引刪除list中對應元素)spa
In [19]: del nums[0] In [20]: nums Out[20]: [5, 2, 3, 4, 200, 7, 9, 8, 500] In [21]:
四則運算:code
In [21]: [1, 2, 3] + [4, 5, 6] Out[21]: [1, 2, 3, 4, 5, 6]
In [22]: [1, 2, 3] * 3 Out[22]: [1, 2, 3, 1, 2, 3, 1, 2, 3]
In [23]: nums = list(range(10)) In [24]: nums Out[24]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [25]: nums[: 5] Out[25]: [0, 1, 2, 3, 4] In [26]: nums[4:] Out[26]: [4, 5, 6, 7, 8, 9] In [27]: nums[4: 7] Out[27]: [4, 5, 6] In [28]: nums[2: 7: 2] Out[28]: [2, 4, 6] In [29]:
In [29]: nums = list(range(10)) In [30]: nums_1 = nums In [31]: nums_1[1] = 20 In [32]: nums Out[32]: [0, 20, 2, 3, 4, 5, 6, 7, 8, 9] In [33]:
In [35]: nums Out[35]: [0, 20, 2, 3, 4, 5, 6, 7, 8, 9] In [36]: nums_2 = nums[:] In [37]: nums_2[1] = 40 In [38]: nums Out[38]: [0, 20, 2, 3, 4, 5, 6, 7, 8, 9] In [39]: nums_2 Out[39]: [0, 40, 2, 3, 4, 5, 6, 7, 8, 9] In [40]:
In [40]: id(nums) Out[40]: 139966877693384 In [41]: id(nums_1) Out[41]: 139966877693384 In [42]: id(nums_2) Out[42]: 139966877738696 In [43]:
In [43]: nums[:: -1] Out[43]: [9, 8, 7, 6, 5, 4, 3, 2, 20, 0] In [44]:
In [44]: nums[:: 2] Out[44]: [0, 2, 4, 6, 8] In [45]:
In [46]: nums Out[46]: [0, 20, 2, 3, 4, 5, 6, 7, 8, 9] In [47]:
語法:list[start: end] = [v_1, v_2, v_3. ..., v_n]orm
使用列表list[start: end] = [v_1, v_2, v_3. ..., v_n]替換切片對應位置的元素排序
按照順序替換切片中的元素,切片中多餘的元素刪除索引
按照順序替換切片中的元素,賦值list中多餘的元素則添加到切片最右索引以後
In [48]: nums = list(range(10)) In [49]: nums Out[49]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [50]: nums[1: 2] Out[50]: [1] In [51]: nums[1: 2] = [11, 12] In [52]: nums Out[52]: [0, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9] In [53]: nums[5:7] Out[53]: [4, 5] In [54]: nums[5:7] = [21] In [55]: nums Out[55]: [0, 11, 12, 2, 3, 21, 6, 7, 8, 9] In [56]: nums[8:10] Out[56]: [8, 9] In [57]: nums Out[57]: [0, 11, 12, 2, 3, 21, 6, 7, 8, 9] In [58]: nums[8:10] = [] In [59]: nums Out[59]: [0, 11, 12, 2, 3, 21, 6, 7] In [60]: nums[:] = [] In [61]: nums Out[61]: []
In [62]: dir(list) Out[62]: ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] In [63]:
In [63]: help(list.append) Help on method_descriptor: append(...) L.append(object) -> None -- append object to end
In [2]: nums = [] In [3]: nums Out[3]: [] In [4]: nums.append(1) In [5]: nums.append("abc") In [6]: nums.append([2, 3, 4]) In [7]: nums Out[7]: [1, 'abc', [2, 3, 4]] In [8]:
In [8]: nums = [] In [9]: nums Out[9]: [] In [10]: nums.extend("abc") In [11]: nums.extend([2, 3, 4]) In [12]: nums Out[12]: ['a', 'b', 'c', 2, 3, 4] In [13]:
In [13]: nums = list(range(10)) In [14]: nums Out[14]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [15]: nums.clear() In [16]: nums Out[16]: [] In [17]:
In [17]: nums = list(range(10)) In [18]: nums Out[18]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [19]: nums_3 = nums.copy() In [20]: nums_3[1] = 30 In [21]: nums Out[21]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [22]: nums_3 Out[22]: [0, 30, 2, 3, 4, 5, 6, 7, 8, 9] In [23]:
In [23]: nums = list(range(10)) In [24]: nums Out[24]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [25]: nums.count(4) Out[25]: 1 In [26]: nums.count(-1) Out[26]: 0 In [27]:
In [27]: nums = list(range(10)) In [28]: nums Out[28]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [29]: nums.index(4) Out[29]: 4 In [30]: nums.index(-1) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-30-d837de89ae77> in <module>() ----> 1 nums.index(-1) ValueError: -1 is not in list In [31]:
In [31]: nums Out[31]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [32]: nums.pop() Out[32]: 9 In [33]: nums.pop(1) Out[33]: 1 In [34]: nums Out[34]: [0, 2, 3, 4, 5, 6, 7, 8] In [35]: nums.pop(10) --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-35-0dd19aea6900> in <module>() ----> 1 nums.pop(10) IndexError: pop index out of range In [36]:
In [37]: nums = list(range(10)) In [38]: nums Out[38]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [39]: nums.remove(3) In [40]: nums Out[40]: [0, 1, 2, 4, 5, 6, 7, 8, 9] In [41]: nums.remove(10) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-41-59cce22df83b> in <module>() ----> 1 nums.remove(10) ValueError: list.remove(x): x not in list In [42]:
In [42]: nums = list(range(10)) In [43]: nums Out[43]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [44]: nums.reverse() In [45]: nums Out[45]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
In [46]: nums = [1, 5, 3, 4, 2] In [47]: nums Out[47]: [1, 5, 3, 4, 2] In [48]: nums.sort() In [49]: nums Out[49]: [1, 2, 3, 4, 5] In [50]:
提示用戶輸入do或者任務(非do)
若是用戶輸入任務,則添加到list中
若是用戶輸入do,當任務爲空時則打印「無任務」並退出,不然從list中根據先進先出原則打印以前輸入的一個任務
????
nums_1 = [1, 2, 3, 4, 5, 3, 10, 11]
nums_2 = [1, 2, 3, 1, 4, 5, 5, 3, 12, 34]
注:保證第二個練習中第三個列表中元素不重複
#encoding: utf-8 nums_1 = [1, 2, 3, 4, 5, 3, 10, 11] nums_2 = [1, 2, 3, 1, 4, 5, 5, 3, 12, 34] nums_3 = [] for num in nums_1: if num in nums_2: nums_3.append(num) print(nums_3)
不可變的列表
In [1]: nums = (1, 2, 4, 5, 7, 8, 9, 3, 2) In [2]: users = ('kk', 'pc', 'wd', 'woniu', 'xuegao')
In [3]: nums = (1,) In [4]: nums_2 = (1) In [5]: type(nums), type(nums_2) Out[5]: (tuple, int) In [6]:
從左向右依次爲 0, 1, 2, 3, ..., n-1
從右向左依次爲 -1, -2, -3, ..., -n
In [6]: for num in nums: ...: print(num) ...: 1 In [7]:
In [9]: tuple('abcdefg') Out[9]: ('a', 'b', 'c', 'd', 'e', 'f', 'g') In [10]: tuple([1, 2, 3, 4, 5, 6]) Out[10]: (1, 2, 3, 4, 5, 6) In [11]: tuple(range(10)) Out[11]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) In [12]:
In [17]: nums = tuple(range(10)) In [18]: nums Out[18]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) In [19]: len(nums) Out[19]: 10 In [20]:
In [20]: max(nums), min(nums) Out[20]: (9, 0)
In [21]: 1 in nums Out[21]: True In [22]: 23 in nums Out[22]: False In [23]:
必須爲兩個tuple相加
In [23]: (1, 2, 3) + (5, 6, 7) Out[23]: (1, 2, 3, 5, 6, 7) In [24]:
必須爲一個整數
In [24]: (1, 2,3) * 5 Out[24]: (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3) In [25]:
按照規則獲取tuple中一部分元素生成新的tuple
In [25]: nums = tuple(range(10)) In [26]: nums Out[26]: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) In [27]: nums[:3] Out[27]: (0, 1, 2) In [28]: nums[4:7] Out[28]: (4, 5, 6) In [29]: nums[4:7:2] Out[29]: (4, 6) In [30]:
In [30]: nums = (1, 2, [5, 6]) In [31]: nums[2] Out[31]: [5, 6] In [32]: nums[2] = [7, 8, 9] --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-32-370d2f16530e> in <module>() ----> 1 nums[2] = [7, 8, 9] TypeError: 'tuple' object does not support item assignment In [33]: nums[2].append(7) In [34]: nums Out[34]: (1, 2, [5, 6, 7]) In [35]:
爲何nums[2].append(7)執行成功了?
In [35]: id(nums[2]) Out[35]: 140210392503816 In [36]: nums[2].append(8) In [37]: id(nums[2]) Out[37]: 140210392503816 In [38]:
In [38]: dir(tuple) Out[38]: ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index'] In [39]:
用戶管理: