新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python list()
list()函數(shù)有助于返回 Python 中的列表對象。python 中的列表是有序的,并且有精確的計(jì)數(shù)。列表組件被編入索引,因此索引從零開始。

南湖網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,南湖網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為南湖上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的南湖做網(wǎng)站的公司定做!
**list([iterable])** # object can be string,sets,tuples,dictionary etc
列表()參數(shù):
只接受一個參數(shù)。在這種情況下,序列可以是字符串,元組和集合可以是集合,字典。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 可迭代的 | 可以是序列或集合或任何迭代器對象的對象 | 可選擇的 |
列表()返回值
它返回一個列表,并且只有一個參數(shù)。
| 投入 | 返回值 | | 沒有參數(shù) | 空列表 | | 可迭代通過 | 由可重復(fù)項(xiàng)目組成的列表 |
Python 中list()方法的示例
示例 1:從字符串、元組和列表創(chuàng)建列表
# 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']示例 2:從集合和字典創(chuàng)建列表
# 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']示例 3:從迭代器對象創(chuàng)建列表
# 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] 新聞標(biāo)題:創(chuàng)新互聯(lián)Python教程:Python list()
標(biāo)題網(wǎng)址:http://fisionsoft.com.cn/article/dheiopi.html


咨詢
建站咨詢
