Home  >  Article  >  Backend Development  >  About the index value problem of Python list

About the index value problem of Python list

WBOY
WBOYforward
2022-09-13 17:27:293810browse

【Related recommendations: Python3 video tutorial

The index value of the list

1. Index of the list

Like strings, each element in the list also has its own number, and this number is the index of the list.

2. List index value

Through string index, we can take out the elements in the string.

Through list index, we can take out the elements of the list.

[Syntax]

  • The list name or the list itself.
  • English square brackets.
  • index.

3. Positive index value of the list

# 新建一个str列表
str_list = ["当归", "人参",  "黄芪"]

print(str_list[0])
print(str_list[1])
print(str_list[2])

[Terminal output]

Angelica
Ginseng
Astragalus

The positive index is numbered from left to right, and the numbering starts from 0!

str_list[0]Get the first element of the listAngelica.
str_list[1]Get the second element of the listGinseng.
str_list[2]Get the third element of the listAstragalus.

4. Negative index value of the list

# 新建一个str列表
str_list = ["当归", "人参",  "黄芪"]

print(str_list[-1])
print(str_list[-2])
print(str_list[-3])

[Terminal output]

Astragalus
Ginseng
Angelica

Negative indexes are numbered from right to left, starting from -1!

str_list[-1]Get the penultimate element of the list黄芊.
str_list[-2]Get the penultimate element of the listGinseng.
str_list[-3]Get the third to last element of the listAngelica.

Negative index values ​​are usually used when you don’t know how many elements there are in the list, or when there are too many elements in the list, and you need to get the nth element from the bottom.

[Warm Tips]

It is important to distinguish that positive indexes start numbering from 0, while negative indexes start numbering from -1.

5. Use the list itself to get the value

print(["当归", "人参",  "黄芪"][2])

[Terminal output]

黄芪

The above code is directly from a list Get the 3rd element of the list.

This method is rarely used, just understand it.

6. Get values ​​from nested lists

[Syntax]

List[Index][Index]

When When you need to get a value from a list within a list, that is, when you need to get a value from two indexes, the syntax is list[index][index].

A nested list means that there is a list within the list, that is, the data type of one or several elements of the list is a list.

  1. List [Index]Remove the element containing the ID number from the list:
# 新建一个stu列表
stu_list = ["白敬亭",   ["联系方式", 19987658765] , ["身份证号",  533001199101023456]]

# 用正索引取出列表的第3个元素
print(stu_list[2])

# 用负索引取出列表的倒数第1个元素
print(stu_list[-1])

# 查看取到的元素的数据类型
print("取到的元素数据类型为",type(stu_list[2]))
print("取到的元素数据类型为",type(stu_list[-1]))

[Terminal output]

['ID card number', 533001199101023456]
['ID card number', 533001199101023456]
The element data type obtained is a12f8764f17af704951bb70a4fbdc029
The element data obtained The type is a12f8764f17af704951bb70a4fbdc029

Observe the above results, the data type of the obtained element is listlist.

But this is not the final result I want. I still want to continue to take out his ID number. How should I do this?

2. List [index][index]Get the list elements in the list:

# 新建一个stu列表
stu_list = ["白敬亭",   ["联系方式", 19987658765] , ["身份证号",  533001199101023456]]

# 用正索引取出列表的第3个元素,然后取出第3个元素中的第2个元素
print(stu_list[2][1])

# 用负索引取出列表的倒数第1个元素,然后取出倒数第1个元素中的倒数第1个元素
print(stu_list[-1][-1])

# 查看取到的元素的数据类型
print("取到的元素数据类型为",type(stu_list[2][1]))
print("取到的元素数据类型为",type(stu_list[-1][-1]))

[Terminal output]

533001199101023456
533001199101023456
The data type of the element obtained is 09d8d99b70abac08606bcc27aa231644
The data type of the element obtained is 09d8d99b70abac08606bcc27aa231644

stu_list[2]The third element in the list stu_list is obtained, which is ['ID card number', 533001199101023456].

[2]

in stu_list[2][1] first gets ['ID number', 533001199101023456]. The

[1]

after gets the second element in ['ID number', 533001199101023456], which is 533001199101023456.

The data type obtained is integer.

7. After-class exercises

1. There is an umbrella, mobile phone, and wallet in my schoolbag; there are bank cards, bus cards, and meal cards in the wallet.

[Question Requirements]

  • Use a list to store the items in the schoolbag.
  • Use a list to store items in the wallet.
  • Write code to remove the bus card from the school bag list.

【Reference Answer】

# 新建一个钱包列表
qianbao = ["银行卡", "公交卡", "饭卡"]

# 新建一个书包列表
shubao = ["雨伞", "手机", qianbao]

# 从书包中取出第3个元素qianbao,然后取出qianbao中的第2个元素公交卡
print(shubao[2][1])

【Terminal Output】

Bus Card

In order to facilitate everyone’s understanding, the above I use Chinese for both the list name and the elements in the list.

2. Take out 19987658765 from the contact information in the list below.

# 新建一个stu列表
stu_list = ["白敬亭",   ["联系方式", [19987658765, 'xyz77520520']] , ["身份证号",  533001199101023456]]

【Reference answer】

# 新建一个stu列表
stu_list = ["白敬亭",   ["联系方式", [19987658765, 'xyz77520520']] , ["身份证号",  533001199101023456]]

# 正索引取值
print(stu_list[1][1][1])

# 负索引取值
print(stu_list[-2][-1][-1])

# 查看取到的元素的数据类型
print("取到的元素数据类型为",type(stu_list[1][1][1]))
print("取到的元素数据类型为",type(stu_list[-2][-1][-1]))

【Terminal output】

xyz77520520
xyz77520520
The data type of the element obtained is 3a628129c34d878453c4dde2ff442b59
The data type of the element obtained is 3a628129c34d878453c4dde2ff442b59

The above is a three-level list nesting (named by myself). You can write more complex and multi-level list nesting value programs to experience the multi-index value collection of the list. There will be errors. You can take a screenshot or send me the source code.

8. Summary

The index value of the list has the same syntax as the string value. Note that the positive index number starts from 0.

This section focuses on mastering the syntax of multiple index values: List[index][index].

List value programming is often used and needs to be mastered.

【Related recommendations: Python3 video tutorial

The above is the detailed content of About the index value problem of Python list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete