Iterable是指可以迭代的数据结构,可以使用for循环来遍历。List是一种有序的数据结构,可以使用索引来访问元素。如果要将Iterable转换为List,可以使用list()函数。下面是一个示例:
# 将字符串转换为列表 str = "hello world" lst = list(str) print(lst) # 结果:['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] # 将元组转换为列表 tup = (1, 2, 3, 4, 5) lst = list(tup) print(lst) # 结果:[1, 2, 3, 4, 5] # 将字典转换为列表 dic = {'name':'Tom', 'age':18} lst = list(dic) print(lst) # 结果:['name', 'age']
可以看到,list()函数可以将字符串、元组、字典等Iterable类型的数据转换为列表,从而可以方便地使用索引来访问其中的元素。