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

Example Analysis of the list of Python basic data structures

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article shares with you the content of a sample analysis of a list of Python basic data structures. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

List of basic data structures

A list list is an ordered collection in which elements can be added and removed at any time. It is similar to the array in c and java, but the array in c and java must hold elements of the same type, while in the list of python, the elements can be of different types. Operations that can be performed in a sequence include indexing, slicing, adding, multiplying, and checking members.

1. Representation of the list

To create a list, simply enclose the different data items separated by commas in square brackets.

List1 = ['str',' Chinese', 123 abcde','pppp' Python'] list2 = [2 str1','str2', 'abcde','pppp'] # A list serves as an element in another list 2. Properties of the list

1. List index

The symbol is: [index value]

List = ['str11','str22','str33','str44']

(1)。 Forward index

> list [0] 'str11' > list [1]' str22' > list [3] 'str44'

(2)。 Reverse index

> list [- 1] 'str44' > list [- 2]' str33'

two。 Slices of a list

Like the slicing of a string, each character in the string is used as the basic unit, and each element in the list separated by commas as the basic unit.

List [start:end:step] # starts at start position and ends at end-1 position with a step of step;-if start is omitted, slice from the beginning;-if end is omitted, slice until the end of the string > > list = [111pr. 222pr. 333] 44pr. > list [:] # slice all [111pr. 222 pr. 333] > list [:-1] # from the first element to the penultimate element [111pr. 222 pr. 333] > list [:-2] # from the first element to the penultimate element [111 pas. 22333] # from the first element to the penultimate element [111 pas. 22333] # 55, 77] > > list [::-1] # step size is negative Slice from the end [88, 77, 66, 55, 44,333,222,111] > > list [::-2] # step size is-2 [88,66,44,222]

3. Repetition of the list

The symbol is: *

> list = ['aa','bb','cc'] > > list*3 [' aa','bb','cc', 'aa','bb','cc',' aa','bb','cc']

4. Connection to the list

The symbol is: +

> a = ['ab','ef','gg'] > b = [' nice','good','great','perfect'] > aquib ['ab','ef','gg',' nice','good','great','perfect']

5. Member operator

Symbols are: in and not in

> list = ['abc','egg','yyy',' perfect'] > 'abc' in listTrue >' a'in listFalse >'I 'not in listTrue >' perfect 'not in listFalse3. Update list

The symbol is: assignment symbol =

> list = ['Life is too short','I choose python','2223333','+-*/'] > list [3] = 123456 > list ['Life is short','I choose python','2223333', 123456] > list [0] = 'pypy' > list [' pypy','I choose python','2223333', 123456] 4. Nested list

A nested list is a list that contains other lists.

1. Create nested columns

(1)。 Create directly:

> list1 = [['ab','cd','ee'], [12jre 34 ab','cd','ee' 56], [' pause recalcitrance]] > list1 [['ab','cd','ee'], [12mai 3jue 56], [' pause dagger]]

(2)。 Create indirectly:

> a = [1ww','tt','yy'] > b = ['ww','tt','yy'] > c = [' df','hh',99] > list2 = ['df','hh',99] > list2 [[1list2], [' ww','tt','yy'], ['df','hh',99]]

two。 Index of nested lists

Similar to the two-dimensional array in c and java. Index with a combination of brackets. The index of a nested list is interesting. It is a combined operation of slicing and indexing. (in fact, I think an index is essentially a slice, except that the end value of the slice is start+1 and the step size is 1)

You can only query an element or more primary colors of a sublist in a nested list at a time, but not an element in the unified index location of multiple sublistings. If you have to do so, the result will be a sublist.

> > list2 [[1,2,34,66], ['ww',' tt', 'yy'], [' df', 'hh', 99]] > list2 [:] [1] [' ww', 'tt',' yy'] > list2 [:] [0] [1,2,34,66] > > list2 [0] [:] [1,2,34,66] > list2 [:-1] [0] [1,2,34] 66] > list2 [0] [:-1] [1,2,34] > 5. List built-in function name description len (list) list element number max (list) return list element maximum value min (list) return list element minimum value list (seq) convert tuple to list list.append (obj) add a new object list.count (obj) count the number of times an element appears in the list list.extend (seq) append another sequence at the end of the list at one time Multiple values of (extend the original list with the new list) list.index (obj) find the index position of the first match of a value from the list list.insert (index) Obj) inserts an object into the list list.pop ([index=-1]]) removes an element from the list (default last element) And return the value of the element list.remove (obj) remove the first match of a value in the list list.reverse () elements list.sort (cmp=None, key=None, reverse=False) in the reverse list sort the original list list.clear () empty the list list.copy () copy the list thank you for reading! This is the end of the article on "sample Analysis of the list of Python basic data structures". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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