Python list (list), tuple (tuple), Dictionary (dict) and the set (set) Comments

Python built four common data structures: a list (list), tuple (tuple), the dictionary (dict) and a set (set).

Once these four data structures can be used to store multiple data items, which is very important for the program, as the program only requires the use of a single variable to hold the data, also you need to use a variety of data structures to store large amounts of data, and lists, tuples, dictionaries and collections can meet the needs of a large number of saved data.

List (list) and tuple (tuple) is quite similar, they are in order to save elements, each element has its own index, so the lists and tuples can be accessed via an index element. The difference is that tuples can not be modified, but the list is modified.

Dictionary (dict) and set (set) Similarly, the data they are stored unordered, wherein the dictionary is  key-value in the form of stored data

 

Detailed sequence python

The so-called sequence, refers to a plurality of values ​​can be stored in contiguous memory space, these values ​​arranged in a certain order, which can be accessed by a number (called an index) for each position where the value.

Whether using positive index value, or negative index value, you can access any element in the sequence.

Note that, in a negative value as the index value of each element in a column sequence used, start from -1, instead of from zero.
[List the root @ Kube] # CAT demo1.py 
STR = " Py Detailed sequence of " 
Print (STR [0], " --- " , STR [-6 ])
 Print (STR [. 5], " --- " , STR [-1 ])
[root@kube list]# py demo1.py 
P --- y
Details --- Solution
[root@kube list]# 

 

Sequence slice

The slicing operation is another method to access elements in the sequence, it can access a range of elements, by a dicing operation, a new sequence may be generated.

Slicing operation sequence to achieve the following syntax:

sname[start : end : step]
Wherein the meaning of the various parameters are:
sname: indicates the name of the sequence;
start: start index indicates the position of the slice (including the location), this parameter may not be specified, the default is 0, that is, from the beginning of the sequence slicing;
end: end slice index indicates a position (this position is not included), if not specified, the default is the length of the sequence;
step: shows the slicing process, every few memory locations (including the current position) to take time element, i.e., if the step value is greater than 1, during the time slice to sequence elements, will "jump" to take elements . If the set value of the step is omitted, then the last colon can be omitted.

 

 

[List the root @ Kube] # CAT demo2.py 
STR = " Py sequence slice " 
Print (STR [: 0]) is 0 #end index indicating the end position but not including, the position, the empty 
 Print (STR [:. 1 ] )
 Print (STR [: 2 ])
 Print (STR [. 1: 2 ])
NUM = " 123456789 " 
Print ([. 1:. 5: 2 NUM ]) # 1-5 to take two compartments of a value interval
[root@kube list]# py demo2.py 

p
py
Y
24
[root@kube list]# 

 

 

 Sequences are added

Python, support the same two types of sequences using the "+" operator make the addition operation, it will be connected to two sequences, but does not remove duplicate elements.

Where the term "same type" refers to the "+" operator or the flanking sequences are sequence type, either both tuple type, either both strings.
Kube List @ root] # CAT demo3.py 
Print ( " Hello " + " I'm good " + " dajiahao " )
[root @ Kube List] # Py demo3.py 
Hello my good dajiahao
[root@kube list]# 

 

Multiplying the sequence

Python, a new sequence by multiplying the number n generates a sequence, the contents of the original sequence is repeated n times results

[root @ Kube List] # CAT demo3.py 
str = " Hello " + " I'm good " + " dajiahao " 
Print (str * 3 )
[root @ Kube List] # Py demo3.py 
Hello I'm so good I dajiahao Hello Hello I'm so dajiahao dajiahao
[root@kube list]# 

 

 

 Check element is included in the sequence

Python can be used in a keyword to check whether an element is a member of the sequence, its syntax is:

value in sequence

Keyword usage and in the same, but the functionality is exactly the opposite, and not in a keyword, use it to check whether an element not contained in the specified sequence

value not in sequence

 

[root@kube list]# cat demo4.py 
#coding:utf-8

str="test.com.cn"
print('e' in str)
print('e' not in str)
[root@kube list]# py demo4.py 
True
False
[root@kube list]# 

 

And serial correlation of built-in functions

Python provides several built-in functions (Table 3), it may be used to implement some common operations related sequences

Table 3 built-in functions related sequences
function Features
Only () Calculating the length of the sequence, i.e., it returns the number of elements contained in the sequence.
max() To find the largest element in the sequence. Note that, when using the sequence of sum () function, are digital summation must be operated, a character or character string can not otherwise function throws an exception, because the interpreter can not determine the connecting operation is to be done (the + operator you can connect two sequences), or do add and operation.
(I) Find the smallest element in the sequence.
list() The sequence into a list.
str() Sequence into a string.
sum() And computing elements.
sorted() Sort elements.
reversed() Reverse sequence elements.
enumerate() The sequence is a combination of the sequence index, is used in a for loop.

 

[root@kube list]# cat demo5.py 
str="test.com.cn"
print(max(str))
print(min(str))
print(len(str))
print(list(str))
[root@kube list]# py demo5.py 
t
.
11
['t', 'e', 's', 't', '.', 'c', 'o', 'm', '.', 'c', 'n']
[root@kube list]# 

python list list

Python  is not an array, but adding a more powerful list. If the array is seen as a container, then Python list is a factory warehouse.

 Formally, a list of all elements will be placed in a pair of square brackets [], between adjacent elements separated by a comma, format, element1 ~ elementn represent elements in the list, the number is not limited as far as Python supported data types can be. As follows

