Python程式分割字串為自定義長度


在本文中,我們將學習如何在python中按自定義長度分割字串。

使用的方法

以下是完成此任務的各種方法:

  • 使用for迴圈和切片

  • 使用列表推導式、join()和next()函式

示例

假設我們已經獲取了一個**輸入字串**和**自定義長度列表**。我們現在將使用上述方法根據輸入的自定義長度分割輸入字串。

輸入

inputString = 'hitutorialspoint'
customLengths = [4, 1, 6, 5]

輸出

['hitu', 't', 'orials', 'point']

在上面的示例中,首先根據給定的自定義長度分割輸入列表:

4個字元 - 'hitu’

1個字元 - ‘r’

6個字元 - ‘orials’

5個字元 - ‘point

方法1:使用for迴圈和切片

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入字串。

  • 列印輸入字串。

  • 建立一個變數來儲存輸入的自定義長度列表。

  • 建立一個空列表來儲存結果列表。

  • 將起始索引初始化為0。

  • 使用**for迴圈**遍歷自定義長度列表中的每個元素。

  • 使用**append()**函式(在列表末尾新增元素)追加在給定索引之間切片的結果。

  • 將起始索引的值增加自定義長度列表元素。

  • 根據給定的自定義長度分割輸入列表後,列印結果列表。

示例

以下程式使用for迴圈和切片,在根據給定的自定義長度分割輸入列表後返回一個列表:

# input string
inputString = 'hitutorialspoint'

# printing input string
print("Input string: ", inputString)

# input custom lengths list
customLengths = [4, 1, 6, 5]

# empty list for storing a resultant list
outputList = []

# initializing start index as 0
startIndex = 0

# travsering through each element of the custom lengths list
for l in customLengths:

   # appending the custom length string sliced from the
   
   # starting index to the custom length element
   outputList.append(inputString[startIndex: startIndex + l])
   
   # Increment the start Index value with the custom length element
   startIndex += l

# printing the resultant output list
print("Resultant list after splitting by custom lengths:\n", outputList)

輸出

執行上述程式將生成以下輸出:

Input string: hitutorialspoint
Resultant list after splitting by custom lengths:
['hitu', 't', 'orials', 'point']

方法2:使用列表推導式、join()和next()函式

列表推導式

當您希望根據現有列表的值構建新列表時,列表推導式提供了一種更短/簡潔的語法。

next()函式

返回迭代器(如列表、字串元組等)中的下一個專案。如果可迭代物件到達其末尾,您可以新增一個預設返回值來返回。

語法

next(iterable, default)

**join()函式** - join()是Python中的一個字串函式,用於連線由字串分隔符分隔的序列元素。此函式連線序列元素以轉換為字串。

iter()函式

返回給定物件的迭代器。它建立一個每次迭代一個元素的物件。

語法

以下是函式的語法。

iter(object, sentinel)

引數

  • **object** - 此引數指定要建立其迭代器(列表、元組、集合等)的物件

  • **sentinel** - 它是一個表示序列結束的唯一值。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用**iter()**函式返回輸入字串的迭代器(遍歷字串)

  • 遍歷customLengths列表中的每個元素,並使用next()和join()函式連線字串的下一個l(迭代器值)個字元。

  • 根據給定的自定義長度分割輸入列表後,列印結果列表。

示例

以下程式使用列表推導式、join()和next()函式,在根據給定的自定義長度分割輸入列表後返回一個列表:

# input string
inputString = 'hitutorialspoint'

# printing input string
print("Input string: ", inputString)

# input custom lengths list
customLengths = [4, 1, 6, 5]

# Getting the string as an iter object to iterate through it
stringitr = iter(inputString)

# traversing through each element of customLengths list and

# Joining the next l characters of the string
outputList = ["".join(next(stringitr) for i in range(l)) for l in customLengths]

# printing the resultant output list
print("Resultant list after splitting by custom lengths:\n", outputList)

輸出

執行上述程式將生成以下輸出:

Input string: hitutorialspoint
Resultant list after splitting by custom lengths:
['hitu', 't', 'orials', 'point']

結論

在本文中,我們學習瞭如何使用兩種不同的方法將給定的字串分割成不同的長度。此外,我們還學習瞭如何使用iter()方法遍歷字串,以及如何使用next()函式獲取最多n個字元的字串。

更新於: 2023年1月27日

3K+瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.