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 the list in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use the list in Python. It is very detailed and has a certain reference value. Friends who are interested must finish it!

(1) create objects inside list / / list that can be strings, characters, numbers, and support mashup.

AList = ['apple', 100,0.01,' banana','A','B','C']

(2) access list / / directly through the subscript

> print (aList [0])

'apple'

(3) slicing of the list / / taking part of the list through slicing

> print (aList [4:6])

['Agar,' B']

(4) nesting of lists / / lists supports nesting, that is, lists can be nested in lists, even dictionaries, tuples, etc.

BList= [1001,200, ['aaa','bbb','ccc']]

> print (bList [2] [0])

Aaa

(5) list supports *, +

List1= [1,2,3]

List2= [100200300]

List3=list1+list2

Print (list3)

> [1,2,3,100,200,300]

List4= ['axiajiajiao', 'Bengyun' [']

List5=list4*3

Print (list5)

> ['await,' baked, 'cased,' await, 'baked,' clocked, 'baked,' c']

(6) calculate the length of the list / / use the built-in function len ()

AList= [1,2,3,4,5]

Print (len (aList))

> 5

(7) calculate the maximum and minimum values in the list

AList= [1,2,3,4,5]

Print (min (aList))

> > 1

Print (max (aList))

> 5

(8) the list is extended with the built-in extend function, which looks similar to +, but the difference is that + returns a new list, while extend modifies the list directly.

AList= [1,2,3]

B = [4, 5, 5, 6]

AList.extend (b)

Print (aList)

[1, 2, 3, 4, 5, 6]

(9) find the index / / built-in function index of an element in the list

AList= ['This','is','a','very','good','idea']

Print (alist.index ('very'))

> 3

(10) count the number of times a tuple is in the list, and the built-in function count

AList= ['to','do','or','not','to','do']

Print (aList.count ('to'))

> > 2

The above is all the content of the article "how to use the list in Python". Thank you for reading! Hope to share the content to help you, more related 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