python3.7[列表] 索引切片
python3.7[列表]
索引? 切片 排序
?
?
####
列表.sort 永久排序
?
sorted(列表) 臨時排序
?
###
>>> print(sorted(a))
['abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
>>> a
['tttttt', 'fff', 'f', 'f', 'f', 'f', 'btte', 'bbb', 'asdf', 'aff', 'abc']
>>> a.sort(reverse=True)
>>> a
['tttttt', 'fff', 'f', 'f', 'f', 'f', 'btte', 'bbb', 'asdf', 'aff', 'abc']
>>> a.sort()
>>> a
['abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
>>>
?
?
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
>>> a.reverse()
>>> a
['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'f', 'f', 'tttttt', 'fff', 'f', 'f', 'btte', 'bbb', 'asdf', 'aff', 'abc', '666']
>>>
?
?
##############
列表 增加元素
##########
>>> a
['abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
>>> a.insert(0,'666')
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
>>> a.append ('888')
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt', '888']
>>> a.pop()
'888'
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'f', 'fff', 'tttttt']
?############
刪除
remove(元素)
pop
?
#############
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'fff', 'tttttt']
>>> a.append('f')
>>> a.append('f')
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'f', 'fff', 'tttttt', 'f', 'f']
>>> a.remove('f')
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f']
>>>
?
#####
?
>>> a.extend('aa')
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f', 'a', 'a']
>>> a.extend('aaaaaa')
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
>>>
?
?
######
>>> a.index('f')
6
>>> a.count('f')
4
>>> a
['666', 'abc', 'aff', 'asdf', 'bbb', 'btte', 'f', 'f', 'fff', 'tttttt', 'f', 'f']
>>> len(a)
12
>>>
#########
切片
#######
?
>>> a[1:4:2]
['a', 'a']
>>> a[1::2]
['a', 'a', 'a', 'a', 'f', 'fff', 'f', 'bbb', 'aff', '666']
>>> a[5:2:-2]
['a', 'a']
?###@@@@
?
?
>>> a
['df', 'asdf', 'asd', 'eee', 'as', 'qq']
>>> a.pop('eee')
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
a.pop('eee')
TypeError: 'str' object cannot be interpreted as an integer
>>> a.pop(1)
'asdf'
>>> a
['df', 'asd', 'eee', 'as', 'qq']
>>>
?
##########
?
?
>>> a[0:4:2]
['df', 'eee']
>>>
?
>>> a.index('eee')
2
?
?
>>> print(a[5][2].replace('蘭','kk'))
kk
>>> a
['df', 'asd', 'eee', 'as', 'qq', '武藤蘭']
>>> a[0].upper()
'DF'
>>> a
['df', 'asd', 'eee', 'as', 'qq', '武藤蘭']
>>>
>>> a.index('df')
0
?
增加1個字符 不增加
>>> a.extend("ggg")
>>> a
['df', 'asd', 'eee', 'as', 'qq', '武藤蘭', 'g', 'g', 'g']
>>>
轉載于:https://www.cnblogs.com/xuanbjut/p/11147312.html
總結
以上是生活随笔為你收集整理的python3.7[列表] 索引切片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux更改终端的用户名和主机名的颜色
- 下一篇: c#开发windows应用程序几个小技巧