Python sequence list (List), tuples (Tuple)

 

sequence

Python sequence is the most basic data structures, including strings, lists, tuples.

Sequence, by definition, is ordered, the sequence has index, the index can be carried out, sliced ​​(intercept), was added (connected), multiplication (multiplication), an operation check members.

Because the ordered sequence, can be distinguished by the element position, the sequence may contain the same element.

 

Generic sequence of operations

1, the index

seq [index] index starts from 0, the negative support, -1 is the last element.

 

2, sections (taken)

seq [start: end] sequence may be taken, a copy is returned, the same original sequence. The default start default is 0, end -1.

 

3 + (connector)

seq1 + seq2 + seq3 + ..... sequence linked to a plurality of sequence, a copy is returned, the same prosequence

Type sequence to be identical to the sum, such as a list only lists +, + not string.

 

4, * (multiplication)

seq * n will extend the original sequence n times, such as "hello" * 2 is "hellohello". It returns a copy of the original sequence unchanged.

 

5, in, not in (check member)

element in / not in seq determine whether a member in the sequence, the return value is type bool

 

6, python built-in function, not need to import modules:

len (seq) returns the length of the sequence (number of elements)

max (seq) returns the largest element in the sequence of values

min (seq) returns the minimum value of the sequence element

 

 

 

 

List (List)

List element type may be different, even complex data types can be nested. List [] represented by square brackets.

= List1 [l, 2,3]   # same type 
List2 = [. 1, " OK " , [l, 2,3]]     # Different types of nesting &&

 

 

A list of commonly used methods

function description
list.append(ele) Add an element to the end of the list
list.insert(index,ele)     Insert an element at the specified position
list.count(ele) The number of statistical list appears in the ele
list.extend(x)

Extended list, x can be lists, tuples, sets, dictionaries (only add key). Added to the end of the list.

+ Connect only the same type of sequence.

list.index(ele) Index of the element, return to the index
list.pop([index]) Pop (deletion) and returns the element at the specified location ,, defaults to -1 (last element) default index.
list.remove(ele) Removes the specified elements, if there are multiple ele list, only removes the first ele
list.copy() Copy the list, returns a list of replication. Example: list2 = list1.copy ()
list.clear() clear the list
list.reverse() In reverse order
 list.sort(reverse=True)

The elements in the list in ascending / descending order, the default parameters as default False (ascending), True is descending. Example:

list.sort () # ascending

list.sort (reverse = True) # Descending

Note: This function does not return a sorted list.

 

List (List) is variable, if the above method to modify the List, List will change.

Number (Number The), string (String), tuples (Tuple) is immutable, the operation is often returned copy, the original unchanged.

 

 

 

 

Tuple (Tuple)

Tuples may contain different types of data.

Tuples can not be changed, can not delete, modify element tuple, but you can delete the entire tuple with del.

Tuple () is represented by parentheses.

= tuple1 ()    # empty tuple 
Tuple2 = (1,4,3)   
tuple3 = (. 1, "OK", [l, 2,3]) && # different types Nested

 

 

 

 

List, convert between tuples

1, list (tup) converts a list of tuples

= myTuple (l, 2,3 )
 Print (List (myTuple))   # [. 1, 2,. 3] 

"" " 
Python built-in function list (tup) can be converted to a tuple list, and the list is returned 
for this function does not modifies the tuple itself, myTuple still is a tuple, list itself does not become 
"" " 

 

 

2, tuple (list) to convert the list to tuple

= myList [l, 2,3 ]
 Print (tuple (myList))   # (. 1, 2,. 3) 
"" " 
Python built-in function tuple (list) can be converted to a list of tuples, the tuple and returns 
this function does not modify the list itself, myList still list itself does not become a tuple 
"" "

 

These two methods will not modify the list, tuple itself.

 

Guess you like

Origin www.cnblogs.com/chy18883701161/p/11291126.html