list()
函數有助於返回 Python 中的列表對象。python 中的列表是有序的,並且有精確的計數。列表組件被編入索引,因此索引從零開始。
**list([iterable])** # object can be string,sets,tuples,dictionary etc
只接受一個參數。在這種情況下,序列可以是字符串,元組和集合可以是集合,字典。
參數 | 描述 | 必需/可選 |
---|---|---|
可迭代的 | 可以是序列或集合或任何迭代器對象的對象 | 可選擇的 |
它返回一個列表,並且只有一個參數。
| 投入 | 返回值 |
| 沒有參數 | 空列表 |
| 可迭代通過 | 由可重複項目組成的列表 |
# empty list
print(list())
# vowel string
vowel_string = 'aeiou'
print(list(vowel_string))
# vowel tuple
vowel_tuple = ('a', 'e', 'i', 'o', 'u')
print(list(vowel_tuple))
# vowel list
vowel_list = ['a', 'e', 'i', 'o', 'u']
print(list(vowel_list))
輸出:
[]
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']
['a', 'e', 'i', 'o', 'u']
# vowel set
vowel_set = {'a', 'e', 'i', 'o', 'u'}
print(list(vowel_set))
# vowel dictionary vowel_dicti 1, 'e': 2, 'i': 3, 'o':4, 'u':5}
print(list(vowel_dictionary))
輸出:
['a', 'o', 'u', 'e', 'i']
['o', 'e', 'a', 'u', 'i']
# objects of this class are iterators
class PowTwo:
def __init__(self, max):
self.max = max
def __iter__(self):
self.num = 0
return self
def __next__(self):
if(self.num >= self.max):
raise StopIteration
result = 2 ** self.num
self.num += 1
return result
pow_two = PowTwo(5)
pow_two_iter = iter(pow_two)
print(list(pow_two_iter))
輸出:
[1, 2, 4, 8, 16]
原創文章,作者:I9BWU,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/126706.html