数据类型之列表

多个装备,多个爱好,多门课程,多个女朋友等

定义:[]内可以有多个任意类型的值,逗号分隔
 my_girl_friends=['alex','wupeiqi','yuanhao',4,5] #本质my_girl_friends=list([...])
 print(list('hello'))
 print(int('123'))
 print(str(123))

优先掌握的操作:
按索引存取值(正向存取+反向存取):即可存也可以取
 my_girl_friends=['alex','wupeiqi','yuanhao',4,5]

切片(顾头不顾尾,步长)

长度
 print(my_girl_friends.__len__())
 print(len(my_girl_friends))
成员运算in和not in
 print('wupeiqi' in my_girl_friends)

追加
 my_girl_friends[5]=3 #IndexError: list assignment index out of range
 my_girl_friends.append(6)
 print(my_girl_friends)

删除
my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
单纯的删除
 del my_girl_friends[0]
 print(my_girl_friends)

 res=my_girl_friends.remove('yuanhao')
 print(my_girl_friends)
 print(res)
 print(my_girl_friends)

删除并拿到结果:取走一个值
 res=my_girl_friends.pop(2)
 res=my_girl_friends.pop()
 print(res)

 my_girl_friends=['alex','wupeiqi','yuanhao',4,5]
 print(my_girl_friends.pop(0)) #'alex'
 print(my_girl_friends.pop(0)) #'wupeqi'
 print(my_girl_friends.pop(0)) #'yuanhao'
循环
 my_girl_friends=['alex','wupeiqi','yuanhao',4,5]

 i=0
 while i < len(my_girl_friends):
 print(my_girl_friends[i])
 i+=1

 for item in my_girl_friends:
 print(item)

 for i in range(10):
 if i== 3:
 break
 # continue
 print(i)
 else:
 print('===>')

掌握的方法
my_girl_friends=['alex','wupeiqi','yuanhao','yuanhao',4,5]
 my_girl_friends.insert(1,'egon')
 print(my_girl_friends)

 my_girl_friends.clear()
 print(my_girl_friends)

 l=my_girl_friends.copy()
 print(l)

 print(my_girl_friends.count('yuanhao'))

 l=['egon1','egon2']
 my_girl_friends.extend(l)
 my_girl_friends.extend('hello')
 print(my_girl_friends)

 my_girl_friends=['alex','wupeiqi','yuanhao','yuanhao',4,5]
 print(my_girl_friends.index('wupeiqi'))
 print(my_girl_friends.index('wupeiqissssss'))

 my_girl_friends.reverse()
 my_girl_friends=['alex','wupeiqi','yuanhao','yuanhao',4,5]
 my_girl_friends.reverse()
 print(my_girl_friends)


 l=[1,10,4,11,2,]
 l.sort(reverse=True)
 print(l)

 x='healloworld'
 y='he2'

 print(x > y)

 l=['egon','alex','wupei']
 l.sort()
 print(l)

原文地址:https://www.cnblogs.com/liqui/p/8040492.html