Python full stack path-list (Day 04)

Source: Internet
Author: User

1. Two methods of listing 1. Built-in methods for lists
  1. Add to List

    格式:x.__add__(y)等同于x+y例如:list1 = [1,2,3]     print(list1.__add__([4,5,6])) >>> [1,2,3,4,5,6]返回值:list

    Note: The preceding y can be list/tuple/str/dict/set and so on, but it cannot be int.

  2. Whether it is an inclusive relationship

    格式:x.__contains__(y)等同于y in x例如:list1 = [1,2,3]     print(list1.__contains__([2,3])) >>> True返回值:bool

    Note: The preceding y can be list/tuple/str/dict/set and so on, but it cannot be int.

  3. Specify list index deletions (modify original list)

    格式:x.__delitem__(index)等同于delete x[index]例如:list1 = [1,2,3]     list1.__delitem__(2)     print(list1) >>> [1,2]返回值:None
  4. Determine if two lists are equal

    格式:x.__eq__(y)等同于x==y例如:list1 = [1,2,3]     print(list1.__eq__([2,3])) >>> False返回值:bool
  5. Determine if list 1 is greater than or equal to List 2

    格式:x.__ge__(y)等同于x>=y例如:list1 = [1,2,3]     print(list1.__ge__([1,2])) >>> True返回值:bool

    Note: This method is primarily judged by the first element of the list

  6. Methods for accessing properties

    格式:x.__getitem__(index)等同于x[index]例如:list1 = [1,2,3]     print(list1.__getitem(2)) >>> 3返回值:object
  7. Determine if list 1 is greater than List 2

    格式:x.__gt__(y)等同于x>y例如:list1 = [1,2,3]     print(list1.__gt__([2])) >>> False返回值:bool

    Note: This method is primarily judged by the first element of the list

  8. Add Value of Assignment

    格式:x.__iadd__(y)等同于x += y例如:list1 = [1,2,3]     print(list1__iadd__(‘3‘)) >>> [1, 2, 3, ‘3‘]返回值:list

    Note: The preceding y can be list/tuple/str/dict/set, but not int, and the original list will change.

  9. Multiply the assignment value

    格式:x.__imul__(num)等同于x *= num例如:list1 = [1,2,3]     print(list1.__imul__(2)) >>> [1,2,3,1,2,3]返回值:list

    Note:num can only be of type int, and the original list will change.

  10. Constructor method (Modify the original list)

    格式:x.__init__(y)例如:list1 = [1,2,3]     list1.__init__([4,5])     print(list1) >>> [4,5]返回值:list

    Note:y does not specify to construct an empty list

  11. Iterators

    格式:x.__iter__()等同于iter(x)例如:list1 = [1,2,3]     die = list1.__iter__()     print(die.__next__()) >>> 1返回值:迭代器
  12. Determine if list 1 is less than or equal to List 2

    格式:x.__le__(y)等同于x<=y例如:list1 = [1,2,3]     print(list1.__le__([2])) >>> True返回值:bool

    Note: This method is primarily judged by the first element of the list

  13. List length

    格式:x.__len__()等同于len(x)例如:list1 = [1,2,3]     print(list1.__len__()) >>> 3返回值:int
  14. Determine if list 1 is less than List 2

    格式:x.__lt__(y)等同于x<y例如:list1 = [1,2,3]     print(list1.__lt__([2])) >>> True返回值:bool

    Note: This method is primarily judged by the first element of the list

  15. Multiplication of lists

    格式:x.__mul__(num)等同于x*y例如:list1 = [1,2,3]     print(list.__mul__(2)) >>> [1,2,3,1,2,3]返回值:list
  16. Determine if two lists are not equal

    格式:x.__ne__(y)等同于x!=y例如:list1 = [1,2,3]     print(list1.__ne__([4,5])) >>> True返回值:bool
  17. Redefining a list

    格式:x.__new__(list)例如:list1 = [1,2,3]     print(list1.__new__(list)) >>> []返回值:list
  18. Built-in transformations

    格式:x.__reversed__()等同于reversed(x)例如:list1 = [1,‘2‘,3,‘d‘,‘$‘]     die = list1.__reversed__()     print(die.__next__()) >>> ‘$‘返回值:迭代器
  19. Right → Multiply left list

    格式:x.__rmul__(num)等同于num*x例如:list1 = [1,2,3]     print(list1.__rmul__(2)) >>> [1,2,3,1,2,3]返回值:list
  20. Modify the elements of the specified index (modify the original list)

    格式:x.__getitem__(index,value)等同于x[index]=value例如:list1 = [1,2,3]     list1.__getitem__(1,5)     print(list1) >>> [1,5,3]返回值:None
  21. List in memory size, converted to bytes for calculation

    格式:x.__sizeof__()例如:list1 = [1,2,3]     list2 = [2,3]     print(list1.__sizeof__()) >>> 64     print(list2.__sizeof__()) >>> 56       返回值:int#列表长度的不同,导致类别在内存中的大小也不一样。
