Data structures in Python
Here's a summary of the built-in data structures in Python (built-in data Structure): lists, tuple tuples, dictionary dict, set set, covers only some of the focus, detailed to describe each knowledge point is not involved.
Notable features of List listlist
Each element in the
-
List is mutable
means that each element can be modified and deleted
-
Lists are ordered, each element's position is deterministic, and each element can be accessed with an index
- The
element in the list can be any object in the Python
can be any object that means that the element can be a string, an integer, a tuple, or it can be an object in Python, such as a list.
>>> x = [1 , 2 , 3 ] >>> y = { ' name ' : ' Sakura ' } >>> z= "Test" >>> a= [X,y,z]>>> a[[1 , 2 , 3 ], {: }, ' Test ' ]
I think the list in Python is quite similar to the array in C, except that the element type in the list can be any object in Python, and that the array in C can only be of the same type. Indexes can be used when accessing the elements in them. It is important to note that the list can be indexed as well as a positive index to an array.
Positive and negative indices in list
???
List of additions and deletions to check the change
List has a lot of methods, here only to the list of elements to make additions and deletions to check and change methods.
- Use Insert () and shard to add elements to the specified location, using the Remove () and the Keyword del pairs
element to delete
Note that the position in the Shard [A:B] does not contain position B, and [a,a] can be used to add elements to position a
???
???
Searching for elements with positive and negative indexes
You can assign a value to an element modification directly instead
There are other methods in the list such as pop () to remove the end element, pop (i) to delete the element at the specified position I, append () add element to the end
List function
You can create a series as a list by using list
Python contains a sequence of 6 built-in: Lists, tuples, strings, Unicode strings, buffer objects, and Xrange objects.
>>>list("Hello,world")[‘H‘‘e‘‘l‘‘l‘‘o‘‘,‘‘w‘‘o‘‘r‘‘l‘‘d‘]
In fact, list is a type is not a function, but there is not much difference between the two. The following tuple, Dict are the same.
Tuple tuples
Tuples can be understood as a fixed list, and once the elements are initialized they cannot be modified (carefully understood), only the elements can be queried
>>>Test=(1,2,3)>>>Test1,2,3)>>>test[1]=4Traceback (most recent): File"<input>", line1,inch <Module>TypeError:' tuple ' ObjectDoes notSupport Item Assignment>>>test[1:1]= 4Traceback (most recent): File"<input>", line1,inch <Module>TypeError:' tuple ' ObjectDoes notSupport Item Assignment>>>test[1]2
?? From the above, the tuple does not support the modification of elements (including deletion), and the tuple initialization is fixed.
?? Let's look at one more example.
>>> test = ( ' a ' , ' B ' , [ ' a ' , ' B ' ]) >>> print (Test) ( ' a ' , Span class= "St" > ' B ' , [ ' A ' , ' B ' ]) >> test[2 ][0 ]= ' x ' >>> test[2 ][1 ] = ' y ' >>> test (, , [ ' x ' , ])
?? This looks like the elements in the element have changed, but with careful analysis, the third element in the tuple is a list.
?? The code 3.4 line changes the value in the list, the tuple refers to the element list does not change, you need to pay attention to this!
?? This involves mutable objects and immutable objects in Python, which are mutable objects like List, and tuples are immutable objects.
Tuples are fixed lists, so what is the meaning of tuples?
Because the tuple is immutable, the code is more secure. If possible, you can use tuple instead of list
And you need to pay attention to the variability of the elements in the tuple!!
Empty tuple can be recorded as (), if only one element of the tuple is recorded as (1,)
Since it is recorded as (1), this actually represents the number 1, at this time () is the parentheses in the mathematical formula
Because tuples are fixed lists, most of their built-in methods and lists are similar.
You can convert a sequence to a tuple through a tuple, using the same as List
>>>tuple(‘Hello,world!‘)(‘H‘‘e‘‘l‘‘l‘‘o‘‘,‘‘w‘‘o‘‘r‘‘l‘‘d‘‘!‘)
Dictionary Dict
The concept of a dictionary is based on real-life dictionary prototypes, where life uses name-content to build data, and Python uses key (key)-value storage, which is a map in C + +.
Notable characteristics of Dict
The data in the dictionary must appear as a key-value pair
Keys are not repeatable, values are repeatable
Key if the last value corresponding to the key is only remembered in the repeating dictionary
In the dictionary, the key (key) is immutable, immutable, and cannot be modified, while the value (values) can be modified and can be any object.
In Dict, the storage location of value is calculated based on key, and if each calculation of the same key results in a different result, the dict interior is completely chaotic.
Dict and additions and deletions to change
- You can add elements to the dictionary by using the "key-value pair" method and the update () method
Delete can use the Keyword del and the Pop () method
???
- Queries use indexes such as query list elements to look up values using keys as indexes
If the element does not exist will be an error, before the search, there are two ways to determine the existence of key:
① Membership operator--in operator
②get () method (returns null if the value does not exist, or the value returned)
>>> test={‘Mon‘:1}>>>‘Fri‘in testFalse>>> test.get(‘Fri‘)>>> test.get(‘Fri‘,-1)-1
The method that can be used to directly overwrite the original value to be worth modifying
The elements in the dict are unordered and cannot be used in shards.
dict function
You can use Dict to build a dictionary from other mappings or sequences (key, value) pairs.
>>> test=[(‘name‘,‘Sakura‘),(‘age‘,20)]>>>=dict(test)>>> d{‘name‘‘Sakura‘‘age‘20}
Dict can also use the keyword parameter to create a dictionary, or map as the dict parameter, Dict returns an empty dictionary without any parameters
>>>=dict(name=‘Sakura‘,age=20)>>> d{‘name‘‘Sakura‘‘age‘20}>>> a=dict()>>> a{}
Set Set
Collections are closer to the concept of a mathematically set. Each element in the collection is an unordered, non-repeating arbitrary object.
You can use a collection to determine the dependencies of the data, or you can subtract the duplicated elements of the data structure through a collection. Collections can be set operations, and elements can be added and deleted.
Creation of collections
When creating a collection, you need to use list as the input collection, which can be added by the Add () method, removing the element with the remove () method
>>> test = set ([1 , 2 , 3 ]) >>> test{1 , 2 , 3 }>>> test.add (3 ) >>> test{1 , 2 , 3 } >>> test.add (6 ) >>> test{ 1 , 2 , 3 , 6 }>>> test.remove (3 ) >>> test{< Span class= "DV" >1 , 2 , 6 }
Duplicate elements in the collection are filtered out
Set operation
Collections in Python can also perform intersection, and, and other operations between collections
>>>=set([1,2])>>> s2=set([2,3])>>>& s2 # s1与s2{2}>>>| s2 # s1与s2进行或运算{123}
Data structures in Python