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

How to use list in Python

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

Share

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

Editor to share with you how to use the list in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The sequence structures commonly used in python are: string, list, tuple, dictionary, collection.

1 list introduction

List: used to store any number of data sets of any type.

A list is a built-in variable sequence, an ordered and contiguous memory space containing multiple elements. Standard syntax format for list definitions:

A = [10, 10, 20, 30, 40]

Among them, 10, 20, 30, 40, these elements are called: list a.

The elements in the list can be different and can be of any type. For example: a = [10, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 20, 10, 10, 20, 10, 10, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 20, 10, 10, 10, 10, 10, 10, 10, 10, 10,

two。 Create a list

Basic syntax [] create > a = [10pc20 > a = [] # create an empty list object > a.append (20) > a [0] 20list () create # use list () to convert any iterable data into a list > a = list () # create an empty list object > > a = list (range (10)) > > a [0,1,2,3,4,5,6,7,8 9] > a = list ("gaoqi,sxt") > > a ['gathers,' asides, 'oaths,' qails, 'iTunes,', 'slots,' xbands,'t']

3. Add and delete

When the list adds and deletes elements, the list automatically manages memory, greatly reducing the burden on programmers. However, this feature involves a large number of movements of list elements and is inefficient.

Unless necessary, we usually only add or delete elements at the end of the list, which greatly improves the efficiency of the list.

> a = [20pr 40] > a.append (80) > a [20pr 40,80] 4. Deletion of list elements

Delete the element at the specified location in the list

> a = [100200888300400] > del a [1] > a [100200300400] 5. Sort the list > a = [20pint 10pint 30je 40] > id (a) 46017416 > > a.sort () # by default, it is in ascending order > > a = [10pence 20pct 30] > a.sort (reverse=True) # descending order > > a [40,30,20,10] > > import random > random.shuffle (a) # disrupt the order > > a [20,40,30,10] is all the contents of this article "how to use the list in Python"! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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