python之列表

列表app

向列表添加元素url

append()排序

number = [1 , 2 , 3 , 4 ,5]rem

number.append(9)sort

只能在最後添加一個參數參數

extend()index

number.extend([8 , 10])

添加一個列表到另外一個列表中

>>> yourlist
['A', 'E', 'G']
>>> mylist
[1, 7, 9, 11, 13, 15, 17, 19, 23]
>>> mylist.extend(yourlist)
>>> mylist
[1, 7, 9, 11, 13, 15, 17, 19, 23, 'A', 'E', 'G']
>>> yourlist
['A', 'E', 'G']

insert()

number.insert(1, 11)

在第二位插入11,能夠在任何位置插入參數

刪除列表元素

number.pop() #刪除最後一個元素

number.pop(i) #刪除任意一個元素

刪除列表

del(number)

刪除指定元素

del(number[i])

刪除指定元素

number.remove(3)

修改指定元素

number[0] = 9

切片  顧頭不顧尾

>>> mylist
[1, 7, 9, 13, 11, 15, 17, 19]
>>> mylist[1:4]
[7, 9, 13]
>>> mylist[0:2]
[1, 7]
>>> mylist[:3]
[1, 7, 9]
>>> mylist[:]
[1, 7, 9, 13, 11, 15, 17, 19]
>>> mylist[::2]
[1, 9, 11, 17]
>>> mylist[::3]
[1, 13, 17]
>>> mylist[0:-7]
[1]
>>> mylist[-7:-5]
[7, 9]

獲取下標

>>> mylist.index(13)
3
>>> print(mylist.index(9))
2

反轉

>>> mylist
[1, 7, 9, 13, 11, 15, 17, 19]
>>> mylist.reverse()
>>> mylist
[19, 17, 15, 11, 13, 9, 7, 1]

排序

按照ASCII碼

>>> mylist[1, 7, 9, 23, 11, 13, 15, 17, 19]>>> mylist.sort()>>> mylist[1, 7, 9, 11, 13, 15, 17, 19, 23]

相關文章
相關標籤/搜索