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

What are the common operations of Python lists and arrays

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail what the common operations of Python lists and arrays are, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The list (list) in Python is similar to the variable array (ArrayList) in C # and is used for sequential storage structures.

Create a list

Sample_list = ['axiaojia 1, (' axiomagery')]

Python list operation

Sample_list = ['axiaqingjiaoyuanzhongyuanzhongyuanqingyuanzhongyuanzhongyuanzhongyuanfangzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanqingzhongyuanzhongyuanqingzhongyuanzhongyuanzhongyuanzhongzhongjia sample_list 1]

Get a value in the list

Value_start = sample_list [0]

End_value = sample_list [- 1]

Delete the first value of the list

Del sample_list [0]

Insert a value in the list

Sample_list [0:0] = ['sample value']

Get the length of the list

List_length = len (sample_list)

List traversal

For element in sample_list:

Print (element)

Python list Advanced Operations / skills

Generate a numerical increment list

Num_inc_list = range (30)

# will return a list [0,1,2,...,29]

Initialize the list with a fixed value

Initial_value = 0

List_length = 5

Sample_list = [initial_value for i in range (10)]

Sample_list = [initial_value] * list_length

# sample_list = [0pc0pl 0p0p0p0p0p0p0]

Attached: python built-in type

1. List: list (that is, dynamic array, vector of C++ standard library, but can contain different types of elements in a list)

A = ["I", "you", "he", "she"] # element can be of any type.

Subscript: read and write by subscript and treat it as an array

Starting with 0, there is a negative subscript.

0 first element,-1 last element

-the first element of len, the last element of len-1

Take the number of elements of list

The length of len (list) # list. This method actually calls the _ _ len__ (self) method of this object.

Create a continuous list

L = range (1, 5) # that is, L = [1, 2, 3, 4], excluding the last element

L = range (1,10,2) # that is, L = [1,3,5,7,9]

List's method

L.append (var) # append elements

L.insert (index,var)

L.pop (var) # returns the last element and removes it from list

L.remove (var) # deletes the element that first appears

L.count (var) # the number of this element that appears in the list

L.index (var) # the location of the element. If not, an exception is thrown.

L.extend (list) # append list, that is, merge list to L

L.sort () # sort

L.reverse () # reverse order

List operator:, +, *, keyword del

A [1:] # fragment operator for extraction of sub-list

[1pr 2] + [3jr 4] # is [1pr 2pr 3pr 4]. Same as extend ()

[2] * 4 # is [2pm 2pm 2pm 2]

Del L [1] # Delete the element of the specified subscript

Del L [1:3] # Delete elements with specified subscript range

Replication of list

L1 = L # L1 is an alias for L, which means the pointer address is the same in C, and the operation for L1 is the operation for L. This is how function arguments are passed.

L1 = L [:] # L1 is a clone of L, that is, another copy.

List comprehension

[for k in L if]

2. Dictionary: dictionary (i.e. map of C++ standard library)

Dict = {'ob1':'computer',' ob2':'mouse', 'ob3':'printer'}

Each element is pair, which contains two parts: key and value. Key is of type Integer or string, and value is of any type.

The key is unique, and the dictionary only recognizes the last assigned key value.

Dictionary's method

D.get (key, 0) # is the same as dict [key]. If there is no more, the default value is returned, 0. [] if not, an exception is thrown.

D.has_key (key) # returns TRUE with this key, otherwise FALSE

D.keys () # returns a list of dictionary keys

D.values ()

D.items ()

D.update (dict2) # adds merge dictionaries

D.popitem () # gets a pair and removes it from the dictionary. Throw an exception if it is empty

D.clear () # clear the dictionary, same as del dict

D.copy () # copy dictionary

D.cmp (dict1,dict2) # comparison dictionary, (priority is number of elements, key size, key value size)

# the first big returns 1, the small returns-1, and the same returns 0

Replication of dictionary

Dict1 = dict # alias

Dict2=dict.copy () # clone, that is, another copy.

3. Tuple: tuple (that is, constant array)

Tuple = ('averse,' baked, 'clocked,' dumped,'e')

You can extract elements with the [],: operator of list. You just can't modify the element directly.

4. String: string (that is, the character list that cannot be modified)

Str = "Hello My friend"

A string is a whole. If you want to modify a certain part of the string directly, it is impossible. But we can read a certain part of the string.

Extraction of substring

Str [: 6]

The string contains the judgment operator: in,not in

"He" in str

"she" not in str

The string module also provides many methods, such as

S.find (substring, [start [, end]]) # indicates the range to find the substring and returns the index value, otherwise it returns-1

S.rfind (substring, [start [, end]]) # reverse lookup

S.index (substring, [start [, end]]) # is the same as find, but no ValueError exception was found.

S.rindex (substring, [start [, end]]) # reverse lookup as above

S.count (substring, [start [, end]]) # returns the number of substrings found

S.lowercase ()

S.capitalize () # initials are capitalized

S.lower () # to lowercase

S.upper () # turn uppercase

S.swapcase () # case swapping

S.split (str,'') # converts string to list and splits it with spaces

S.join (list,'') # convert list to string and connect with spaces

Built-in functions for handling strings

Len (str) # string length

Cmp ("my friend", str) # string comparison. The first big, return 1

Max ('abcxyz') # finds the largest character in a string

Min ('abcxyz') # finds the smallest character in a string

Conversion of string

Oat (str) # becomes a floating point number, and the result of float ("1e-1") is 0.1

Int (str) # becomes an integer, and int ("12") results in 12

Int (str,base) # becomes a base integer, and int ("11", 2) results in 2.

Long (str) # becomes a long integer

Long (str,base) # becomes a base binary long integer

Formatting of strings (pay attention to their escape characters, such as those in C, abbreviated)

Str_format% (parameter list)

On the Python list and array of common operations are shared here, I hope the above content can be of some help to 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

Internet Technology

Wechat

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

12
Report