Python list of related operations

List of related operations

Data is stored in the list can be modified, such as "add", "delete", "change", "check"

<1> additive element ( "increase" append, extend, insert)

append

You can add elements to a list by append

demo:

    #定义变量A,默认有3个元素
    A = ['xiaoWang','xiaoZhang','xiaoHua']

    print("-----添加之前,列表A的数据-----") for tempName in A: print(tempName) #提示、并添加元素 temp = input('请输入要添加的学生姓名:') A.append(temp) print("-----添加之后,列表A的数据-----") for tempName in A: print(tempName) 

result:

result

extend

May be added one by one by another set of the elements extend to the list

>>> a = [1, 2]
>>> b = [3, 4] >>> a.append(b) >>> a [1, 2, [3, 4]] >>> a.extend(b) >>> a [1, 2, [3, 4], 3, 4] 

insert

insert (index, object) is inserted into the specified position in front of the element object index

>>> a = [0, 1, 2]
>>> a.insert(1, 3) >>> a [0, 3, 1, 2] 

<2> modified element ( "change")

Modify elements of time, to determine which element is to be modified by the subscript, and then to make changes

demo:

    #定义变量A,默认有3个元素
    A = ['xiaoWang','xiaoZhang','xiaoHua']

    print("-----修改之前,列表A的数据-----") for tempName in A: print(tempName) #修改元素 A[1] = 'xiaoLu' print("-----修改之后,列表A的数据-----") for tempName in A: print(tempName) 

result:

    -----修改之前,列表A的数据-----
    xiaoWang
    xiaoZhang
    xiaoHua
    -----修改之后,列表A的数据-----
    xiaoWang
    xiaoLu
    xiaoHua

<3> Find element ( "check" in, not in, index, count)

The so-called lookup, is to look at the specified element exists

in, not in

Python find common method is as follows:

  • in (there is), if there is then the result is true, otherwise false
  • not in (does not exist), if not present then the result is true, otherwise false

demo

    #待查找的列表
    nameList = ['xiaoWang','xiaoZhang','xiaoHua']

    #获取用户要查找的名字 findName = input('请输入要查找的姓名:') #查找是否存在 if findName in nameList: print('在字典中找到了相同的名字') else: print('没有找到') 

:( 1 results found)

result

The results did not find :( 2)

result

Description:

The method will be used in long, then the same is not in use, but not in judgment does not exist

index, count

index and the usage count in the same string

>>> a = ['a', 'b', 'c', 'a', 'b'] >>> a.index('a', 1, 3) # 注意是左闭右开区间 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 'a' is not in list >>> a.index('a', 1, 4) 3 >>> a.count('b') 2 >>> a.count('d') 0 

<4> Remove elements ( "delete" del, pop, remove)

In real life analogy, if a student transfer classes, then they should put the pieces left of the student's name removed; often used in the development of deleting this function.

Common delete method list elements are:

  • del: delete according to index
  • pop: Removes the last element
  • remove: remove as value elements

demo (of the)

    movieName = ['加勒比海盗','骇客帝国','第一滴血','指环王','霍比特人','速度与激情'] print('------删除之前------') for tempName in movieName: print(tempName) del movieName[2] print('------删除之后------') for tempName in movieName: print(tempName) 

result:

    ------删除之前------
    加勒比海盗
    骇客帝国
    第一滴血
    指环王
    霍比特人
    速度与激情
    ------删除之后------
    加勒比海盗
    骇客帝国
    指环王
    霍比特人
    速度与激情

demo:(pop)

    movieName = ['加勒比海盗','骇客帝国','第一滴血','指环王','霍比特人','速度与激情'] print('------删除之前------') for tempName in movieName: print(tempName) movieName.pop() print('------删除之后------') for tempName in movieName: print(tempName) 

result:

    ------删除之前------
    加勒比海盗
    骇客帝国
    第一滴血
    指环王
    霍比特人
    速度与激情
    ------删除之后------
    加勒比海盗
    骇客帝国
    第一滴血
    指环王
    霍比特人

demo:(remove)

    movieName = ['加勒比海盗','骇客帝国','第一滴血','指环王','霍比特人','速度与激情'] print('------删除之前------') for tempName in movieName: print(tempName) movieName.remove('指环王') print('------删除之后------') for tempName in movieName: print(tempName) 

result:

    ------删除之前------
    加勒比海盗
    骇客帝国
    第一滴血
    指环王
    霍比特人
    速度与激情
    ------删除之后------
    加勒比海盗
    骇客帝国
    第一滴血
    霍比特人
    速度与激情

<5> sorted (sort, reverse)

sort the list is rearranged in a specific order, the default is small to large, reverse = True parameters can be changed to reverse, descending order.

reverse method is to list the reverse position.

>>> a = [1, 4, 2, 3] >>> a [1, 4, 2, 3] >>> a.reverse() >>> a [3, 2, 4, 1] >>> a.sort() >>> a [1, 2, 3, 4] >>> a.sort(reverse=True) >>> a [4, 3, 2, 1]

Guess you like

Origin www.cnblogs.com/yzg-14/p/12181751.html