程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to query data after Python lists are nested (code example)

編輯:Python

The so-called list nesting means that a list contains other sub lists .

Application scenarios :

To store class one 、 Two 、 Names of students in three classes , And the names of the students in each class are in a list .

Such as , This is a large list, which contains three small lists , Each small list is a class , According to the basic syntax of the list, each data is separated by a comma :

name_list = [[' The small white ', ' indigo plant ', ' Xiaohong '], [' Wang Wu ', ' Li Si ', ' Zhang San '], ['Python', 'Java', 'Php']]

List nested data query

step : 

First step : Find the sub list containing a certain data according to the subscript

The second step : From the list of words found , Then find the final desired data according to the subscript

Code experience :

name_list = [[' The small white ', ' indigo plant ', ' Xiaohong '], [' Wang Wu ', ' Li Si ', ' Zhang San '], ['Python', 'Java', 'Php']]
print(name_list) # result [[' The small white ', ' indigo plant ', ' Xiaohong '], [' Wang Wu ', ' Li Si ', ' Zhang San '], ['Python', 'Java', 'Php']]
# Data query when the list is nested
# Get the first word list
print(name_list[0]) # result :[' The small white ', ' indigo plant ', ' Xiaohong ']
# Get the second data in the first sub list ---- Think of the first word list as a list name, just write a subscript after it
print(name_list[0][1]) # result : indigo plant 

Execution results as shown :

more Python For learning articles, you can go to Python Self study network Search for , There are not only basic knowledge articles, but also many examples for you to practice , Want to learn systematically Python The whole stack course It's OK .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved