[3.7] Python study notes Day16 sequence


Sequences include a list of tuples and strings

1. common list, tuples and strings

1) Each element can be obtained homogeneous index
2 default index value) are from 0, also supports the negative index
3) can be set within a range of elements by the method of fragment
4) share many operators (repetition operator, splicing character, operator member)

2. Common BIF method

1) list () method

Converting an iterator object list, create a new one, by the iterative loop, is inserted into the list, to form a new list
List ()
List (Iterable)
tuple ([Iterable]) to convert an iterator object a tuple

a = list()
print(a)
b = 'I love weivid'
b = list(b)     #将字符串中所有字符全部转换为单个字符作为列表的元素
print(b)
c = (1,1,2,3,5,8,13,21,34)
c = list(c)     #把元组变为一个列表
print(c)

Here Insert Picture Description

2) str () function is converted to a string
3) len () function returns the length of a string
4) max () function returns the maximum argument inside
print(max(1,2,3,4,5,6))     #打印6
b = 'I love weivid'
print(max(b))               #返回w,是参照assic码的大小

number = [1,18,13,214,-312,32,333]
print(max(number))      #打印333

Here Insert Picture Description

5) min () function returns the minimum value of the sequence parameter inside
print(min(number))          #打印-312

char =  '1234567890'
print(min(char))            #打印0

Here Insert Picture Description
Since each numeric character string can have a corresponding assic code 0 corresponding to the smallest assic code
using max or min must be guaranteed to be one and the same type, either character or numeric, otherwise error
max () the principle of the method

list1 = [1,2,3,4,5,6]
max1  = list1[0]
for each in list1:
    if each > max1:
        max1 = each
#return max1
print(max1)

Here Insert Picture Description

6) sum (iterable [, start = 0]) method returns the sum of the start sequence and optional parameters of iterable
tuple2 = (3.1,2.3,3.4)
print(sum(tuple2))
number = [1,2,3,4]
print(sum(number,5))

Here Insert Picture Description
sum method can not be summed characters

7) sorted () method, the default is small to large
number3 = [12,32,45,52,1,23,44]
print(sorted(number3))

Here Insert Picture Description
Attention and sort () the difference between
the variable name .sort ()
sorted (variable name)

8) reversed () method, and the like reverse
number3 = [12,32,45,52,1,23,44]
print(reversed(number3))
   返回的是python内部的迭代器对象,但是可以将此迭代器对象转化为列表,使用list的方式
print(list(reversed(number3)))

Here Insert Picture Description

9) enumerate () method to enumerate, generating index values ​​and the item value of each element of a tuple
number3 = [12,32,45,52,1,23,44]
print(list(enumerate(number3)))

Here Insert Picture Description
The index value of each element of the list and a tuple form themselves into a new list

10) zip () method returns a tuple consisting of the respective parameter sequence
a = [1,2,3,6,4,5]
b = [4,5,6,7,8,9,10]
print(list(zip(a,b)))   #成对打包,列表b中多出的元素,在列表中省去

Here Insert Picture Description

Published 105 original articles · won praise 71 · views 40000 +

Guess you like

Origin blog.csdn.net/vivid117/article/details/104625162