List comprehension, list length, list subscript and slice, list addition, deletion, change check, maximum number, minimum number and sum, list subscripted traversal, bubble sorting

1. Find the length of the list

The len() function returns the length of the list, so I won’t demonstrate it

2. List subscript and slice change check

The list can also find data based on the subscript, and can also find the content of the corresponding length based on the slice. For details, see the common operations of strings , but different from the string, the list can also modify its own data according to the subscript and slice , please see below Demo:

name1 = ['张三', '李四', '马武', '帅哥']
name1[1] = 1
print(name1) # ['张三', 1, '马武', '帅哥']
# name1[1] = 1,2,2 # 如果是像这样的用逗号连起来的多个数据,则会把这几个数据拼成一个元组
# print(name1) # ['张三', (1, 2, 2), '马武', '帅哥']

The above is to modify the data according to the subscript, and the following is to modify the data according to the slice. The slice to modify the data is to replace the content of the cut with the given content. The content that can be given here is ( list, tuple, set (only add key) Value), range(), a comma should be added to a single data ), so slices can also be used to add data to a list. The slice assignment is similar to the extend method of that list :
note that name1[n:n] does not get data. Yes, what you get is just an empty list, so you can use this slicing method to add one or more data in one location

# 是一句一句执行的,就不一一打印了
name1 = ['张三', '李四', '马武', '帅哥']
# name1[1:3] = range(5) ['张三', 0, 1, 2, 3, 4, '帅哥']
# name1[1:3] = (1,2,3) # ['张三', 1, 2, 3, '帅哥']
# name1[1:3] = [1,2,3,4] # ['张三', 1, 2, 3, 4, '帅哥']
# name1[1:3] = 1, # ['张三', 1, '帅哥']
# name1[1:3] = {'name':'zhangsan','age':18} # ['张三', 'name', 'age', '帅哥']
print(name1)

3. Adding, deleting and checking the list

Zha : index and the string index method as see the specific character string common operation , returned to match the corresponding index, no error will, not here demonstrates;
by : the append INSERT Extend
the append (Object) : list Add a piece of data to the end of the. This data can be a list, tuple, dictionary, single element, etc.;
insert(index,object) : add data at the specified position. The data you can add is the same as append;
extend(iterable) : in Concatenate iterable objects, lists, tuples, sets (only add key value), range(), etc. at the end of the list;

hero = ['后裔', '鲁班', '孙尚香', '百里守约', '伽罗']
hero.append(1)  # 在列表的末尾加入1
print(hero) # ['后裔', '鲁班', '孙尚香', '百里守约', '伽罗', 1]

hero.insert(0,2) # 在下标为0处插入2
print(hero) # [2, '后裔', '鲁班', '孙尚香', '百里守约', '伽罗', 1]

# 下面也是一条一条执行的,添加列表的我就不演示了
# hero.extend({'name':'zhangsan','age':18}) # [2, '后裔', '鲁班', '孙尚香', '百里守约', '伽罗', 1, 'name', 'age']
# hero.extend(('a','b','c')) # [2, '后裔', '鲁班', '孙尚香', '百里守约', '伽罗', 1, 'a', 'b', 'c']
# hero.extend(range(2)) # [2, '后裔', '鲁班', '孙尚香', '百里守约', '伽罗', 1, 0, 1]
print(hero)

Delete: pop remove clear
pop(index ): When the parameter index is not added, the default is to delete the last data. If you add it, you delete the data according to the subscript you added. The deleted data is the return value of this method;
remove(object ) : Delete the original data in the list, the content to be deleted must be in the original list, otherwise an error will be reported;
clear() : Clear the list;
you can also use del to delete

fashi = ['小乔', '貂蝉', '张良', '干将', '妲己']
a = fashi.pop(0)  # pop默认删除最后一个数据,并且返回这个数据,还可以根据下标删除数据
print(fashi)# ['貂蝉', '张良', '干将', '妲己']
print(a)# '小乔'

b = fashi.remove('干将')
print(fashi) ['貂蝉', '张良', '妲己']
# fashi.remove('大桥') # 如果列表里面没有这个数据,就会报错 ValueError: list.remove(x): x not in list

fashi.clear()
print(fashi) # []

nums = [1,2,1,3,4,1,2]
del nums[0]
print(nums)# [2, 1, 3, 4, 1, 2]
del nums[3:]
print(nums)# [2, 1, 3]

4. Find the maximum and minimum numbers of the list, and sum the list

Max() seeks the maximum number, min() seeks the minimum number, sum() sums the list and seeks the
maximum and minimum values. It is necessary to ensure that the data types in the list are the same. The sum summation seems to only support numeric types. You can try it. I tried the string is not working

num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 5, 4]
print(max(num))# 8
print(min(num))# 0
print(sum(num))# 45

5. List traversal

I will only demonstrate one kind of list traversal, which is the traversal with subscript

killers = ['李白', '韩信', '猴子', '橘右京', '娜可露露']
for x,y in enumerate(killers):
    print(x,y)
# 0 李白
# 1 韩信
# 2 猴子
# 3 橘右京
# 4 娜可露露

Extension: methods such as count sort reverse

count : I won’t talk about it. The usage method is the same as the string count. See common string operations.
sort : sort the list, the default is ascending, you can modify the parameter reverse=True to change to descending order;
reverse : perform the list Reverse

nums = [5, 3, 1, 6, 7, 8, 4, 2]
nums.sort()
print(nums) # [1, 2, 3, 4, 5, 6, 7, 8]
nums.reverse() 
print(nums) # [8, 7, 6, 5, 4, 3, 2, 1]

Bubble Sort

num = [6, 5, 3, 1, 8, 7, 2, 4]
b = 0
count = 0 # 统计排序的次数
while b < len(num) - 1:
    flag = True
    a = 0
    while a < len(num) - 1 -b:
        count += 1
        if num[a] > num[a + 1]:# 前一个比后一个大,就交换两个位置
            flag = False # 只要进行了一次换位,下面的if语句就不会执行
            num[a], num[a + 1] = num[a + 1], num[a]
        a += 1
    if flag:# 
        break
    b +=1
print(num)# [1, 2, 3, 4, 5, 6, 7, 8]
print(count)# 27

Hope to help you
Like you don't get lost
Hehehe

Guess you like

Origin blog.csdn.net/hmh4640219/article/details/111870783