共計(jì) 573 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
在 Python 中,list() 函數(shù)用于將其他可迭代對(duì)象(如字符串、元組、字典等)轉(zhuǎn)換為列表。其語(yǔ)法如下:
list(iterable)
參數(shù):
– iterable(可迭代對(duì)象):要轉(zhuǎn)換為列表的可迭代對(duì)象。
返回值:返回一個(gè)新的列表。
示例:
string = "Hello"
list_string = list(string)
print(list_string) # 輸出:['H', 'e', 'l', 'l', 'o']
tuple1 = (1, 2, 3)
list_tuple = list(tuple1)
print(list_tuple) # 輸出:[1, 2, 3]
dict1 = {'a': 1, 'b': 2, 'c': 3}
list_dict = list(dict1)
print(list_dict) # 輸出:['a', 'b', 'c']
注意:在使用 list() 函數(shù)轉(zhuǎn)換字典時(shí),只會(huì)返回字典的鍵(而不是值)。如果需要轉(zhuǎn)換為包含鍵值對(duì)的列表,可以使用 dict.items() 方法。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完