python basis of three (lists and tuples)

Data structures: a data structure in some way (such as by number) combining the set of data elements. In python, the basic data structure of a sequence, and each element has a sequence number starts from 0.

1, an overview of the sequence:
 Python built many types of sequences, lists , tuples , strings and the like.
 Different lists and tuples is that the list can be amended, but not tuples, which means that the list of circumstances need a way to add elements, and tuples apply to situations not be modified elements.

1.1, create a list

Suppose you need something to store multiple names, then use the list just right

list_name = ["王晓明","李二"]               #所有元素放在方括号内,并且里面的元素有逗号隔开

Which may also contain a list of other lists
python basis of three (lists and tuples)
1.2, generic sequence of operations
Some operations apply to all series, these operations include an index , a slice , addition , multiplication , and the membership check . There are some built-in functions used to determine the length of the sequence and to identify maximum and minimum values.

1.2.1 , all elements of a sequence are numbered from 0 increments, you can be like (figure below) such access
python basis of three (lists and tuples)
in this way to access elements on the map called index, this method is applicable to index sequence. -1 position represents the last element

1.2.2 Slice
elements in addition to using the index to access a single element, but also can be used to access a specific range of slice
python basis of three (lists and tuples)
Note: only the slices cut before a second number of, here it is not only output .ba that's right i

If I have a list there are many elements, then I want the last three, how can I do? Perhaps you can do. . .

top = 'www.baidu.com'        #我只想要最后的com
print(top[-3:-1])                    #也许我可以这样
>>>co                                 #代码输出(这样好像不包括最后一个元素)
print(top[-3:0])                     #那在-1再进一步写成0呢
>>>[]                                   #代码输出(是个空列表)
注:如果第一个索引的位置位于第二个索引的后面那么就是空序列这里-3在0的后面
print(top[-3:])                      #事实上可以这样写,不指定第二个元素结束于序列末尾
#同样的,如果切片在一个序列开头,可以不指定第一个元素
print(top[:5])                       

#如果要复制整个序列,可将两个索引都省略
top[:]                                   #代码输出www.baidu.com

1.2.3, step
Shihai execution sections can be specified third number, the entire number must be greater than 1 (the default is 1), represents the whole number of steps (see below, every element take a)
python basis of three (lists and tuples)

1.2.4 sequences are added
the addition operator to splice sequence, it is to be noted that only the same type of splicing.
python basis of three (lists and tuples)

1.2.5, multiplying the sequence
when the sequence is multiplied by the number n, the entire sequence is repeated n times to generate a new sequence
python basis of three (lists and tuples)
1.2.6, membership in
to check whether a value using the operator in the sequence. Satisfies the condition returns True, is not satisfied return False (Boolean operators).
python basis of three (lists and tuples)

1.3 List

前面已经说过列表的基本操作,这里主要说下列表的方法

1.3.1, the list function
list for type conversion function, can be converted into a string or a list of tuples
python basis of three (lists and tuples)
basic operation 1.3.2, list

修改列表的值
lis = [1,2,3]                   #定义一个列表
lis[0] = 5                       #使用索引给指定的元素赋值即可
print(lis)                        #命令回显[5,2,3]

删除元素
lis = [1,2,3]
del lis[0]                       # 将第一个元素删除

1.3.3, the list method
append method (to add an object to the end of the list)
python basis of three (lists and tuples)

clear method (Clear List)
python basis of three (lists and tuples)

Method copy (copy)
python basis of three (lists and tuples)

Method count (the number of calculated values ​​given appear in the list)
python basis of three (lists and tuples)

extend method (to add more value to the end of the list), then the value is not to say you can add a list to another list it? See the figure
python basis of three (lists and tuples)

insert method (also add elements to the list, but you can specify the location)
python basis of three (lists and tuples)

index method (to return a specified number subscript)
python basis of three (lists and tuples)

pop (delete a value from the end of the list)
python basis of three (lists and tuples)

remove (delete a specified value)
python basis of three (lists and tuples)

reverse (reverse order sorted list)
python basis of three (lists and tuples)

sort (sort)
method is used to sort the list is sorted, but it is to the original sort the list, instead of returning a copy.
python basis of three (lists and tuples)


Tip: If you do not want to modify the original list so only one copy, can not directly accept the sort of value, because
it has no value


1.4, tuples
and lists, tuples is also a sequence, and a list of different places that tuples can not be modified. Create a tuple is also very simple, as long as the number of values separated by commas, automatically create a tuple. B create a tuple of time with the following diagram (), which also can generally do this (second recommendation).
python basis of three (lists and tuples)

1.4.1 How to create an empty tuple and a tuple value as long as the
empty tuple with two parentheses, a tuple element behind a comma
python basis of three (lists and tuples)

**1.4.2、tuple函数**
tuple和list工作原理一样,都是将一个序列作为参数,转换为列表或者元组

python basis of three (lists and tuples)


Added: visit tuples and lists the same way, subscript and slice, tuples can not be modified, so I do not have any way to modify () actually there are ways to modify


Guess you like

Origin blog.51cto.com/12020040/2427275