Python学习笔记(一):list属性及方法浅析

举报
悦来客栈的老板 发表于 2020/12/28 23:57:26 2020/12/28
【摘要】 列表学习。 List是python中的基本数据结构之一。创建一个列表的方法 L = [1,2,3,'a'] 通过help(list)查看列表的属性及方法。 >>> help(list) Help on class list in module builtins: class list(object)  |&nbsp...





































































































































  
  1. In [1]: a = list()
  2. In [2]: a
  3. Out[2]: []







  
  1. In [6]: s = "hello world!"
  2. In [7]: L = list(s)
  3. In [8]: L
  4. Out[8]: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']








  
  1. In [1]: a = [1,2]
  2. In [2]: b = [1,2]
  3. In [3]: a.__add__(b) # 返回a+b,a,b本身没有改变
  4. Out[3]: [1, 2, 1, 2]
  5. In [4]: a
  6. Out[4]: [1, 2]
  7. In [5]: b
  8. Out[5]: [1, 2]
  9. In [6]: c = a.__add__(b)
  10. In [7]: c
  11. Out[7]: [1, 2, 1, 2]
  12. In [8]: d = a+b
  13. In [9]: c == b
  14. Out[9]: False
  15. In [10]: c == d
  16. Out[10]: True









  
  1. In [11]: L = [1,2,3,4]
  2. In [12]: L.__contains__(1)
  3. Out[12]: True
  4. In [13]: L.__contains__(2)
  5. Out[13]: True
  6. In [14]: L.__contains__(0)
  7. Out[14]: False
  8. In [15]: L.__contains__('a')
  9. Out[15]: False
  10. In [16]: L.__contains__(L) #并不包含本身
  11. Out[16]: False
  12. In [18]: L in L
  13. Out[18]: False
  14. In [19]: 2 in L
  15. Out[19]: True
  16. In [20]: 0 in L
  17. Out[20]: False









  
  1. In [21]: L = [1,2,3]
  2. In [22]: L.__delitem__(0) #删除第0个对象
  3. In [23]: L
  4. Out[23]: [2, 3]
  5. In [24]: del L[1] #删除第1个对象
  6. In [25]: L
  7. Out[25]: [2]









  
  1. In [36]: a = [1,2,3,4]
  2. In [37]: b = [1,2]
  3. In [38]: a.__eq__(1)
  4. Out[38]: NotImplemented #没有实现该功能
  5. In [39]: a.__eq__([1,2])
  6. Out[39]: False
  7. In [40]: a.__eq__(b)
  8. Out[40]: False
  9. In [41]: a.__eq__([1,2,3,4])
  10. Out[41]: True
  11. In [42]: a.__eq__((1,2,3,4))
  12. Out[42]: NotImplemented
  13. 与 == 略有差异
  14. In [43]: a = [1,2,3,4]
  15. In [44]: b = [1,2]
  16. In [45]: a == 1
  17. Out[45]: False
  18. In [46]: a == [1,2]
  19. Out[46]: False
  20. In [47]: a == [1,2,3,4]
  21. Out[47]: True
  22. In [48]: a == b
  23. Out[48]: False
  24. In [49]: a == (1,2,3,4)
  25. Out[49]: False











  
  1. In [63]: a = [1,2,3,4]
  2. In [64]: a.__getattribute__('__len__')
  3. Out[64]: <method-wrapper '__len__' of list object at 0x02558EE0>
  4. In [65]: b = a.__getattribute__('__len__')
  5. In [66]: b()
  6. Out[66]: 4
  7. In [67]: a
  8. Out[67]: [1, 2, 3, 4]
  9. In [68]: a.__getattribute__('copy')
  10. Out[68]: <function list.copy>
  11. In [69]: b = a.__getattribute__('copy')
  12. In [70]: c = b()
  13. In [71]: c
  14. Out[71]: [1, 2, 3, 4]









  
  1. In [1]: a = [1,2,3,"hello",(1,2,3)]
  2. In [2]: a.__getitem__(0)
  3. Out[2]: 1
  4. In [3]: a.__getitem__(3)
  5. Out[3]: 'hello'
  6. In [4]: a.__getitem__(4)
  7. Out[4]: (1, 2, 3)
  8. In [5]: a.__getitem__(5) 无法访问越界的对象
  9. ---------------------------------------------------------------------------
  10. IndexError Traceback (most recent call last)
  11. <ipython-input-5-3a58e3eacf6a> in <module>()
  12. ----> 1 a.__getitem__(5)
  13. IndexError: list index out of range #越界报错
  14. In [6]: a = [1,2,3,"hello",(1,2,3)]
  15. In [7]: a[0]
  16. Out[7]: 1
  17. In [8]: a[3]
  18. Out[8]: 'hello'
  19. In [9]: a[4]
  20. Out[9]: (1, 2, 3)
  21. In [10]: a[5]
  22. ---------------------------------------------------------------------------
  23. IndexError Traceback (most recent call last)
  24. <ipython-input-10-b6a934feab86> in <module>()
  25. ----> 1 a[5]
  26. IndexError: list index out of range














  
  1. In [3]: a = [1,2,3,4]
  2. In [4]: a.__iadd__("hello")
  3. Out[4]: [1, 2, 3, 4, 'h', 'e', 'l', 'l', 'o'] #先将字符串转换为列表,然后再相加,列表会改变
  4. In [5]: a.__iadd__((1,2,3))
  5. Out[5]: [1, 2, 3, 4, 'h', 'e', 'l', 'l', 'o', 1, 2, 3] #先将元组转换为列表,然后再相加,列表会改变
  6. In [6]: a.__iadd__([1111,1113]) #列表相加,列表会改变
  7. Out[6]: [1, 2, 3, 4, 'h', 'e', 'l', 'l', 'o', 1, 2, 3, 1111, 1113]
  8. In [7]: a #a已改变
  9. Out[7]: [1, 2, 3, 4, 'h', 'e', 'l', 'l', 'o', 1, 2, 3, 1111, 1113]







  
  1. In [15]: a = [1,2,3,'abc']
  2. In [16]: a.__imul__(2) #*2并返回给a
  3. Out[16]: [1, 2, 3, 'abc', 1, 2, 3, 'abc']
  4. In [17]: a #a已改变
  5. Out[17]: [1, 2, 3, 'abc', 1, 2, 3, 'abc']
  6. In [18]: a.__imul__([1,2]) #参数不是integer类型,报错
  7. ---------------------------------------------------------------------------
  8. TypeError Traceback (most recent call last)
  9. <ipython-input-18-58ca78754a99> in <module>()
  10. ----> 1 a.__imul__([1,2])
  11. TypeError: 'list' object cannot be interpreted as an integer









  
  1. In [23]: a = []
  2. In [24]: b = [1,2,3,4]
  3. In [25]: a.__init__(b)
  4. In [26]: a
  5. Out[26]: [1, 2, 3, 4]
  6. In [27]: a.__init__((1,2,3)) #先将元组转换为列表,再初始化给a
  7. In [28]: a
  8. Out[28]: [1, 2, 3]








  
  1. In [34]: a = [1,2,3,4,54]
  2. In [35]: for i in a.__iter__():
  3. ...: print (i)
  4. ...:
  5. 1
  6. 2
  7. 3
  8. 4
  9. 54
  10. In [36]: a.__iter__
  11. Out[36]: <method-wrapper '__iter__' of list object at 0x024B7D00>
  12. In [37]: a.__iter__() #列表类型的迭代器
  13. Out[37]: <list_iterator at 0x25586b0>




