From entry to prison-lists and related functions

Getting started N day

List slice-get some elements in the list (get multiple elements at once)

1. Basic syntax
[start subscript: end subscript: step length]-get from the start subscript to before the end subscript, each time the subscript value increases and the step size is added to get the next element
. The slice of the list must be a list
The element corresponding to the end subscript must not be retrieved

The positive or negative of the step size represents the direction of the value. A positive step size means that the value is taken from the beginning index to the back.
If it is negative, the value is
taken from the beginning index to the front. The range that cannot be obtained is an empty list

list1 = [0, 1, 2, 3, 4, 5]

print(list1[1:4:2])  # [1, 3]

print(list1[5:1:-1])  # [5, 4, 3, 2]
# 取不到的情况
print(list1[2:5:-1])  # []
print(list1[3:3:1])  # []
print(list1[1:-1:-1])  # []

# 切片省略
# 省略


# 省略步长 :列表[开始下标:结束下标]
masters = ['貂蝉', '小乔', '甄姬', '王昭君', '周瑜', '小乔']
print(masters[1:3])  # ['小乔', '甄姬']

# 省略开始下标: 步长为正开始下标为0,步长为负开始下标是-1
masters = ['貂蝉', '小乔', '甄姬', '王昭君', '周瑜', '小乔']
print(masters[:5:2])  # ['貂蝉', '甄姬', '周瑜']
print(masters[:-2:2])  # ['貂蝉', '甄姬']
print(masters[:4:-2])  # ['小乔']

# 省略结束下标:步长为正取到最后一个元素为止,步长为负:取到第一个元素为止
masters = ['貂蝉', '小乔', '甄姬', '王昭君', '周瑜', '小乔']
print(masters[1::1])  # ['小乔', '甄姬', '王昭君', '周瑜', '小乔']
print(masters[2::-1])  # ['甄姬', '小乔', '貂蝉']
print(masters[3::-2])  # ['王昭君', '小乔']

# 开始下标和结束下标都省略
print(masters[::-1])  # ['小乔', '周瑜', '王昭君', '甄姬', '小乔', '貂蝉']
print(masters[::-2])  # ['小乔', '王昭君', '小乔']
# 全部省略
print(masters[:])  # ['貂蝉', '小乔', '甄姬', '王昭君', '周瑜', '小乔']


# 列表相关操作
# 列表.clear ()  清空指定列表
list1 = [1, 2, 3, 5, 6, 4]
list1.clear()
print(list1)  # []

# 列表.copy()  复制指定列表,产生一个一模一样的新列表(地址不同)
list1 = [1, 2, 3, 5, 6, 4]
list1_copy = list1.copy()
print(list1_copy)  # [1, 2, 3, 5, 6, 4]
print(id(list1), id(list1_copy))  # 两个列表的地址不同
print(id(list1[0]), id(list1_copy[0]))  # 两个列表中的元素地址一样、

# 列表.count() 统计指定元素在列表中出现的次数

names1 = ['张三', '李四', '大黄', '张三']
print(names1.count('张三'))  # 2

# 列表.extend(序列) 将序列中所有的元素全部添加在列表中
names1 = ['张三', '李四', '大黄', '张三']
names1.extend(['1', '2'])
print(names1)    # ['张三', '李四', '大黄', '张三', '1', '2']

# 列表.index(元素)  获取指定元素在列表中的下标,正向的值,如果元素不存在会报错

names1 = ['张三', '李四', '大黄', '张三']
print(names1.index('大黄'))  # 2
print(names1.index('张三')) # 0  如果元素有多个,只去其中第一个的下标值

# 列表.reverse()  列表倒序
names1 = ['张三', '李四', '大黄', '张三']
names1.reverse()
print(names1)        # ['张三', '大黄', '李四', '张三']

# 列表.sort   将列表元素从小到大排序,不会产生新的列表
list1 = [1, 9, 3, 5, 6, 4]
list1.sort()
print(list1)   # [1, 3, 4, 5, 6, 9]
# 列表.sort(reverse=True)
list1.sort(reverse=True)
print(list1) # [9, 6, 5, 4, 3, 1]

re=list1.sort()
print(list1.sort()) # None(表示没有)






1. Addition and multiplication of
lists Addition: List 1 + List 2-Combine list 1 and list 2 to create a new list.
List can only be added to list

Multiplication: List N/N List ---- Repeat the elements in the list N times to generate a new list


list1 = [100, 1, '11']
list2 = [13, 25, '13']
list3 = list1 + list2
print(list3)  # [100, 1, '11', 13, 25, '13']

list4 = list1 * 3
print(list4)  # [100, 1, '11', 100, 1, '11', 100, 1, '11']

'''
比较运算
两个数字列表之间支持比较大小相等
比较第一队不相等的数字的大小,谁大对应的列表就大

比较相等
元素一样,顺序不一样的列表不相等
'''
list1 = [100, 1, '11']
list2 = [13, 25, '13']
print(list1 > list2)  # True

list1 = [100, 1, '11']
list2 = [100, '11', 1]
print(list1 == list2)  # False

'''
in 和 not in
元素 in 列表 --判断列表中是否存在指定的元素
元素 not in -- 判断列表中是否不存在指定的元素

'''
list1 = [100, [1, 2]]
print(1 in list1)  # False
print(1 not in list1)  # True
print([100, [1, 2]] in list1)  # False
print([1, 2] in list1)  # True

# 练习:获取两个列表中公共元素
a = [1, 2, 5, 10, 3, 2]
b = [5, 4, 3, 2, 1]
c = []

for i in a:
    if i in b and i not in c:
        c.append(i)
print(c)

# 相关函数
'''
sum,max,min,sorted,len,list
'''
a = [1, 2, 3, 5, 6, 4]
# sum(数字列表)-求所有元素的和 21
print(sum(a))

# max(列表)/min(列表) 求列表中最大值和最小值,注意元素的类型必须一致,且支持比较运算
print(max(a), min(a))

# sorted(列表) 将列表中的元素从小到大排序(升序),产生一个新的列表,不会修改原列表
new_lista = (sorted(a))
print(new_lista)  # [1, 2, 3, 4, 5, 6]
print(a)  # [1, 2, 3, 5, 6, 4]

# sorted(列表,reverse=True) 将列表中的元素从大到小排序(降序),产生一个新的列表,不会修改原列表


new_lista = (sorted(a, reverse=True))
print(new_lista)  # [6, 5, 4, 3, 2, 1]
print(a)  # [1, 2, 3, 5, 6, 4]

# len(列表) 获取列表长度--指列表中元素的个数
print(len(a))  # 6

# list(数据)
'''
将指定数据准换成列表,数据必须得是序列(容器型数据类型)
转换的时候直接将序列中的元素作为新的列表元素
'''
print(list('231'))  # ['2', '3', '1']
print(list(range(4)))  # [0, 1, 2, 3]



Guess you like

Origin blog.csdn.net/weixin_44628421/article/details/108844966