溫馨提示×

python工廠函數有哪些

九三
273
2021-02-20 12:42:22
欄目: 編程語言

python工廠函數有哪些

python中常見的工廠函數有以下幾種

1.list()

list()函數的作用是生成一個列表。

>>> l=list('python')

>>> l

['p', 'y', 't', 'h', 'o', 'n']

2.dict()

dict()函數的作用是生成一個字典。

>>> dict()

{}

>>> dict(one=1,two=2)

{'two': 2, 'one': 1}

>>> dict(zip(('one','two'),(1,2)))

{'two': 2, 'one': 1}

>>> dict([('one',1),('two',2)])

{'two': 2, 'one': 1}

>>> dict([['one',1],['two',2]])

{'two': 2, 'one': 1}

>>> dict((('one',1),('two',2)))

{'two': 2, 'one': 1}

>>> dict((['one',1],['two',2]))

{'two': 2, 'one': 1}

3.set()

set()函數的作用是生產可變集合。

>>> s=set('python')

>>> s

set(['h', 'o', 'n', 'p', 't', 'y'])

>>> s.add(825)#可變集合

>>> s

set(['h', 'o', 'n', 'p', 't', 'y', 825])

4.frozenset()

frozenset()函數的作用是生成不可變集合。

>>> s=frozenset('python')

>>> s

frozenset(['h', 'o', 'n', 'p', 't', 'y'])

>>> s.add()#不可變集合

AttributeError: 'frozenset' object has no attribute 'add'

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女