5.Python入門到精通

從列表中獲取元素python

跟數組同樣,咱們能夠經過元素的索引值(index)從列表獲取單個元素,注意,列表索引值是從 0 開始的。shell

['u', 'a', 'b', 'c', 'd', 'e', 'k', 'f']api

>>> member[0]數組

'u'app

>>> member[1]ssh

'a'ide

>>>學習

>>> temp = member[0]spa

>>> member[0] = member[1]3d

>>> member

['a', 'a', 'b', 'c', 'd', 'e', 'k', 'f']

>>> member[1] = temp

>>> member

['a', 'u', 'b', 'c', 'd', 'e', 'k', 'f']

>>> 

 

從列表刪除元素

remove()

>>> member.remove('b')

>>> member

['a', 'u', 'c', 'd', 'e', 'k', 'f']

>>> 

del

>>> del member[0]

>>> member

['u', 'c', 'd', 'e', 'k', 'f']

>>> 

pop()

>>> member.pop()

'f'

>>> member

['u', 'c', 'd', 'e', 'k']

>>> name=member.pop()

>>> name

'k'

>>> member.pop(2)

'd'

>>> 


列表分片(Slice)

利用索引值,每次咱們能夠從列表獲取一個元素,可是咱們老是貪心的,若是一次性須要獲取多個元素,有沒有辦法實現呢?利用列表分片,咱們能夠簡單的實現這個要求。

>>> member[1:3]

['c', 'e']

>>> 

>>> member

['u', 'c', 'e']

>>> member[:3]

['u', 'c', 'e']

>>> member[:]

['u', 'c', 'e']

>>> member[1:]

['c', 'e']

>>> 

列表的一些經常使用操做符

比較操做符

>>> list1 = [123]

>>> list2 = [234]

>>> list1 > list2

False

>>> list1 = [123,456]

>>> list2 = [234,123]

>>> list1 > list2

False

>>> list3=[123,456]

邏輯操做符

>>> (list1 < list2) and (list1 == list3)

True

鏈接操做符

>>> list4 = list1 + list2

>>> list4

[123, 456, 234, 123]

>>> list1 + 'x'

Traceback (most recent call last):

  File "<pyshell#79>", line 1, in <module>

    list1 + 'x'

TypeError: can only concatenate list (not "str") to list

重複操做符

>>> list3 * 3

[123, 456, 123, 456, 123, 456]

>>> list3 *= 3

>>> list3

[123, 456, 123, 456, 123, 456]

>>> list3 *= 5

>>> list3

[123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456]

>>> 

成員關係操做符

>>> 123 in list3

True


>>> 'd' not in list3

True

>>> 123 not in list3

False

>>>

>>> list5 = [123,['d','u'],456]

>>> 'd' in list5

False

>>> 'd' in list5[1]

True

>>> list5[1][1]

'u'

>>> 

列表的小夥伴們


>>> dir(list)

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

>>> list3.count(123)

15

>>> list3.index(123)

0

>>> list3.index(123,3,7)

4

>>> list3.reverse()

>>> list3

[456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123, 456, 123]

>>> list6 = [4,5,1,7,0,9]

>>> list6.sort()

>>> list6

[0, 1, 4, 5, 7, 9]

>>> list6.sort(reverse=True)

>>> list6

[9, 7, 5, 4, 1, 0]

>>> 

關於分片「拷貝」概念的補充


>>> list7 = list6[:]

>>> list7

[9, 7, 5, 4, 1, 0]

>>> list8 = list6

>>> list8

[9, 7, 5, 4, 1, 0]

>>> list6.sort()

>>> list6

[0, 1, 4, 5, 7, 9]

>>> list7

[9, 7, 5, 4, 1, 0]

>>> list8

[0, 1, 4, 5, 7, 9]

>>> 

wKiom1jL192SjFH6AACObeoPSFs699.jpg-wh_50
元組:戴上了枷鎖的列表

因爲和列表是近親關係,因此元組和列表在實際使用上是很是類似的。

咱們這節課主要經過討論元組和列表到底有什麼不一樣來學習元組,醬紫你們就不會以爲總是重複同樣的內容。

咱們主要從如下幾個點來討論學習:

創鍵和訪問一個元組

>>> temp=(1)

>>> temp

1

>>> type(temp)

<class 'int'>

>>> temp2=2,3,4

>>> type(temp2)

<class 'tuple'>

>>> temp = []

>>> type (temp)

<class 'list'>

>>> temp = ()

>>> type(temp)

<class 'tuple'>

>>> temp = (1,)

>>> type(temp)

<class 'tuple'>

>>> 

>>> temp = 1,

>>> type(temp)

<class 'tuple'>

>>> 8 * (8)

64

>>> 8 * (8,)

(8, 8, 8, 8, 8, 8, 8, 8)

>>> 

更新和刪除一個元組

>>> temp = ('d','c','a','b')

>>> temp = temp[:3] + ('f',) + temp[3:]

>>> temp

('d', 'c', 'a', 'f', 'b')

>>> del temp

>>> temp

Traceback (most recent call last):

  File "<pyshell#148>", line 1, in <module>

    temp

NameError: name 'temp' is not defined

>>> 

元組相關的操做符


>>> str1 = 'I love Python'

>>> str1[:6]

'I love'

>>> str1

'I love Python'

>>> str1[6]

' '

>>> str1[5]

'e'

>>> str1[:6] + 'inserstr' + str1[6:]

'I loveinserstr Python'

>>> str1

'I love Python'

>>> 

字符串:各類奇葩的內置方法


wKiom1jL39igLcp3AAHr-n07REg837.jpg-wh_50

wKioL1jL4EfAL2VkAAHdaPv3P6Q978.jpg-wh_50


>>> str2 = 'ivwdcwso'

>>> str2.capitalize()

'Ivwdcwso'

>>> str2='IvwDcwSo'

>>> str2.casefold()

'ivwdcwso'

>>> str2

'IvwDcwSo'

>>> str2.center(40)

'                IvwDcwSo                '

>>> str2.count('w')

2

>>> str2.endswith('w')

False

>>> str2.endswith('o')

True

>>> str3 = 'I\tlove\tPython!'

>>> str3.expandtabs()

'I       love    Python!'

>>> str3.find('efc')

-1

>>> str3.find('o')

3

>>> 


>>> str3

'I\tlove\tPython!'

>>> str3.istitle()

False

>>> str3.join('123')

'1I\tlove\tPython!2I\tlove\tPython!3'

>>> 


>>> str6 = '    I love Python!'

>>> str6.lstrip()

'I love Python!'

>>> str6 = 'i love python'

>>> str6.partition('ov')

('i l', 'ov', 'e python')

>>> str6

'i love python'

>>> str6.replace('python','Python')

'i love Python'

>>> 


>>> str6.split()

['i', 'love', 'python']

>>> 

>>> str6.split('i')

['', ' love python']

>>> 

>>> str7 = '    aaaa     '

>>> str7.strip()

'aaaa'

>>> str7=str7.strip()

>>> str7

'aaaa'

>>> 

相關文章
相關標籤/搜索