描述
這個list()方法采用序列類型並將它們轉換為列表。這用於將給定的元組轉換為列表。
注意- 元組與列表非常相似,唯一的區別是元組的元素值不能改變,元組元素放在括號而不是方括號之間。此函數還將字符串中的字符轉換為列表。
用法
以下是語法list()方法 -
list( seq )
參數
seq- 這是要轉換為列表的元組或字符串。
返回值
此方法返回列表。
示例
下麵的例子展示了 list() 方法的用法。
#!/usr/bin/python3
aTuple = (123, 'C++', 'Java', 'Python')
list1 = list(aTuple)
print ("List elements:", list1)
str = "Hello World"
list2 = list(str)
print ("List elements:", list2)
結果
當我們運行上麵的程序時,它會產生以下結果 -
List elements: [123, 'C++', 'Java', 'Python'] List elements: ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
相關用法
- Python 3 List len()用法及代碼示例
- Python 3 List pop()用法及代碼示例
- Python 3 List index()用法及代碼示例
- Python 3 List extend()用法及代碼示例
- Python 3 List count()用法及代碼示例
- Python 3 List reverse()用法及代碼示例
- Python 3 List insert()用法及代碼示例
- Python 3 List append()用法及代碼示例
- Python 3 List max()用法及代碼示例
- Python 3 List sort()用法及代碼示例
- Python 3 List min()用法及代碼示例
- Python 3 Number tan()用法及代碼示例
- Python 3 os.fstatvfs()用法及代碼示例
- Python 3 os.minor()用法及代碼示例
- Python 3 dictionary cmp()用法及代碼示例
- Python 3 String isupper()用法及代碼示例
- Python 3 os.close()用法及代碼示例
- Python 3 String decode()用法及代碼示例
- Python 3 os.unlink()用法及代碼示例
注:本文由純淨天空篩選整理自 Python 3 - List list() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。