Learning Python and the packet is certainly no stranger to the list, they form the basis python, but often it is easy to confuse these two in the application, and even sometimes hard Qiaowan several hundred lines of code error results can be wrong the root cause is a list of these groups and simple. . . . . This is to make people crazy. . . . .
The following detailed list of packets and to distinguish:
Python and how to distinguish between packet list
slice
The slicing operation is another method to access elements in the sequence, it can access a range of elements. A new sequence may be generated by slicing operation. Achieve slicing operation syntax is as follows:
sname[start:end:step]
Parameter Description:
- sname: indicates the name of the sequence;
- start: indicates the start position of the slice (including the location), if not specified, the default is 0;
- end: As represents a slice position (not including the location), if not specified, the default is the length of the sequence;
- step: step indicates slice if omitted, the default is 1, when the step is omitted, the last colon may be omitted.
Python built-in functions
function | effect |
---|---|
list() | The sequence is converted to a list |
str() | Sequence into a string |
sum() | And computing element |
reversed() | Reverse sequence of elements |
enumerate() | The sequence is a combination of the sequence index, is used in a for loop |
sorted() | Sort elements |
Create and delete lists
- Python with other types of variables, when you create a list, you can also use the assignment operator "=" directly to a list assignment to a variable, syntax is as follows:
listname = [element 1, element 2, element 3, ..., element n]
- Create an empty list
in python, you can create an empty list, for example, to create a name for the empty list emptylist, you can use the following code:
emptylist = []
- Create a list of values
in python may be used list () function to convert the direct result of the function cycle out of range () as a list.
The basic syntax list () function is as follows:list(data)
where, data represents the data can be converted to a list, which may be the type of range target, a string, a tuple or other types of data iteration. - Delete List
for the list has been created, when not in use, you can use the del statement to remove it. Syntax is as follows:
Del listname
where, listname is the name of the list you want to delete.
Traverse the list
(1) directly for-loop
Values for loop directly through the list, only the output elements, syntax is as follows:
for item in listname:
#输出 item
Wherein, item used when saving the acquired element value, the element content to be output directly to the output of the variable; listname name for the list.
(2) for loop and the enumerate () function to achieve
Using a for loop and the enumerate () function can be achieved while the output index values and element contents, syntax is as follows:
for index, item in enumerate(listname):
#输出index和item
Parameter Description:
- index: the index for the element of preservation.
- item: When the acquired element for storing a value, the content of the element to be output directly to the output of the variable.
- listname is the name of the list.
Add list element syntax
listname.append(obj)
Described above is to add an element to the list, if you want to add all the elements of a list to another list, you can use a list of objects extend () methods. Syntax extend () method is as follows:
listname.extend(seq)
Which, listname is the name of the list you want to add elements, obj to be added to the end of the list of objects. seq to be added to the list. After the statement is executed, the contents will be appended to the seq of listname.
List of statistics and computing
(1) Get Occurrence specified element appears
Use the list of objects count () method gets the number of occurrences of the specified element in the list. The basic syntax is as follows:
listname.count(obj)
(2) to obtain the first occurrence of the specified element subscript
Use the list of objects index (method to obtain the position of the first occurrence of the specified element in the list (or index) basic syntax is as follows:
listname.index(obj)
Parameter Description:
- listname: indicates the name of the list.
- obj: indicates whether the object is determined to exist, where only an exact match, i.e., not part of the element value.
Elements and (3) a list of values statistics
In Python, a sum () function is used for statistical values of the elements in the list, and. Syntax is as follows:
sum(iterable[,start])
Parameter Description:
- iterable: pledged to statistics list.
- start: the statistics indicate that the number from which to start (add start soon statistics specified number), it is optional, if not specified, the default value is 0.
Sort the list
(1) a list of objects using a method sort0
List object provides sort () method is used for the original elements of the list are sorted. After the sort order of the elements in the original list will change. Syntax sort a list of objects () method is as follows:
listname.sort(key=None,reverse=False)
Parameter Description:
- listname: said to be sorted list.
- key: indicates a key for specifying the extraction of comparison from each element (e.g., setting "key-str.lower" represents - insensitive when sorting).
- reverse: an optional parameter if the designated value is True, it represents a descending order: If False, said ascending order, the default is ascending.
(2) using the built-sorted () function to achieve
In Python, it provides a built-in function has the following syntax sorted (function for sorting the list after sorting using this function, the original order of the elements of the list unchanged .storted.):
sorted(iterable,key=None,reverse False)
Parameter Description:
- iterable: represents the list of names to be sorted.
- key: indicates a key for specifying the extraction of comparison from each element (e.g., setting "key-str.lower" represents - insensitive when sorting).
- reverse: an optional parameter if the designated value is True, it represents a descending order; if False, said ascending order, the default is ascending.
List comprehensions
A list of values (1) to generate a specified range
Syntax is as follows:
list = [Expression for var in range]
Parameter Description:
- list: represents a list of names generated.
- Expression: expression used to calculate the new elements of the list.
- var: loop variable.
- range: using the range (range of the object function generation.
(2) generating a list based on the list of requirements specified
Syntax is as follows:
newlist = [Expression for var in list]
Parameter Description:
- newlist: represents a list of names of the new generation.
- Expression: expression used to calculate the new elements of the list.
- var: variable value of each element of the list is later.
- list: the original list generation for the new list.
(3) Select the qualifying element from the list to form a new list
Syntax is as follows:
newlist = [Expression for var in list if condition]
Parameter Description:
- newlist: represents a list of names of the new generation.
- Expression: expression used to calculate the new elements of the list.
- var: variable value of each element of the list is later.
- list: the original list generation for the new list.
- condition: a conditional expression for specifying the filter criteria.
Create and delete tuples
(1) using the assignment operator directly create a tuple
Like other types of Python variables when creating a tuple, you can also use the assignment operator "=" directly to a tuple assigned to the variable. Syntax is as follows:
tuplename = (element 1,element 2,element 3...ement n)
Wherein tuplename
represents the name of the tuple, Python may conform to any naming identifier; elemnet 1. elemnet 2. elemnet 3. elemnet n
represents tuple in number is not limited.
(2) create a value-tuple
In Python, may be used tuple () function directly out of range results loop function () converted into a numerical tuple. tuple () function has the following basic syntax:
tuple(data)
Where, data represents the group can be converted to data elements, which may be the type of range target, a string, a tuple or other types of data iteration.
(3) Delete tuples
For tuples already created, when no longer in use, you can use the del statement to remove it. Syntax is as follows:
del tuplename
Which, tuplename is the name of the tuple to be deleted.
- Useful if little friends to a trifecta, so that more people can see it -
Thumbs up, plus interest
Thumbs up, plus interest
Thumbs up, plus interest