Python basics-05 list

Basic use of lists

  • When we have multiple data that need to be saved in a certain order, we can consider using a list
  • Use [] to represent a list, and each data in the list is called an element
  • Elements are separated by commas
names = ['zhangsan','lisi','wangwu','ermazi','dazhutou']
# 可以使用list(可迭代对象)将可迭代对象转换成一个列表
print(list(('aaa','bbb','ccc','ddd')))  # ['aaa', 'bbb', 'ccc', 'ddd']

# 和字符串一样,列表可以使用下标来获取元素和对元素进行切片
print(names[3])  # ermazi
print(names[3:6])  # ['ermazi', 'dazhutou']

# 同时,可以使用下标来修改列表里面的元素
names[3] = 'erliuzi'
print(names)  # ['zhangsan', 'lisi', 'wangwu', 'erliuzi', 'dazhutou']

List operations

  • The list is used to save multiple data, which is orderly and variable
  • The operation list generally includes adding, deleting, modifying and checking

increase

  • The method of adding elements append insert extend
names = ['zhangsan','lisi','wangwu','ermazi','dazhutou']

names.append('liuziheng')  # append在列表的最后追加一个数据
print(names)  # ['zhangsan', 'lisi', 'wangwu', 'ermazi', 'dazhutou', 'liuziheng']

names.insert(2,'zhuzhuzhu')  # insert 在指定的索引之前插入一个数据
print(names)  # ['zhangsan', 'lisi', 'zhuzhuzhu', 'wangwu', 'ermazi', 'dazhutou', 'liuziheng']

other_names = ['aaa','bbb','ccc']
names.extend(other_names)  # extend 在指定的列表后拼接一个可迭代对象
print(names)  # ['zhangsan', 'lisi', 'zhuzhuzhu', 'wangwu', 'ermazi', 'dazhutou', 'liuziheng', 'aaa', 'bbb', 'ccc']

delete

  • The method of deleting elements pop remove clear
names = ['zhangsan','lisi','wangwu','ermazi','dazhutou']

x = names.pop()  # pop 方法可以根据传入的index参数删除并返回列表中的元素,默认参数是最后一个
print(x)  # dazhutou
print(names)  # ['zhangsan', 'lisi', 'wangwu', 'ermazi']

names.remove('lisi')  # remove 方法根据传入的参数删除指定的元素,如果元素不存在,则报错
print(names)  # ['zhangsan', 'wangwu', 'ermazi']

names.clear()  # clear 方法用来清空一个列表
print(names)  # []

change

  • Use subscripts to directly modify elements in the class table

check

  • The method index in to find the element
names = ['zhangsan','lisi','wangwu','ermazi','dazhutou']

x = names.index('zhangsan')  # index 返回元素在列表中的下标,元素不存在则报错
print(x)  # 0

x  = ('lisi' in names)  # in 判断元素是否在列表中,是返回True,否返回False
print(x)  # True

list traversal

  • Traversal: access all the data once. Traversal is for iterable objects
  • while loop traverses
  • for...in loop through
names = ['zhangsan','lisi','wangwu','ermazi','dazhutou']

# while循环,通过访问每一个元素的下标来访问每一个数据
i = 0
while i < len(names):
    print(names[i])
    i += 1


# for..in循环,本质是不断的调用next方法查找下一个数据
for i in names:
    print(i)

Bubble Sort

  • The list can be sorted directly using the sort built-in method
n = [6,5,3,1,8,7,2,4]

x = 0
while x < 7:
    x += 1
    i = 0
    while i < len(n) -1:
        if  n[i] >  n[i+1]:
            n[i],n[i+1] = n[i+1],n[i]
        i += 1

print(n)

list sort reverse

  • Lists can be sorted using the built-in method sort
  • Using the built-in function sorted will not change the original list data, but will generate a new sorted data
  • Use the reverse method, which is equivalent to [::-1], to reverse the list
names = ['zhangsan','lisi','wangwu']

names.sort()  # 排序
print(names)  # ['lisi', 'wangwu', 'zhangsan']
names.sort(reverse=True)  # reverse 关键字,默认False,True代表倒序排列
print(names)  # ['zhangsan', 'wangwu', 'lisi']

# sorted
names = ['zhangsan','lisi','wangwu']
x = sorted(names)
print(names)  # ['zhangsan', 'lisi', 'wangwu']
print(x)  # ['lisi', 'wangwu', 'zhangsan']

# reverse
names = ['zhangsan','lisi','wangwu']
names.reverse()
print(names)  # ['wangwu', 'lisi', 'zhangsan']

names = ['zhangsan','lisi','wangwu']
print(names[::-1])  # ['wangwu', 'lisi', 'zhangsan']

Mutable and immutable data types

  • Data in Python is stored in memory
  • Data in Python is divided into mutable and immutable types
  • Immutable types:
    • string
    • number
    • tuple
  • mutable type
    • the list
    • dictionary
    • gather
  • If the value of immutable data is modified, the memory address will change
  • Variable type of data, if the value is modified, the memory address will not change

copy of list

  • Call the copy method to copy a list
  • This new list has the same content as the original list, but points to a different memory space
  • In addition to using the built-in copy method, you can also use the copy method in the cpoy module, and the effect is the same
  • A slice is actually a shallow copy

deep copy and shallow copy

import copy

# 浅拷贝
nums = [1,2,3,4,5,6]

nums1 = nums  # 赋值(不属于深/浅拷贝)

nums2 = nums.copy()  # 浅拷贝
nums3 = copy.copy(nums)  # 浅拷贝

# 深拷贝,只能用copy模块实现
words = ['hello','good',[100,200,300],'yes']

words1 = words.copy()
words[0] = '你好'
print(words1)  # ['hello', 'good', [100, 200, 300], 'yes']

words[2][0] = 1
print(words1)  # ['hello', 'good', [1, 200, 300], 'yes']
# 浅拷贝可以认为只拷贝了一层

words = ['hello','good',[100,200,300],'yes']
words2 = copy.deepcopy(words)  # 深拷贝
words[2][0] = 1
print(words2)  # ['hello', 'good', [100, 200, 300], 'yes']

Supongo que te gusta

Origin blog.csdn.net/Lz__Heng/article/details/130124812
Recomendado
Clasificación