python- list - sorted containers

Test sites

如何定义一个空列表,请使用两种方式实现
[]
list()

列表数据的类型,用英语怎么拼写
list

有一个列表name_list,请获取索引值为3的数据
name_list[3]

给列表name_list,添加一个数据 "诸葛亮"
name_list.append("诸葛亮")

删除列表name_list中,索引值为2的数据
name_list.pop(2)

删除列表name_list中,值为"夏候渊"的数据
name_list.remove("夏候渊")


List, sorted containers

Container, there are storage functions

For the python in the container, storing it python various data types such as integer, float, string, list

Ordered understand

Here Insert Picture Description

Definition list

Mark list is []

Type list, words are, list

The definition of an empty list, there are two ways

1 embodiment, is directly defined by the list of symbols

变量 = []

2 embodiment, is defined by the type name

变量 = list()

Definition of the content of the list

变量 = [数据1,数据2,数据n]

E.g:

xm_list = ["张三", "李四", "王五"]

Example, data definition charts

List defines the name of the leaderboard

# 刘一 陈二 张三 李四 王五 赵六 孙七 周八 吴九 郑十
xm_list = ["刘一", "陈二", "张三", "李四", "王五", "赵六", "孙七", "周八", "吴九", "郑十"]
print(xm_list)

About count

  • NATURAL counting, from the beginning

  • A program counter data, start number counting from 0

Index and list of values

The list is ordered, sorted list counting from zero, each location would correspond to a number of data, we this position is called the order value index

By the index value, it can correspond to a particular member of the list

The list index position, the value of the format

Variable = list [index]

In this way, to get a data

A list of values

值 = 列表[索引值]

In both cases the square brackets

  • Define a list of cases
    []

  • The value of the container case of
    variable [5]

List of application scenarios

# 通常情况,我们用列表来保存相同的内容,更多的情况
name_list = ["张三","李四","王五"]

# 列表也可以用于记录一串信息
lisi_info = ["李四", 18, "武汉"]

List index upper limit number of data relationships

NATURAL counting method, the number is 3 out of a list of data

Then the index limit the list to only 2

NATURAL counting method, the number of data out of 4

Index can only limit to the number 3

law:

A list of its index value = the upper limit of the number list data --1

Add a list of data

To finish adding data through the append method of the list

列表.append(数据)

Effect, which in the original list, most are not bit, add the data

Here Insert Picture Description

Here Insert Picture Description

Delete the list of data

Here Insert Picture Description

  • Press the Delete Index

format

列表.pop(索引值)

Xiao Ming, please help me put the number of index values ​​(data) 1 out (small bookshelf in a book)

Here Insert Picture Description

book_list = ["三国演义", "水浒传", "西游记"]

# 按照索引来删除,pop方法
# 列表.pop(索引值),pop理解为弹出
book_list.pop(0)
print(book_list)
book_list.pop(1)
print(book_list)
  • Press delete data

Xiao Ming, please help me, "Water Margin" to take over the book

Here Insert Picture Description

format

列表.remove(数据)

book_list = ["三国演义", "水浒传", "西游记"]

# 按照数据来删除
# 列表.remove(数据)
print('删除前', book_list)
book_list.remove("西游记")
print('删除后', book_list)

Guess you like

Origin blog.csdn.net/ifubing/article/details/94330167