2. Common methods
  1. Add an element to the original list

    格式:x.append(y)    #直接修改原列表,添加的元素可以任意对象,每次只能添加一个元素例如:list1 = [1,2,3,4]     list1.append([1,2,3])     print(list1) >>> [1,2,3,4,[1,2,3]]返回值:None
  2. Clear the original list

    格式:x.clear()    #删除列表中所有元素,列表本身不删除例如:list1 = [1,2,3,4]     list1.clear()     print(list1) >>> []返回值:None
  3. Copy List

    格式:x.copy()    #复制原列表,生成一个新列表例如:list1 = [1,2,3,4]     print(list1.copy()) >>> [1,2,3,4]返回值:list
  4. Count

    格式:x.count(value)    #统计列表中指定元素的个数例如:list1 = [1,2,2,3,4,2,3]     print(list1.count(2)) >>> 3返回值:int
  5. Expand list

    格式:x.extend()    #在原列表中扩充列表(修改原列表),两个列表合并为一个列表例如:list1 = [1,2,3,4]     list1.extend([‘1‘,‘2‘])     print(list1) >>> [1, 2, 3, 4, ‘1‘, ‘2‘]返回值:None
  6. Removes the index of the specified element value

    格式:x.index(value,strat,end)     #在指定的索引范围内,查找元素的索引,默认全列表查找例如:list1 = [1, 2, 3, 4, ‘1‘, ‘2‘]     print(list1.index(‘1‘,2,5)) >>> 4返回值:int
  7. inserting elements

    格式:x.insert(index,object)    #在原列表中,指定索引位置处添加一个元素,元素可以是任意对象例如:list1 = [1, 2, 3, 4, ‘1‘, ‘2‘]     list1.insert(3,‘four‘)     print(list1) >>> [1, 2, 3, ‘four‘, 4, ‘1‘, ‘2‘] 返回值:None
  8. Remove the value of the specified index

    格式:x.pop(index)    #删除指定索引的值,默认从列表的最后面开始删除例如:list1 = [1, 2, 3, ‘four‘, 4, ‘1‘, ‘2‘]     print(list1.pop()) >>> ‘2‘     print(list1.pop(2)) >>> 3     print(list1) >>> [1, 2, ‘four‘, 4, ‘1‘]返回值:object
  9. To remove the specified element

    格式:x.remove(value)    #删除指定元素,直接修改原列表例如:list1 = [1, 2, 3, ‘four‘, 4, ‘1‘, ‘2‘]     list1.remove(3)     print(list1) >>> [1, 2, ‘four‘, 4, ‘1‘, ‘2‘]返回值:None
  10. Reverse

    格式:x.reverse()    #对列表进行升序排序后再进行颠倒例如:list1 = [1, 2, 3, ‘four‘, 4, ‘1‘, ‘2‘]     list1.reverse()     print(list1) >>> [‘2‘, ‘1‘, 4, ‘four‘, 3, 2, 1]返回值:None
  11. Sort Ascending

    格式:x.sort()    #对列表进行升序排序,一般int和str类型不能同时存在例如:list1 = [6,2,3,4]     list1.sort()     print(list1) >>> [2,3,4,6]返回值:None
