Python3中的list()函数可以将其他类型的数据转换成列表,这种转换功能非常有用。
使用方法
list()函数的使用方法非常简单,只需要在list()函数中传入要转换的数据,就可以将其他类型的数据转换成列表。
# 例子 # 将元组转换成列表 tuple = (1, 2, 3, 4) list_tuple = list(tuple) print(list_tuple) # 输出 [1, 2, 3, 4]
list()函数也可以用来将字符串转换成列表,只需要将字符串传入list()函数中即可。
# 例子 # 将字符串转换成列表 string = 'hello world' list_string = list(string) print(list_string) # 输出 ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
list()函数也可以用来将字典转换成列表,只需要将字典传入list()函数中即可。
# 例子
# 将字典转换成列表
dict = {'name': 'john', 'age': 20}
list_dict = list(dict)
print(list_dict)
# 输出
['name', 'age']
list()函数可以用来将集合转换成列表,只需要将集合传入list()函数中即可。
# 例子
# 将集合转换成列表
set = {1, 2, 3, 4}
list_set = list(set)
print(list_set)
# 输出
[1, 2, 3, 4]
以上就是Python3中list()函数的使用方法,可以用来将其他类型的数据转换成列表,这种转换功能非常有用。