Use the list
Non-numeric types: lists, tuples, dictionaries, strings, are Python Advanced variable types
of non-numeric types in Python are supported by some common operations: is a sequence can also be understood as a container, used to hold things with
List of data types of scenarios: storing a plurality of the same data type, through an iterative traversal, for each element, the same operation
Note: the list of different types of data can be stored, but generally not so used.
1 List Definition
List - in other languages equivalent to the array, for storing a series of information
recording names of three individuals, three string variables can be stored, can also use a list storing a plurality of variables
name_list = [ "zhangsan", "lisi", "wangwu"]
With [] a list of definitions, wherein the elements separated by commas,
2 list of values
Using the extraction of index elements in the list, the index is out of range, the program will be given
name_list[1]
zhangsan
By 3 lists, delete, search, change
A list of common operations include: add, delete, search, change. By functions and methods to perform common operations in two ways: () function call by the function name, variable name by a method of calling a method.
3.1 modify data specified position
name_list[1]=‘lisisi’
3.2 determine the index of the specified element
name_list.index(“lisi”)
Use index method requires attention, if not in the list of data transfer, the program will complain
3.3 increase in operating
1. append data to the list
name_list.append(“wangxiaoer”)
2. Add a list of the index data at the specified position (between 0 and 1 is added)
name_list.insert(1,“xiaomeiemi”)
3. be iterative data at the end of the current object increases
temp_list=[“sunwukong”,“zhuerge”,“sanshidia”]
name_list.extend(temp_list)
3.4 deletion
1. Remove the specified data, the list when there are multiple "zhangsan", will delete a first specified data
name_list.remove(“wangwu”)
2.pop default may be the last element in the list deleted
name_list.pop()
pop method can be specified index, delete the specified elements, while the pop-up element can be returned
a=name_list.pop(3)
3.claer method will list it empty
name_list.clear()
name_list=[]
Python2.7 version of the compiler is not this method even if there is, it will clear the list
4. Extended: Del delete data from the list, to delete the specified index element
del name_list[1]
the variable del essentially removed from memory, if you use del to delete a variable, then the follow-up will not be able to use the variable. Use caution
4 list of statistics
Statistics The method of operation includes a function of a
1. The list of statistical data saved how many data, the length of the list data
len = list_len (NAME_LIST)
Print ( "list contains:% d"% list_len)
2. a statistical data of the number of occurrences
name_list.count = COUNT ( "zhangsan")
Print ( "zhangsan occurred:% d"% count)
5 list is sorted
The new definition of two lists:
NAME_LIST = [ "zhangsan", "Lisi", "wangwu", "wangxiaoer"]
num_list = [6,8,4,1,10]
Default ascending order, the first order of the string of letters a-z
name_list.sort()
num_list.sort()
Descending output
name_list.sort(reverse=True)
num_list.sort(reverse=True)
Flip (reverse)
name_list.reverse()
num_list.reverse()
Loop through the list -for 6
Loop through : the head to get data from the list to sequentially perform the same operation on each element in the body of the loop. In order to improve the efficiency of traversing the list, specializing iterative interation traversal, For iterating can be achieved.
name_list=["zhangsan","lisi","wangwu","wangxiaoer"]
for my_name in name_list:
print("我的名字叫:%s"%my_name)
Order to obtain data from the list in turn, each cycle, the data will be saved in my_name
by my_name this variable, each time within the loop body can access the current data acquired this time
for my_name in name_list (to traverse the list of variables):
Advantages: no need to design a counter, the counter is not necessary to operate