列表、元组、字典

>>> ports = [21,22,80]
>>> len(ports)
3
>>> ports[0]
21
>>> ports[-1]
80
>>> ports[0:2]
[21, 22]
>>> ports[:2]
[21,22]
>>> ports[:3]
[21,22,80]
>>> ports[:]
[21,22,80]

>>> ports.append('http')
>>> ports.append('ssh')
>>> ports.pop(ports.index('ssh'))
>>> ports.pop(-1)
>>> ports[-1] = 3389

>>> list("abcde")
['a', 'b', 'c', 'd', 'e']

元组不能修改;

元组为单元素时,则需要使用,结尾以便与字符串区分

>>> ports = (1,2,3)
(1, 2, 3)
>>> a = (1)
1
>>> b = (1,)
(1,)

字典,类似于json格式,极快的查找速度

>>> proto = {"http": 80, "https": 443}
>>> proto
{'http': 80, 'https': 443}
>>> proto['http']
80
>>> proto.keys()
['http', 'https']
>>> proto.has_key('ftp')
Flase
>>> proto.has_key('http')
True
>>> for p in proto:
...     print p
http
https
>>> for p in proto:
...     print proto[p]
80
443
>>>

results matching ""

    No results matching ""