Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Application case sharing of list in Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report