日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonlen()

內(nèi)置函數(shù)len()用于返回對象的長度,即對象中的多個元素。這里的對象可以是字符串、數(shù)組、列表、元組、字典等。

 **len(object)** # object can be string,sets,tuples,dictionary etc

len()參數(shù):

只接受一個參數(shù)。在這種情況下,序列可以是字符串、字節(jié)、元組、列表或范圍,集合可以是字典、集合或凍結(jié)集合。

參數(shù) 描述 必需/可選
目標(biāo) 序列或集合 需要

len()返回值

如果缺少參數(shù)或傳遞了無效的參數(shù),將引發(fā)類型錯誤異常。

| 投入 | 返回值 | | 一個對象 | 對象的長度(整數(shù)值) |

Python 中len()方法的示例

示例len()如何處理元組、列表和范圍?

 testList = []
print(testList, 'length is', len(testList))

testList = [1, 2, 3]
print(testList, 'length is', len(testList))

testTuple = (1, 2, 3)
print(testTuple, 'length is', len(testTuple))

testRange = range(1, 10)
print('Length of', testRange, 'is', len(testRange)) 

輸出:

[] length is 0
[1, 2, 3] length is 3
(1, 2, 3) length is 3
Length of range(1, 10) is 9 

示例len()如何處理字符串和字節(jié)?

 testString = ''
print('Length of', testString, 'is', len(testString))

testString = 'Python'
print('Length of', testString, 'is', len(testString))

# byte object
testByte = b'Python'
print('Length of', testByte, 'is', len(testByte))

testList = [1, 2, 3]

# converting to bytes object
testByte = bytes(testList)
print('Length of', testByte, 'is', len(testByte)) 

輸出:

Length of  is 0
Length of Python is 6
Length of b'Python' is 6
Length of b'\x01\x02\x03' is 3 

示例len()如何處理字典和集合?

 testSet = {1, 2, 3}
print(testSet, 'length is', len(testSet))

# Empty Set
testSet = set()
print(testSet, 'length is', len(testSet))

testDict = {1: 'one', 2: 'two'}
print(testDict, 'length is', len(testDict))

testDict = {}
print(testDict, 'length is', len(testDict))

testSet = {1, 2}
# frozenSet
frozenTestSet = frozenset(testSet)
print(frozenTestSet, 'length is', len(frozenTestSet)) 

輸出:

{1, 2, 3} length is 3
set() length is 0
{1: 'one', 2: 'two'} length is 2
{} length is 0
frozenset({1, 2}) length is 2 

示例len()如何用于自定義對象?

 class Session:
    def __init__(self, number = 0):
      self.number = number

    def __len__(self):
      return self.number

# default length is 0
s1 = Session()
print(len(s1))

# giving custom length
s2 = Session(6)
print(len(s2)) 

輸出:

0
6 

當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:Pythonlen()
標(biāo)題來源:http://www.5511xx.com/article/coeehgi.html