Python list array USES instance resolution

  • 2020-04-02 14:16:10
  • OfStack

This article details the use of a Python list array as an example. Share with you for your reference. The details are as follows:

A list in Python is similar to an ArrayList in C# and is used to store structures sequentially.
 
Create a list of
 

sample_list = ['a',1,('a','b')]

 
Python list operation

sample_list = ['a','b',0,1,3]

 
I get some value in the list

value_start = sample_list[0]  
end_value = sample_list[-1]

 
Deletes the first value in the list

del sample_list[0]

 
Insert a value in the list

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

 
I get the length of the list

list_length = len(sample_list)

 
List traversal

for element in sample_list:  
print(element)

 
Python lists advanced operations/tricks
 
Produces a list of increasing values

num_inc_list = range(30)  
#will return a list [0,1,2,...,29]

 
Initializes 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 ==[0,0,0,0,0]

 
Attachment: 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"]      # The element can be of any type. 

 
Subscript: read and write by subscript as an array
Starting at 0, there is the use of negative subscripts
0 the first element, minus 1 the last element,
Minus len the first element, len minus 1 the last element

Take the number of elements in list

len(list)   #list The length of the. The method actually invokes this object __len__(self) Methods. 

 
Create a continuous list
L = range(1,5)      # namely  L=[1,2,3,4], There is no last element 
L = range(1, 10, 2) # namely L=[1, 3, 5, 7, 9]

 
The list of methods
L.append(var)   # Additional elements 
L.insert(index,var)
L.pop(var)      # Returns the last element, and from list To remove the
L.remove(var)   # Delete the element that first appears
L.count(var)    # The number of occurrences of this element in the list
L.index(var)    # The location of the element , No exception is thrown
L.extend(list)  # additional list , namely the merger list to L on
L.sort()        # The sorting
L.reverse()     # Reverse order

List operator: +,*, keyword del

a[1:]       # Fragment operator for children list The extraction of the 
[1,2]+[3,4] # for [1,2,3,4] . with extend()
[2]*4       # for [2,2,2,2]
del L[1]    # Deletes the element with the specified index
del L[1:3]  # Deletes an element with the specified subscript range

The list of the copy

L1 = L      #L1 for L Alias, with C That's the same address, right L1 Operation of L Operation. This is how the function argument is passed 
L1 = L[:]   #L1 for L Is another copy.
list comprehension
[ <expr1> for k in L if <expr2> ]

 
2. Dictionary (map of the C++ standard library)
dict = {'ob1 ' :'computer', 'ob2 ' :'mouse', 'ob3 ' :'printer'}

Each element is a pair, containing 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 recognizes only the last key assigned.
 
The method of the dictionary
D.get(key, 0)       # with dict[key] , returns the default value if there is an extra no, 0 . [] No exception is thrown 
D.has_key(key)      # There is the key to return TRUE , otherwise, FALSE
D.keys()            # Returns a list of dictionary keys
D.values()
D.items()
D.update(dict2)     # Add merge dictionary
D.popitem()         # To get a pair And remove it from the dictionary. Empty throws exception
D.clear()           # Empty the dictionary, same del dict
D.copy()            # Copy a dictionary
D.cmp(dict1,dict2)  # Compare dictionaries, ( Priority is the number of elements, key size, key value size )
# The first big return 1 , little return -1 , same return 0

 
Copy of the dictionary
dict1 = dict        # The alias 
dict2=dict.copy()   # Clone, another copy.

 
3. Tuple: a tuple (that is, an array of constants)
tuple = ('a', 'b', 'c', 'd', 'e')

The [],: operators of list can be used to extract elements. You just can't modify elements directly.

4, the string:         String (that is, list of characters that cannot be modified)

str = "Hello My friend"

A string is an entire body. If you want to modify a part of the string directly, that's not possible. But we can read some part of the string.
Extraction of substrings
str[:6]

The string contains the judgment operators: in, not in

"He" in str
"she" not in str

The string module also provides a number of methods, such as
S.find(substring, [start [,end]]) # Can refer to a range lookup substring, return an index value, otherwise return -1
S.rfind(substring,[start [,end]]) # Reverse lookup
S.index(substring,[start [,end]]) # with find I just can't find it ValueError abnormal
S.rindex(substring,[start [,end]])# Reverse the search as above
S.count(substring,[start [,end]]) # Returns the number of substrings found
S.lowercase()
S.capitalize()      # Capital letter
S.lower()           # Turn to lowercase
S.upper()           # Turn the capital
S.swapcase()        # Case interchange
S.split(str, ' ')   # will string turn list , separated by Spaces
S.join(list, ' ')   # will list turn string , connected by a space

 
A built-in function that handles strings
len(str)                # The length of the string 
cmp("my friend", str)   # String comparison. The first one is big, return 1
max('abcxyz')           # Find the largest character in the string
min('abcxyz')           # Find the smallest character in a string
string The transformation of the
oat(str) # To float, float("1e-1 " )  The results for 0.1
int(str)        # To be an integer,   int("12 " )  The results for 12
int(str,base)   # become base Base integer, int("11 " ,2) The results for 2
long(str)       # It becomes a long integer,
long(str,base)  # become base Base long integer,

 
Formatting of strings (note their escape characters, mostly in C, omitted)
str_format % ( The list of parameters )  � # The parameter list is tuple Is defined in terms of the form that cannot be changed while running 
>>>print ""%s's height is %dcm" % ("My brother", 180)
# The results show that My brother's height is 180cm

 
Transformation of a list and a tuple
tuple(ls)
list(ls)

 
Python removes duplicate elements from a List
 
a = [3, 3, 5, 7, 7, 5, 4, 2]
a = list(set(a)) # [2, 3, 4, 5, 7] I've even sorted it

I hope this article has helped you with your Python programming.


Related articles: