Python study notes (IV): lists, tuples and strings (1)

The fifth chapter lists, tuples and strings

Create a list

Create a list enclosed in square brackets with a bunch of data on it, separated by commas between the data.

>>> number = [1,5,5,7,5,9]
混合列表
>>> mix = [1,"甲鱼",3.14,[1,5,7]]

Add an element to the list

append (): add parameters to the end of the list, but can not add more than at the same time.

>>> number
[1, 5, 5, 7, 5, 9]
>>> number.append(6)
>>> number
[1, 5, 5, 7, 5, 9, 6]

extend (): Adding more elements to the end of the list, a list of parameters should be

>>> number.extend(7,8)
Traceback (most recent call last):
  File "<pyshell#46>", line 1, in <module>
    number.extend(7,8)
TypeError: extend() takes exactly one argument (2 given)
>>> number.extend([7,8])
>>> number
[1, 5, 5, 7, 5, 9, 6, 7, 8]

insert (): To insert an arbitrary element of the list, the first parameter represents a position, the second parameter indicates the inserted element,
note: Python indexes start from zero.

Getting an element from the list

Gets the index value, the list of index values ​​from zero.

>>>number[2]
5

Exchange list elements:

>>> name = ["鸡蛋","鸭蛋","鹅蛋","李狗蛋"]
>>> name[1],name[3]=name[3],name[1]
>>> name
['鸡蛋', '李狗蛋', '鹅蛋', '鸭蛋']

Remove elements from the list

remove(),del,pop()

remove (): only need to know the element in the list, do not need to know the position, if not in the list will complain

>>>name.remove(“李狗蛋”)

del: del is a statement, delete the specified position of the element or the entire list

>>>del name[1]
>>>del name

pop (): default pop the last element in the list, with an index will pop up the corresponding parameters.

>>>name.pop(2)
“鹅蛋”

List slicing

Use list slicing can get a lot of elements at once.
Two spaced index values with a colon, the end element position is not included. List fragmentation does not change the original list.
No starting position python default is 0.
name [: 2]
name [. 1:] From the end of the list to the specified index;
name [:] get a copy of the entire list.

Advanced list slicing

The third parameter setting step, the default value is 1

>>>list[0:9:2]
[1,3,5,7,9]
还可以写成list[::2]

Step is negative

>>>list[::-1]
[9,8,7,6,5,4,3,2,1]
相当于复制一个反转的列表。

Common operator

list1> list2 list may compare
list1 + list2, plus operator is connected, corresponds to the extend () effect.

>>>list = [123]
>>>list *3 重复操作符
[123,123,123]

in和not in

in judgment and not in only one level of membership.
Entering one list list1 [2]

Little friends list

count (): count the number of its parameters appear in the list

>>>list.count(1)
2

index (): returns the position parameter in the list

>>>start = list.index(1)+1
>>>stop = len(list)
>>>list.index(1,start,stop)
2

reverse (): The entire list situ flip.

>>>list.reverse()

sort (): sort the list of programs, the default parameters do not need, from small to large. Default sort (reverse = False), the False True is changed in descending order.

Creating a tuple
create tuples most of the time with parentheses.

>>>tuplel = (1,2,3,4,5,6)
访问元组和列表一样
>>>tuplel[1]
2

You can not modify the elements of a tuple

>>> tuplel[1] = 1
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    tuplel[1] = 1
TypeError: 'tuple' object does not support item assignment

Iconic symbol is tuple (,) comma.
Create an empty tuple can directly use parentheses.
Tuple only one element in followed by a comma can be.

>>> 8*(8)
64
>>> 8*(8,)
(8, 8, 8, 8, 8, 8, 8, 8)

Updating, and deleting a tuple
by tuple slicing method resolved, then the connection string (+) into tuples.

>>> temp = ("小鸡","小鸭","小猪")
>>> temp = temp[:2]+("小甲鱼",)+temp[2:]
>>> temp
('小鸡', '小鸭', '小甲鱼', '小猪')

Delete individual elements of a tuple is impossible, but can indirectly be deleted.

>>> temp = temp[:2]+temp[3:]
>>> temp
('小鸡', '小鸭', '小猪')

Delete the entire element with del

>>>del temp
Published 11 original articles · won praise 0 · Views 67

Guess you like

Origin blog.csdn.net/qq_43863790/article/details/104069293