__le__(self, value, /)      Return self<=value.  判断列表是不大于某列表。与__eq__类似






  
  1. In [38]: a = [1,2,3,"abc",(111,222,333)]
  2. In [39]: a.__len__()
  3. Out[39]: 5
  4. In [40]: a.__len__
  5. Out[40]: <method-wrapper '__len__' of list object at 0x024C44B8>












  
  1. In [41]: a = [1,2,3,"abc",(111,222,333)]
  2. In [42]: a.__mul__(2)
  3. Out[42]: [1, 2, 3, 'abc', (111, 222, 333), 1, 2, 3, 'abc', (111, 222, 333)]
  4. In [43]: a
  5. Out[43]: [1, 2, 3, 'abc', (111, 222, 333)]










  
  1. In [1]: a = [1,2,3,4]
  2. In [2]: b = a.__new__(list)
  3. In [3]: b
  4. Out[3]: []
  5. In [4]: b = a.__new__(str)
  6. ---------------------------------------------------------------------------
  7. TypeError Traceback (most recent call last)
  8. <ipython-input-4-6fee1f0c3d80> in <module>()
  9. ----> 1 b = a.__new__(str)
  10. TypeError: list.__new__(str): str is not a subtype of list
  11. In [5]: b = a.__new__(tuple)
  12. ---------------------------------------------------------------------------
  13. TypeError Traceback (most recent call last)
  14. <ipython-input-5-18991a398a58> in <module>()
  15. ----> 1 b = a.__new__(tuple)
  16. TypeError: list.__new__(tuple): tuple is not a subtype of list






  
  1. In [12]: a = [1,2,3,4]
  2. In [13]: b = a.__repr__()
  3. In [14]: b
  4. Out[14]: '[1, 2, 3, 4]' #字符串
  5. In [15]: a
  6. Out[15]: [1, 2, 3, 4]






  
  1. In [19]: a = [1,2,3,4]
  2. In [20]: a.__reversed__()
  3. Out[20]: <list_reverseiterator at 0x2538c50>
  4. In [21]: a
  5. Out[21]: [1, 2, 3, 4]
  6. In [22]: b = a.__reversed__()
  7. In [23]: for i in b:
  8. ...: print (i)
  9. ...:
  10. 4
  11. 3
  12. 2
  13. 1







  
  1. In [34]: a = [1,2,3,4,"abc"]
  2. In [35]: a.__mul__(-1)
  3. Out[35]: []
  4. In [36]: a.__mul__(0)
  5. Out[36]: []
  6. In [37]: a
  7. Out[37]: [1, 2, 3, 4, 'abc']
  8. In [38]: a.__mul__(2)
  9. Out[38]: [1, 2, 3, 4, 'abc', 1, 2, 3, 4, 'abc']







  
  1. In [39]: a
  2. Out[39]: [1, 2, 3, 4, 'abc']
  3. In [40]: a.__setitem__(2,"aaa")
  4. In [41]: a
  5. Out[41]: [1, 2, 'aaa', 4, 'abc']
  6. In [43]: a
  7. Out[43]: [1, 2, 3, 4, 'abc']








  
  1. In [44]: a = []
  2. In [45]: a.__sizeof__
  3. Out[45]: <function list.__sizeof__>
  4. In [46]: a.__sizeof__()
  5. Out[46]: 20
  6. In [47]: a = [1]
  7. In [48]: a.__sizeof__()
  8. Out[48]: 24
  9. In [49]: a = [1,2,3]
  10. In [50]: a.__sizeof__()
  11. Out[50]: 32
  12. In [51]: a = ['a']
  13. In [52]: a.__sizeof__()
  14. Out[52]: 24
  15. In [53]: a = ['abc']
  16. In [54]: a.__sizeof__()
  17. Out[54]: 24






  
  1. In [55]: a = [1]
  2. In [56]: a.append(2)
  3. In [57]: a
  4. Out[57]: [1, 2]
  5. In [58]: a.append('2')
  6. In [59]: a
  7. Out[59]: [1, 2, '2']
  8. In [60]: a.append([1,2])
  9. In [61]: a
  10. Out[61]: [1, 2, '2', [1, 2]]
  11. In [62]: a.append((3,4))
  12. In [63]: a
  13. Out[63]: [1, 2, '2', [1, 2], (3, 4)]







  
  1. In [64]: a
  2. Out[64]: [1, 2, '2', [1, 2], (3, 4)]
  3. In [65]: a.clear()
  4. In [66]: a
  5. Out[66]: []






  
  1. In [67]: a = [1,2,3,4]
  2. In [68]: b = a.copy()
  3. In [69]: a
  4. Out[69]: [1, 2, 3, 4]
  5. In [70]: b
  6. Out[70]: [1, 2, 3, 4]





  
  1. In [71]: a = [1,2,3,'a','aa',[1,2]]
  2. In [72]: a.count(1)
  3. Out[72]: 1
  4. In [73]: a.count('a')
  5. Out[73]: 1
  6. In [74]: a.count([1,2])
  7. Out[74]: 1
  8. In [75]: a.count(0)
  9. Out[75]: 0






  
  1. In [80]: a = [1,2,3]
  2. In [81]: a.extend((1,2,3))
  3. In [82]: a
  4. Out[82]: [1, 2, 3, 1, 2, 3]
  5. In [83]: a.extend([4,5,6])
  6. In [84]: a
  7. Out[84]: [1, 2, 3, 1, 2, 3, 4, 5, 6]









  
  1. In [85]: a
  2. Out[85]: [1, 2, 3, 1, 2, 3, 4, 5, 6]
  3. In [86]: a.index(2)
  4. Out[86]: 1 #返回第一个出现对象的位置
  5. In [87]: a.index(7) #列表中不存在是时报错
  6. ---------------------------------------------------------------------------
  7. ValueError Traceback (most recent call last)
  8. <ipython-input-87-e64802c95647> in <module>()
  9. ----> 1 a.index(7)
  10. ValueError: 7 is not in list









  
  1. In [90]: a.insert(0,"abc")
  2. In [91]: a
  3. Out[91]: ['abc', 1, 2, 3, 1, 2, 3, 4, 5, 6]










  
  1. In [92]: a.pop(2)
  2. Out[92]: 2
  3. In [93]: a
  4. Out[93]: ['abc', 1, 3, 1, 2, 3, 4, 5, 6]










  
  1. In [100]: a = [1,1,2,2,3,3,4,4,"aa",'aa']
  2. In [101]: a.remove(1)
  3. In [102]: a
  4. Out[102]: [1, 2, 2, 3, 3, 4, 4, 'aa', 'aa']
  5. In [103]: a.remove('aa')
  6. In [104]: a
  7. Out[104]: [1, 2, 2, 3, 3, 4, 4, 'aa']









  
  1. In [105]: a = [1,2,3]
  2. In [106]: a.reverse()
  3. In [107]: a
  4. Out[107]: [3, 2, 1]








  
  1. In [112]: a = [1,9,2,3,7,4]
  2. In [113]: a.sort()
  3. In [114]: a
  4. Out[114]: [1, 2, 3, 4, 7, 9]




文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/77978925

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。