共計 361 個字符,預(yù)計需要花費 1 分鐘才能閱讀完成。
Python 中的 list() 函數(shù)是用于創(chuàng)建一個新的列表對象。它可以將一個可迭代對象(如字符串、元組、字典、集合等)轉(zhuǎn)換為列表。
list() 函數(shù)的用法如下:
- 將可迭代對象轉(zhuǎn)換為列表:
iterable = 'hello'
lst = list(iterable)
print(lst) # ['h', 'e', 'l', 'l', 'o']
- 創(chuàng)建空列表:
empty_lst = list()
print(empty_lst) # []
- 可以接收一個參數(shù) n,創(chuàng)建包含 n 個重復(fù)元素的列表:
repeated_lst = list('abc' * 3)
print(repeated_lst) # ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
需要注意的是,list() 函數(shù)是 Python 內(nèi)置函數(shù),不需要導(dǎo)入任何模塊即可使用。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完