list list in python

introduce

list is an ordered sequence of elements, with subscripts starting from 0. Elements can be of any data type in python and can be modified~

grammar

enclosed in square brackets, each element is separated by a comma

# 列表中的元素可以是:数字、字符串、布尔值、列表、元组、字典
li = [123, "我爱你", True, [], (), {}]
print(li)
# 输出=>[123, '我爱你', True, [], (), {}]

Common skills

iterate over each element in the list

li = ["我", "爱", "你"]
for item in li:
    print(item)
# 输出=> 我
# 爱 
# 你

Slice out the elements of the specified subscript range

li = ["我", "爱", "你"]
# 0:2 表示从索引0开始,知道索引2为止,但是不包括索引2,即索引0,1
print(li[0:2])
# 输出=>['我', '爱']

Modify the element at the specified subscript

li = ["我", "爱", "你"]
# 将下标是2的元素修改成"猪婆"
li[2] = "猪婆"
print(li)
# 输出=>['我', '爱', '猪婆']

You can use the del method to delete elements (the fourth method of deletion, there are three other methods in the common methods below)

li = ["我", "爱", "你"]
del li[2]
print(li)
# 输出=>['我', '爱']

# 使用del切片的方式删除
li2 = ["我", "爱", "你"]
del li2[0:2]
print(li2)
# 输出=>['你']

Convert the elements in the list to a string

# 当全是字符串的时候
li = ["我", "爱", "你"]
print("".join(li))
# 输出=>我爱你

# 当是各种类型的时候需要自己手动循环咯,毕竟数字和字符串拼接的时候是报错的
li = [True, 1234, "我", "爱", "你"]
result = ""
for item in li:
    result += str(item)
print(result)
# 输出=>True1234我爱你

Common method

append(p_object): Append an element at the end of the list (the first way to add an element)

li = ["我", "爱"]
li.append("你")
print(li)
# 输出=>我爱你

insert(index, p_object): Add an element at the specified subscript (the second way to add an element)

li = ["我", "爱", "你"]
li.insert(0, "猪婆")
print(li)
# 输出=>['猪婆', '我', '爱', '你']

# 当下标超出列表长度后,默认在列表末尾追加哦~
li1 = ["我", "爱", "你"]
li1.insert(10000, "猪婆")
print(len(li1))
# 输出=>4

# 当为-1时从右侧往左数哦,当负数超过列表长度后,将在列表第0个元素添加哦~
li2 = ["我", "爱", "你"]
li2.insert(-1, "猪婆")
print(li2)
# 输出=>['我', '爱', '猪婆', '你']

extend(iterable): Append multiple values ​​from another sequence at the end of the list at once (extend the original list with the new list) (the third way to add elements)

li = ["我", "爱", "你"]
# "一万年"这个列表加入到li的列表中去
li.extend(["一", "万", "年"])
print(li)
# 输出=>['我', '爱', '你', '一', '万', '年']

index(value, start=None, stop=None): Search from the left for the index position of the given parameter value in the list (if not found, an error will be reported), if start and end are given values, it is within the specified range search

# 从左侧开始搜寻,找到第一个后将停止搜寻
li = ["我", "爱", "你", "爱"]
print(li.index("爱"))
# 输出=>1

# 表明在第三个元素和第五个元素之间搜寻"爱"
print(li.index("爱", 2, 4))
# 输出=>3

# 当搜寻的元素在列表中不存在时将会报错
print(li.index("中"))
# 输出=>'中' is not in list

pop(index=None): delete the element according to the subscript position, if the subscript is out of bounds, an error is reported, and the deleted element is returned (the first way to delete)

li = ["我", "爱", "你"]
# 返回被删除的元素
print(li.pop(2))
# 输出=>你
print(li)
# 输出=>['我', '爱']

remove(value): Remove the element from the left according to the given parameter value, and report an error if the removed element does not exist (the second way to remove)

li = ["我", "爱", "你", "你"]
# 只会删除从左侧开始搜寻到的第一个元素
li.remove("你")
print(li)
# 输出=>['我', '爱', '你']

li2 = ["我", "爱", "你"]
li2.remove("猪婆")
# 输出=>ValueError: list.remove(x): x not in list

clear(): empty the list (the third way to delete)

li = ["我", "爱", "你"]
li.clear()
print(li)
# 输出=>[]

copy(): Shallow copy list

li = ["我", "爱", "你"]
newli = li.copy()
print(newli)
# 输出=>['我', '爱', '你']

reverse(): reverse the list elements

li = [1, 2, 3, 4]
li.reverse()
print(li)
# 输出=>[4, 3, 2, 1]

sort(key=None, reverse=False): sort

li = [1, 2, 4, 3]
li.sort()
print(li)
# 输出=>[1, 2, 3, 4]

# 表示逆序排序
li.sort(reverse=True)
print(li)
#输出=>[4, 3, 2, 1]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324883886&siteId=291194637