小甲鱼-010-012列表
生活随笔
收集整理的這篇文章主要介紹了
小甲鱼-010-012列表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
python列表可以沒有類型限制,可以存放不同類型的數(shù)據(jù),如整型、浮點型、字符串、對象、列表、tuple
1.創(chuàng)建列表
#普通列表 autos = ['一汽', '東風(fēng)','上汽','長安', '廣汽'] #混合列表 mix_autos = [1, '大雄', ['機器貓','小夫'],(1,3),{'龍珠':'比克','鐵甲小寶':'卡布達'}] #空列表 empty = []2.添加元素
append追加,extend添加一個列表,insert指定位置添加
empty = ['蛇精', '蝎子精', '穿山甲', '青蛙','瓢蟲'] #追加 empty.append('葫蘆娃') #追加個列表 empty.extend(['大娃','二娃']) #指定位置添加 empty.insert(1, '爺爺') print(empty)3.刪除元素
remove pop del
empty = ['大娃', '二娃', '三娃', '四娃','五娃', '六娃', '七娃'] #根據(jù)列表的值刪除 empty.remove('七娃') #pop默認(rèn)刪除最后一個,也可指定索引 a = empty.pop() empty.pop(4) #根據(jù)索引刪除列表的值 del empty[0] print(empty) #刪除整個列表 del empty4.切片
empty = ['大娃', '二娃', '三娃', '四娃','五娃', '六娃', '七娃']#切片選取部分列表,可以指定初始位置和結(jié)束位置,不指定就選取整個列表 list1 = empty[1:3] list2 = empty[:5] list3 = empty[3:] list4 = empty[:] print(list1) print(list2) print(list3) print(list4)5.常用操作符
5.1比較操作符 > < ==
list1 = [123,456] list2 = [234,567] list3 = [123,567] print(list1 < list2) print(list1 < list3) print(list1 == list2) print(list1 == list3)5.2邏輯操作符 and or not
list1 = [123,456] list2 = [234,567] list3 = [123,567] print((list1 < list2) and (list1 < list3)) print((list1 == list2) or (list1 < list3)) print(not (len(list2) > 3))5.3連接操作符 +
list1 = [123,456]
list2 = [234,567]
list3 = list1 + list2
print(list3)
5.4重復(fù)操作符 *
list1 = [123,456] list3 = list1 * 3 list1 *= 2 print(list1) print(list3)5.5成員關(guān)系操作符 in
empty = ['大娃', '二娃', '三娃', '四娃','五娃', '六娃', '七娃'] print('大娃' in empty) print('蛇精' not in empty) mix_autos = [1, '大雄', ['機器貓','小夫']] print('機器貓' in mix_autos[2])6.列表的內(nèi)置方法
len列表長度
count查看值在列表中出現(xiàn)的次數(shù)
index返回值在列表中的索引
reverse 反序
sort(reverse=False)
?
FAQ
復(fù)制列表要使用切片,因為直接復(fù)制列表并不會生成新列表,依舊指向舊列表。針對列表的所有操作都會被記錄
轉(zhuǎn)載于:https://www.cnblogs.com/csj2018/p/10095129.html
總結(jié)
以上是生活随笔為你收集整理的小甲鱼-010-012列表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 省呱呱典型用户和用户场景
- 下一篇: python中的re模块——正则表达式