• list 列表

列表截取

>>> type([])
<class 'list'>
>>> [1,2,3,4,5]
[1, 2, 3, 4, 5]
>>> type([1,2,3,4,5])
<class 'list'>
>>> [1,2,3,'hello world',5,False,True]
[1, 2, 3, 'hello world', 5, False, True]
>>> type([[1,'str'],[True,2],[3,'three',False])
     
SyntaxError: invalid syntax
>>> type([[1,'str'],[True,2],[3,'three',False]])
<class 'list'>
#注意 组的多级获取操作

 

>>> [[1,'str'],[True,2],[3,'three',False]][0]
[1, 'str']
>>> [[1,'str'],[True,2],[3,'three',False]][0][0]
1
>>> [[1,'str'],[True,2],[3,'three',False]][0][1]
'str'
>>> [[1,'str'],[True,2],[3,'three',False]][0][1][-1:]
'r'
>>> ['str',2,True,[4,False]][0]
'str'
>>> ['str',2,True,[4,False]][3]
[4, False]
>>> ['str',2,True,[4,False]][-1:]
[[4, False]]
>>> ['str',2,True,[4,False]][2:3]
[True]
#注意 通过一个位置获取到是列表里的内容,而通过范围获取到是一个列表,列表里包含所取的内容。

列表拼接

>>> ['str',2,True,[4,False]]+['',77]
['str', 2, True, [4, False], '', 77]
>>> ['str',2,True,[4,False]]*3
['str', 2, True, [4, False], 'str', 2, True, [4, False], 'str', 2, True, [4, False]]
>>> ['str',2,True,[4,False]]*['',77]
Traceback (most recent call last):
  File "<pyshell#80>", line 1, in <module>
    ['str',2,True,[4,False]]*['',77]
TypeError: can't multiply sequence by non-int of type 'list'
  • tuple 元组

>>> type((1,2,3))
<class 'tuple'>
>>> (1,2,3)*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> type((1))
<class 'int'>
>>> (1)
1
>>> (1+1)-2
0
>>> (1,)
(1,)
>>> ()
()
>>> type((1,))
<class 'tuple'>
>>> type(())
<class 'tuple'>

 序列 str list tuple

  • 序列中每一个元素都会分配一个序号,可以根据序号对元素进行操作
  •  切片  [[1,'str'],[True,2],[3,'three',False]][0][1][-1:]   'helllo world'[0:6:2]
  • 3 in [1,2,3,4,5]  len("sdfsfsfd")  max([1,2,3,4,5])    min("sdfsfsfd")
>>> '3' not in '1234567'
False
>>> 3 in [1,2,3,4,5]
True
>>> len("sdfsfsfd")
8
>>> max([1,2,3,4,5])
5
>>> min("sdfsfsfd")
'd'
>>> ['helllo world'][0:6:2]
['helllo world']
>>> 'helllo world'[0:6:2]
'hll'

 

>>> ord('d')
100
>>> ord('c')
99
>>> ord('b')
98
>>> ord('')
Traceback (most recent call last):
  File "<pyshell#107>", line 1, in <module>
    ord('')
TypeError: ord() expected a character, but string of length 0 found
>>> ord(' ')
32
#ord():将字符转换为ASCII码
  • set 集合

>>> type({1,2,3,4,'',True})
<class 'set'>
>>> {1,2,3,4,2,3,4,1,1,1,2,True}
{1, 2, 3, 4}
>>> {1, 2, 3, 4}[0]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    {1, 2, 3, 4}[0]
TypeError: 'set' object is not subscriptable
>>> {1, 2, 3, 4,'abcd'}
{1, 2, 3, 4, 'abcd'}
>>> {1, 2, 3, 4, 'abcd',True}
{1, 2, 3, 4, 'abcd'}
>>> {1, 2, 3, 4, 'abcd',True,False}
{False, 1, 2, 3, 4, 'abcd'}
>>> len({1, 2, 3, 4, 'abcd',True,False})
6
\
>>> len({1, 2, 3, 4, 'abcd',False})
6
>>> True in {1, 2, 3, 4, 'abcd',True,False}
True
>>> True in {1, 2, 3, 4, 'abcd',False}
True
>>> 2 not in {1, 2, 3, 4, 'abcd',True,False}
False
>>> max({1, 2, 3, 4, 'abcd',True,False})
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    max({1, 2, 3, 4, 'abcd',True,False})
TypeError: '>' not supported between instances of 'str' and 'int'
>>> max({1, 2, 3, 4})
4
>>> {1,2,3,4,5,6} - {2,3}
{1, 4, 5, 6}
>>> {1,2,3,4,5,6} & {2,3}
{2, 3}
>>> {1,2,3,4,5,6} | {7,3}
{1, 2, 3, 4, 5, 6, 7}
>>> type({})
<class 'dict'>
>>> set()
set()
>>> type(set())
<class 'set'>
  • dict 字典

>>> type({'Q':"新月打击","W":'苍白之瀑'})
<class 'dict'>
>>> {'Q':"新月打击","W":'苍白之瀑'}
{'Q': '新月打击', 'W': '苍白之瀑'}
>>> {'Q':"新月打击","W":'苍白之瀑'}[0]
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    {'Q':"新月打击","W":'苍白之瀑'}[0]
KeyError: 0
>>> {'Q':"新月打击","W":'苍白之瀑'}['Q']
'新月打击'
>>> {'Q':"新月打击","W":'苍白之瀑','Q':"新月打击"}
{'Q': '新月打击', 'W': '苍白之瀑'}
>>> {'Q':"新月打击","W":'苍白之瀑','Q':"新月打击"}['Q']
'新月打击'
>>> {'Q':"新月打击","W":'苍白之瀑',1:"新月打击"}
{'Q': '新月打击', 'W': '苍白之瀑', 1: '新月打击'}
>>> {'1': '新月打击', 'W': '苍白之瀑', 1: '新月打击1'}[1]
'新月打击1'
>>> {'1': '新月打击', 'W': '苍白之瀑', 1: {'1': '新月打击', 'W': '苍白之瀑', 1: '新月打击1'}}
{'1': '新月打击', 'W': '苍白之瀑', 1: {'1': '新月打击', 'W': '苍白之瀑', 1: '新月打击1'}}
>>> {'1': '新月打击', 'W': '苍白之瀑', [1,2]: '新月打击1'}
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    {'1': '新月打击', 'W': '苍白之瀑', [1,2]: '新月打击1'}
TypeError: unhashable type: 'list'
>>> {'1': '新月打击', 'W': '苍白之瀑', (1,2): '新月打击1'}
{'1': '新月打击', 'W': '苍白之瀑', (1, 2): '新月打击1'}
# value 可以是任意的类型
# key 必须是不可变的
  • xmind

最后修改于 2019-04-06 20:29:54
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