[Element1, element2, Element3, ..., elementN]

 

 

[root@kube list]# cat demo6.py
lt=['c.test.com',1,[2,3],2.34,'aa']
print(lt)
print(type(lt))
[root@kube list]# py demo6.py 
['c.test.com', 1, [2, 3], 2.34, 'aa']
<class 'list'>
[root@kube list]# 

Create a list

[List the root @ Kube] # CAT demo7.py 
lt = [. 1, ' x.test.com ' , [2,3 ]] # = number assignment using [] enclosed
str="1234test"
print(type(lt))
print(type(str))
LT2 = List (STR) # use list () function converts
 Print (LT2)
 Print (type (LT2))
[root@kube list]# py demo7.py 
<class 'list'>
<class 'str'>
['1', '2', '3', '4', 't', 'e', 's', 't']
<class 'list'>
[root@kube list]# 

 

 

 python access and delete list

[root @ Kube list] # cat demo8.py 
want = [1, ' test.com ' [3,4,5] 44, ' fff ' , ' ee ' ]
 print (vol [1 ])
 print ( wants [2 ])
 print (vol [2: 4 ])
 the flight
 print (vol)
[root@kube list]# py demo8.py 
[1]
[1, 'test.com']
[[3, 4, 5], 44]
Traceback (most recent call last):
  File "demo8.py", line 6, in <module>
    print(vol)
NameError: name 'vol' is not defined
[root@kube list]# 

 

 

Add three ways elements of python list list

Appending element append () method is used in the list, the syntax of the following methods:

listname.append(obj)

Of course, if desired not be added to the list of tuples or as a whole, but only an additional element of the list, you can use the extend () method of providing the list. Syntax extend () method is as follows:

listname.extend(obj)

 

 If you want to increase the elements in the middle of the list, you can use the list of insert () method, this method syntax is:

listname.insert(index , obj)

Wherein, index parameter refers to an element inserted into the list at the position specified index value.

Using the insert () method to insert elements in the list, and append () method of the same, regardless of the inserted object is a list or tuple, only it will be treated as a whole element.

[root@kube list]# cat demo9.py
a_list=['test.com.cn',2,[2,'a']]
a_list.append('b')
print(a_list)
a_list.extend([9,8,7])
print(a_list)
a_list.insert(4,'MM')
print(a_list)
[root@kube list]# py demo9.py 
['test.com.cn', 2, [2, 'a'], 'b']
['test.com.cn', 2, [2, 'a'], 'b', 9, 8, 7]
['test.com.cn', 2, [2, 'a'], 'b', 'MM', 9, 8, 7]
[root@kube list]# 

 

 

Python list three methods to remove elements of the list

Delete elements in the list, divided into the following three scenarios:

  1. Delete the index value of the position of the target element is located, you can use the del statement;
  2. The value of the element to delete, remove () method list (list type) may be used to provide;
  3. Will be deleted from the list of all the elements of all, you can use the list (list type) provided clear () method.
[root@kube list]# cat demo10.py 
a_list=[1,2,"a.b.c",[4,5],(2,4)]
print(a_list)
del a_list[-2]
print(a_list)
a_list.remove(2)
print(a_list)
a_list.clear()
print(a_list)
[root@kube list]# py demo10.py 
[1, 2, 'a.b.c', [4, 5], (2, 4)]
[1, 2, 'a.b.c', (2, 4)]
[1, 'a.b.c', (2, 4)]
[]
[root@kube list]# 

 

python list modify elements

Elements of the list corresponds to the variable, so the program can be assigned to the elements of the list, so you can modify the list of elements

slice fragmentation can also use

[List the root @ Kube] # CAT demo11.py 
a_list = List (Range (0,10))        # List () function converts a list 
Print (a_list)
a_list [ 2] = ' A '                   # replacement sequence a value of 2 
Print (a_list)
a_list [ -1] = ' FFFF '               # replacement sequence value -1 
Print (a_list)
a_list [ . 3:. 4] = [ ' S ' , ' D ' ]           # replacement value fragment 3-4 
Print (a_list)
a_list [ . 4:. 6] = [] # replace the value 4-6 is empty
 Print (a_list)
a_list [ . 1:. 3] = ' Test When' using slice # syntax assignment list, not a single value; If the string assignment, the Python  automatically processing the character string as a sequence, where each character is an element. 
Print (a_list)
a_list [ 2:. 6: 2] = [ ' the MM ' , ' NN ' ] # slice syntax when using assigned parameters may also be specified step. If the number of list elements but the number of the step parameter is specified requirements assignment list and replaced elements are equal
 Print (a_list)
[root@kube list]# py demo11.py 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 'a', 3, 4, 5, 6, 7, 8, 9]
[0, 1, ' a ' , 3, 4, 5, 6, 7, 8, ' ffff ' ]
[0, 1, 'a', 's', 'd', 4, 5, 6, 7, 8, 'ffff']
[0, 1, ' a ' , ' s ' , 5, 6, 7, 8, ' ffff ' ]
[0, 't', 'e', 's', 't', 's', 5, 6, 7, 8, 'ffff']
[0, 't', 'MM', 's', 'NN', 's', 5, 6, 7, 8, 'ffff']
[root@kube list]# 

 

Guess you like

Origin www.cnblogs.com/zy09/p/11585516.html