Get the App
SLTechnology News&Howtos  ›  Development  › 

Application case sharing of list in Python

Shulou Source: shulou.com Published: 2022-06-03 14:50:41 10月11日 Update

This article mainly explains "the application case sharing of the list in Python". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the application case sharing of the list in Python".

1. Application of list

(1) queue

Features: first in, first out

Usage scenarios: bank queue, inventory, seconds kill, queue for meals, callcenter

Append () + pop (0)

(2) Stack

Features: first in and then out

Usage scenario: get out of the elevator

Append () + pop ()

two。 Case list

Eg1: make a task list, using queue, FIFO, when the user enters do, start to do the task and pop up the done task.

Note: using the first-in-first-out principle, use the pop function of the list to delete pop (0) each time.

#! / usr/bin/pythontask = [] while True: task_name = input ("Please enter the name of the task you want to enter:") if task_name = = "do": if len (task) = 0: print ("Task is empty, exit the program") break else: print (task.pop (0)) else: task.append (task_name)

Execution result

Eg2: to find the intersection of two lists and remove the duplicates

Note: the method of determining whether an element is in the list is used

#! / usr/bin/pythonnum1 = [1, if i in num2, 5, 5, 6, 3] num2 = [3, 5, 5, 3] num3 = [] for i in num1: if i in num2: if i not in num3: num3.append (I) print (num3)

Execution result:

Eg3: sort a list with the largest number at the end

Note: you need an intermediate variable to store the larger one and then swap it.

#! / usr/bin/pythonnum = [1BI 6pm 4J 5J 10] I = 0j = 1while j

< len(num): if num[i] >

Num [j]: Z = num [I] num [I] = num [j] num [j] = z I + = 1 j + = 1 else: Z = num [j] I + = 1 j + = 1print (num)

Execution result:

Thank you for your reading, the above is the content of "the application case sharing of the list in Python". After the study of this article, I believe you have a deeper understanding of the application case sharing of the list in Python, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Applications tasks cases advanced results first-out learning input content scenarios ways characteristics queues maximum two intersection elements functions principles variables Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Redmi MariaDB Huawei Docker