Things to take stock of the list of Python basics

Release: 2023-07-25 16:05:49
forward
1009 people have browsed it

1. List format

Example:

   namesList = ['xiaoWang','xiaoZhg','xiaa']
Copy after login

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

   testList = [1, &#39;a&#39;]
Copy after login

2. List related operations ("add", "delete", "modify", "check" )<br/>

<1> Add elements

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

Example:

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


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


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


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

Run result:<br/>

Things to take stock of the list of Python basics

<2> Modify elements ("change")<br/>

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 = [&#39;rr&#39;, &#39;rag&#39;, &#39;rte&#39;]
print("-----修改之前,列表A的数据-----")
for tempName in A:
    print(tempName)


# 修改元素
A[1] = &#39;Lu&#39;


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

Result:<br/>

Things to take stock of the list of Python basics

<3> 查找元素("查"in, not in, index, count)<br/>

python中查找的常用方法为:

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

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

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

运行结果:(找到)<br/>

Things to take stock of the list of Python basics

运行结果:(没有找到)

Things to take stock of the list of Python basics

注:<br/>

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

<4> 删除元素("删"del, pop, remove)

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

<br/>
Copy after login
Copy after login
Copy after login
 Name = [&#39;加勒比海盗&#39;,&#39;骇客帝国&#39;,&#39;第一滴血&#39;,&#39;霍比特人&#39;,&#39;速度与激情&#39;]
print(&#39;------删除之前------&#39;)for tempName in Name:    print(tempName)
del Name[2]
print(&#39;------删除之后------&#39;)for tempName in Name:    print(tempName)
Copy after login

结果:<br/>

Things to take stock of the list of Python basics

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

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

运行结果:

Things to take stock of the list of Python basics

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

<br/>
Copy after login
Copy after login
Copy after login
Subject= [&#39;数学&#39;, &#39;语文&#39;, &#39;英语&#39;, &#39;地理&#39;, &#39;历史&#39;]
print(&#39;------删除之前------&#39;)for tempSubject in Subject:    print(tempSubject)
# del Subject[2]  #删除第二个元素Subject.remove(&#39;英语&#39;)

print(&#39;------删除之后------&#39;)for tempSubject in Subject:    print(tempSubject)
Copy after login

结果:<br/>

Things to take stock of the list of Python basics

<5> 排序(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)  # 运行结果
Copy after login

运行结果:<br/>

Things to take stock of the list of Python basics

<br/>

三、列表的嵌套<br/>

1. 列表嵌套

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

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

例:

Letter= [[&#39;A&#39;, &#39;B&#39;],         [&#39;C&#39;, &#39;D&#39;, &#39;E&#39;],         [&#39;F&#39;, &#39;R&#39;]]
Copy after login

2. 字典列表<br/>

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

pep1 = {&#39;name&#39;: &#39;蔡同学&#39;, &#39;school&#39;: &#39;北京大学&#39;}pep2 = {&#39;name&#39;: &#39;陈作同&#39;, &#39;school&#39;: &#39;中山大学&#39;}pep_list = [pep1, pep2]for pepo in pep_list:    print(pepo)
Copy after login

运行结果:<br/>

Things to take stock of the list of Python basics

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

3. 列表字典

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

book = {&#39;title&#39;: &#39;现代艺术150年&#39;,        &#39;tags&#39;: [&#39;数学&#39;, &#39;历史学&#39;]}for tags in book[&#39;tags&#39;]:    print(tags)
Copy after login

运行结果:

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!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template