2. Operation of the list 1. The underlying operation of the list
  1. Add to List

    Index operations

    Member detection

    Maximum Value

    注:该函数只能用于纯数字的列表。

  2. 转换为列表

    格式:for variable in list: 使用variable 例如:list1 = [1,2,3] for i in list1: print(i) >>> 1 >>> 2 >>> 3
  3. while looping through

          Format: variable = 0 while variable < len (list): Use elements in List (list[variable]) variable + = 1 For example: List1 = [+/-] i = 0 while I < Len (list1): print (List1[i]) i + = 1 >>> 1 >>> 2 >>> ; 3      
  4. 二级列表的遍历

    1. 格式:for variable1 in list: for variable2 in variable1: 使用variable 例如:list1 = [[‘a‘,‘b‘,‘c‘],[‘d‘,‘e‘,‘f‘],[‘g‘,‘h‘,‘i‘]] for i in list1: for j in i: print(j) #输出的j就是大列表中列表的元素2. 格式:for variable1,variable2,... in list: 直接使用variable1,variable2,... 例如:list1 = [[‘a‘,‘b‘,‘c‘],[‘d‘,‘e‘,‘f‘],[‘g‘,‘h‘,‘i‘]] for n1,n2,n3 in list1: print(n1) >>> ‘a‘ >>> ‘d‘ >>> ‘g‘

    注:方法2只能遍历同等长度的二级列表。

  5. 列表推导式

    1. 简单的列表推导式

      格式:[variable for variable in list]例如:list1 = [1,3,4,5] print([i for i in list1]) >>> [1,3,4,5]返回值:list#只是把原来的列表遍历了一遍,生成了和原列表一样的列表
    2. 稍作修改的列表推导式

      格式:[‘*‘+variable+‘*‘ for variable in list]例如:list1 = [‘a‘,‘b‘,‘c‘] print([‘*‘+i+‘*‘ for i in list1]) >>> [‘*a*‘, ‘*b*‘, ‘*c*‘]返回值:list#对variable进行修改,可以是int的操作,也可以是str的操作,具体需根据列表元素来执行
    3. 带有判断条件的列表推导式

      格式:[variable for variable in list if 条件表达式]例如:list1 = [2,3,4,2,1,2,23,54,21,33] print([i for i in list1 if i>20]) >>> [23, 54, 21, 33]返回值:list
    4. 多个列表推导式

      格式:[variable1+variable2 for variable1 in list1 for variable2 in list2]例如:list1 = [‘a‘,‘b‘,‘c‘] list2 = [‘I‘,‘love‘,‘you‘,‘!‘] print([x+y for x in list1 for y in list2]) >>> [‘aI‘, ‘alove‘, ‘ayou‘, ‘a!‘, ‘bI‘, ‘blove‘, ‘byou‘, ‘b!‘, ‘cI‘, ‘clove‘, ‘cyou‘, ‘c!‘]返回值:list
    5. 带有判断条件的多个列表推导式

      格式:[variable1+variable2 for variable1 in list1 for variable2 in list2 if 条件表达式]例如:list1 = [‘a‘,‘b‘,‘c‘] list2 = [‘I‘,‘love‘,‘you‘,‘!‘] print([x+y for x in list1 for y in list2 if list1.index(x)==list2.index(y)]) >>> [‘aI‘, ‘blove‘, ‘cyou‘]返回值:list

Turn from: Python Changes the life--Introduction to the list in Python

Python full stack path-list (Day 04)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.