Article directory
List loop traversal
1. Use while loop
In order to more efficiently output each data of the list, you can use a loop to complete
namesList = ['xiaoWang','xiaoZhang','xiaoHua']
length = len(namesList) # 获取列表长度
i = 0
while i<length:
print(namesList[i])
i+=1
result:
xiaoWang
xiaoZhang
xiaoHua
2. Use a for loop
The while loop is a basic way of traversing list data, but the most common and easiest way is to use the for loop
namesList = ['xiaoWang','xiaoZhang','xiaoHua']
for name in namesList:
print(name)
result:
xiaoWang
xiaoZhang
xiaoHua
3. Swap the values of 2 variables
use intermediate variables
a = 4
b = 5
c = 0
c = a
a = b
b = c
print(a)
print(b)
practise
Manually implement bubble sort (difficult)
nums = [5, 1, 7, 6, 8, 2, 4, 3]
for j in range(0, len(nums) - 1):
for i in range(0, len(nums) - 1 - j):
if nums[i] > nums[i + 1]:
a = nums[i]
nums[i] = nums[i+1]
nums[i+1] = a
print(nums)
There is a list names, save a group of names names=['zhangsan','lisi','chris','jerry','henry'], and then let the user enter a name, if the name exists in the list, prompt The user name already exists; if the name does not exist in the list, the name is added to the list.
1. List nesting
Similar to nesting of while loops, lists also support nesting
The elements in a list are another list, then this is the nesting of lists
Here we focus on how to operate the nested list
schoolNames = [
[1, 2, 3],
[11, 22, 33],
[111, 222, 333]
]
schoolNames[1][2] # 获取数字 33
schoolNames[1][2] = 'abc' # 把 33 修改为 'abc'
schoolNames[1][2][2] # 获取 'abc' 里的字符c
That is to say, to operate nested lists, just use the subscript of the element to be operated as a variable name.
2. Application
A school has 3 offices, and now there are 8 teachers who are waiting for the allocation of seats. Please write a program to complete the random allocation
import random
# 定义一个列表用来保存3个办公室
offices = [[],[],[]]
# 定义一个列表用来存储8位老师的名字
names = ['A','B','C','D','E','F','G','H']
i = 0
for name in names:
index = random.randint(0,2)
offices[index].append(name)
i = 1
for tempNames in offices:
print('办公室%d的人数为:%d'%(i,len(tempNames)))
i+=1
for name in tempNames:
print("%s"%name,end='')
print("\n")
print("-"*20)
The result of the operation is as follows:
Advanced case
[Python] Python realizes the word guessing game-challenge your intelligence and luck!
[python] Python tkinter library implements GUI program for weight unit converter
[python] Use Selenium to get (2023 Blog Star) entries
Use Tencent Cloud Cloud studio to realize scheduling Baidu AI to realize text recognition
[Xiaobai must see] Python word cloud generator detailed analysis and code implementation
[Xiaobai must see] Python crawls an example of NBA player data
[Must-see for Xiaobai] Sample code for crawling and saving Himalayan audio using Python
[Xiaobai must see] Python crawler data processing and visualization
[Must-see for Xiaobai] Python crawler program to easily obtain hero skin pictures of King of Glory
[Must-see for Xiaobai] Use Python to generate a personalized list Word document
Geospatial data processing and visualization using geopandas and shapely (.shp)
Use selenium to crawl Maoyan movie list data
Detailed explanation of the principle and implementation of image enhancement algorithm Retinex
Getting Started Guide to Crawlers (8): Write weather data crawler programs for visual analysis
Getting started with crawlers (4): The best way to crawl dynamic web pages using Selenium and API
Getting Started Guide to Crawlers (3): Python network requests and common anti-crawler strategies
Getting started with crawlers (2): How to use regular expressions for data extraction and processing
Getting started with reptiles (1): Learn the basics and skills of reptiles
Python object-oriented programming basics and sample code
MySQL database operation guide: learn how to use Python to add, delete, modify and query operations
Python file operation guide: encoding, reading, writing and exception handling
Selenium Automation Toolset - Complete Guide and Tutorials
Python web crawler basics advanced to actual combat tutorial
Pandas data processing and analysis tutorial: from basics to actual combat
Detailed explanation of commonly used data types and related operations in Python
[Latest in 2023] Detailed Explanation of Six Major Schemes to Improve Index of Classification Model
Use 4 regression methods to draw prediction result charts: vector regression, random forest regression, linear regression, K-nearest neighbor regression
** [Learn python from zero] 18. Detailed explanation of basic operations of Python lists (1) **