Home  >  Article  >  Backend Development  >  Things to take stock of the list of Python basics

Things to take stock of the list of Python basics

Go语言进阶学习
Go语言进阶学习forward
2023-07-25 16:05:49824browse

1. List format

Example:

   namesList = ['xiaoWang','xiaoZhg','xiaa']

is more powerful than the C language array The point is that the elements in the list can be of different types.

   testList = [1, 'a']

2. List related operations ("add", "delete", "modify", "check" )

f35d6e602fd7d0f0edfa6f7d103c1b57 Add elements

append() You can add elements to the list through append.

Example:

# 定义变量A,默认有3个元素
A = ['rr', 'rag', 'rte']


print("-----添加之前,列表A的数据-----")
for tempName in A:
    print(tempName)


# 提示、并添加元素
temp = input('请输入要添加的学生姓名:')
A.append(temp)


print("-----添加之后,列表A的数据-----")
for tempName in A:
    print(tempName)

Run result:

Things to take stock of the list of Python basics

2cc198a1d5eb0d3eb508d858c9f5cbdb Modify elements ("change")

When modifying elements, you must use the subscript to determine what you want to modify. which element it is before it can be modified.

Example:

# 定义变量A,默认有3个元素
A = ['rr', 'rag', 'rte']
print("-----修改之前,列表A的数据-----")
for tempName in A:
    print(tempName)


# 修改元素
A[1] = 'Lu'


print("-----修改之后,列表A的数据-----")
for tempName in A:
    print(tempName)

Result:

Things to take stock of the list of Python basics

5bdf4c78156c7953567bb5a0aef2fc53 查找元素("查"in, not in, index, count)

python中查找的常用方法为:

  1. in(存在),如果存在那么结果为true,否则为false。

  2. not in(不存在),如果不存在那么结果为true,否则false。


 #待查找的列表    A = ['rr', 'rag', 'rte']
    #获取用户要查找的名字    findName = input('请输入要查找的内容:')
    #查找是否存在    if findName in A:        print('在字典中找到了相同的内容')    else:        print('没有找到')

运行结果:(找到)

Things to take stock of the list of Python basics

运行结果:(没有找到)

Things to take stock of the list of Python basics

注:

    in的方法只要会用了,那么not in也是同样的用法,只不过not in判断的是不存在。

23889872c2e8594e0f446a471a78ec4c 删除元素("删"del, pop, remove)

  1. del (根据下标进行删除)


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

结果:

Things to take stock of the list of Python basics

  1. pop(删除最后一个元素)

Subject= ['数学', '语文', '英语', '地理', '历史']
print('------删除之前------')for tempSubject in Subject:    print(tempSubject)
del Subject[2]  #删除第二个元素
print('------删除之后------')for tempSubject in Subject:    print(tempSubject)

运行结果:

Things to take stock of the list of Python basics

  1. remove (根据元素的值进行删除)


Subject= ['数学', '语文', '英语', '地理', '历史']
print('------删除之前------')for tempSubject in Subject:    print(tempSubject)
# del Subject[2]  #删除第二个元素Subject.remove('英语')

print('------删除之后------')for tempSubject in Subject:    print(tempSubject)

结果:

Things to take stock of the list of Python basics

43ad812d3a971134e40facaca816c822 排序(sort, reverse)

sort方法是将list按特定顺序重新排列,默认为由小到大,参数reverse=True可改为倒序,由大到小。

reverse方法是将list逆置。

a = [1, 4, 2, 3]print(a)
a.reverse()print(a) # 运行结果a.sort()print(a)  # 运行结果a.sort(reverse=True)print(a)  # 运行结果

运行结果:

Things to take stock of the list of Python basics


三、列表的嵌套

1. 列表嵌套

类似while循环的嵌套,列表也是支持嵌套的。

一个列表中的元素又是一个列表,那么这就是列表的嵌套。

例:

Letter= [['A', 'B'],         ['C', 'D', 'E'],         ['F', 'R']]

2. 字典列表

列表中包含字典。比如花名册:

pep1 = {'name': '蔡同学', 'school': '北京大学'}pep2 = {'name': '陈作同', 'school': '中山大学'}pep_list = [pep1, pep2]for pepo in pep_list:    print(pepo)

运行结果:

Things to take stock of the list of Python basics

有的应用场景,会在列表中包含大量的字典, 而且其中的每个字典都会包含拥有众多属性的大对象。

3. 列表字典

字典包含列表。比如图书的标签,一本书会被标注多个标签:

book = {'title': '现代艺术150年',        'tags': ['数学', '历史学']}for tags in book['tags']:    print(tags)

运行结果:

Things to take stock of the list of Python basics


四、总结

    本文详细的讲解了Python基础 。介绍了常见的列表操作,以及在实际操作中会遇到的问题,提供了解决方案。最后通过一个小项目,使读者能够更好的理解Python列表的使用方法。希望可以帮助你更好的学习。

The above is the detailed content of Things to take stock of the list of Python basics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Go语言进阶学习. If there is any infringement, please contact admin@php.cn delete