Python basic tutorial: summary of common methods of list, tuple, range

list

The list (array) is an iterable object, and the list is variable, so the method of the list is changed in the list itself. Inside, you can put data of various data types and store a large amount of data. The
connection list can use + or extend()

a = [1, 3, 2]
b = [3, 4, 5]
print(a+b)
print(a.extend(b))

Increase of the list:

# append()       可追加各种类型数据,只有一个参数追加数据到列表末尾
# insert()          按照指定下角标位置插入,比如插入位置是1,列表原来的数据就会集体后移,效率低
# extend()       迭代增加,注意增加字典只会增加key值

lst = ['hello', 'kitty', 123, 'qqq']          
lst.append('wangxd')                 输出:['hello', 'kitty', 123, 'qqq', 'wangxd']
lst.insert(1, 'gogogo')                输出:['hello', 'gogogo', 'kitty', 123, 'qqq']
lst.extend({
    
    'a': 1, 'b': '2','c': 3})   输出: ['hello', 'kitty', 123, 'qqq', 'a', 'b', 'c']  #增加字典只会增加key值  
lst.extend(['aa', 'bb', 'cc'])          输出:['hello', 'kitty', 123, 'qqq', 'aa', 'bb', 'cc']
lst.extend('qweasd')                  输出:['hello', 'kitty', 123, 'qqq', 'q', 'w', 'e', 'a', 's', 'd']
print(lst)

List delete:

#pop()          list.pop(1)   唯一的一个有返回值的可以print(list.pop(1) )   。按索引、下标删
#remove()    list.remove('hello')  无返回值,按元素去删
#clear()         list.clear()    清空列表,列表还在内存,没删原列表
#del()          del list   在内存中删除列表,按索引删除del list[1] print(list)  可切片删除del list[:3] 


lst = ['hello', 'kitty', 123, 'qqq'] 
data = lst.pop(1)        #按下标删除 返回删除的内容,可以不接收返回值,原列表更改
lst.remove('hello')       #按元素删除
del lst[1:3]                   #切片删
lst.clear()                    #全部删除

List change
Change slice change according to index

#list[2] = 'hello'  把下表2的改为hello
#list[:3] = 'abc'   1先将切片区域元素删除,2按最小元素添加,将添加内容全部增加进去(删除俩个插入三个元素),类似extend。
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''

lst = ['王者', '魔兽', 'dnf', '逆水寒', 'cs']
lst[0] = '少雷'
lst[3] = '吃鸡'
lst[1:3] = 'hah'          #会迭代 更改 ['王者', 'h', 'a', 'h', '逆水寒', 'cs']
lst[1:3] = ['跑跑']       #更改先删除remove() 后添加 extend()
lst[1::2] = ['qq华夏', 'qq仙侠传']  #切片修改的时候 步长不是1 注意元素个数否则会报错 ,(相邻的相当于一个元素位)
print(lst)

List search
1. Search list[3] by index
2. Search list[:] by slice 3. Search list[]
by for loop

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
lst = ['1', '2', 3, 4]
for el in lst:
    print(el)
for i in range(len(lst)):
    print(lst[i])

Sort

#index      list.index('hello')           返回'hello'在列表中下角标的位置。
#sort         list.sort()                   从小到大排序,没有返回值 直接更改list列表
#sort(reverse=True)                      从大到小排序,列表中必须是相同类型,数字和字符串都可排序
#reverse    list.reverse()                 倒叙,将原列表内容倒置,在原列表中改,无返回值


lst = [7, 4, 6, 3, 4]
lst.sort()                               #正序
lst.sort(reverse=True)         #倒序

Nesting of lists

list = [1, 2, 'hello' , 'kitty', ['oldboy'['66', 'qwe', ['333', 'jjjj']]],]
list[4][1][2][1]   ==>取到'jjjj'

Tuple tuple to list directly ->list() type conversion, the
tuple is iterable, accepts any element, no additions, deletions, and changes, only the read-only list is checked. The son class of the tuple cannot be changed, the grandchild class It can be changed.
If there is only one element in the
tuple , (element,) tu = tuple() defines an empty tuple

tu = (1,2,True,[1,2,3],'alex')   

You can’t change tu, but [1,2,3] can be changed

#index    通过元素找索引
#len()      长度
tu[::2]      索引也可以用 可以循环取值

range
range is an iterable object mn is the range, regardless of the q step length, can be used to directly go to odd and even numbers, etc.

1. range(n)                              0 -> n-1
for i in range(10):
    print(i)
2. range(m,n)                            m -> n-1
for i in range(1,10):
    print(i)
3. range(m,n,q)                          m -> n-1 每q个取一个
for i in range(1,10,3):
    print(i)
for i in range(100, 90, -1):
    print(i)

Guess you like

Origin blog.csdn.net/qdPython/article/details/112672933