如何在 Python 列表中為字串新增字尾/字首?


簡介

本文將向用戶講解如何在 Python 列表中為字串新增字尾/字首。列表會使用這些 “[]” 括號儲存不同資料型別的元素。Python 語言可以進行字串操作。Python 中有各種內建字串方法可以為 Python 列表中的字串新增字尾/字首,例如 reduce()、map() 等。本文演示了兩種方法,可以將指定字元新增到字串的開頭或結尾。

實現此目標的方法多種多樣,一些方法可能只是簡單的迴圈迭代,另一些則可能用到一些函式和方法。

語法

reduce()

Python 中的 collections 模組有許多子類,例如 “defaultdict()” 和 reduce() 方法。reduce() 方法始終使用兩個引數,然後將其縮減為單個值。

map()  

這是一個 Python 內建函式,用於迭代數字列表中的每個專案。

方法

方法 1 - 使用 typing 模組

方法 2 - 使用 functools 模組

方法 1:使用 typing 模組為字串新增字尾/字首的 Python 程式

定義了兩個列表,一個列表中有三個元素,另一個元素是要新增到第一個列表中的字串。然後呼叫函式獲取列表的值並將其打印出來。

演算法

  • 步驟 1 - 從 typing 模組匯入所需的函式

  • 步驟 2 - 定義一個帶有三個引數的函式,用於向列表新增字尾或字首

  • 步驟 3 - 使用 map() 和 lambda 表示式將字尾/字首新增到列表的每個元素

  • 步驟 4 - 列印 append() 函式後的完整列表。

示例

# Importing the list function from typing module
from typing import List

# Defining the function to add suffix/prefix to a list
def add_suffix_prefix_to_list(lst: List[str], suffix: str, prefix: str) -> List[str]:
   # Adding the suffix to list’s element using map() and lambda expression
   suffix_list = list(map(lambda a: a + suffix, lst))

   # Adding the prefix to list’s element using map() and lambda expression
   prefix_list = list(map(lambda a: prefix + a, lst))

   # Returns the complete list after appending
   return suffix_list, prefix_list

# Initializing list with three elements
list_1 = ['note', 'book','pen']

# The additional elements that need to be added to the list
list_2 = 's'

# Calling the function to add suffix/prefix to the list
suffix_list, prefix_list = add_suffix_prefix_to_list(list_1, list_2, list_2)

# Printing the complete lists after adding suffix/prefix
print("suffix addition of element to list", suffix_list)
print("prefix addition of element to list", prefix_list)

輸出

suffix addition of element to list ['notes', 'books', 'pens']
prefix addition of element to list ['snote', 'sbook', 'spen']

方法 2:使用 functools 模組為字串新增字尾/字首的 Python 程式

匯入 functools 庫並定義兩個列表,一個列表中有三個元素,另一個元素是要新增到第一個列表中的字串。然後使用 reduce 函式分別將列表的每個元素新增到字首和字尾中。

演算法

  • 步驟 1 - 從 functools 模組匯入所需的函式

  • 步驟 2 - 使用字串元素初始化列表。

  • 步驟 3 - 初始化另一個列表,以向現有列表新增字尾或字首。

  • 步驟 4 - 使用 reduce() 函式進行追加並列印結果。

示例

# The reduce function is imported
from functools import reduce

# Initializing list with three elements
list_1 = ['note', 'book','pen']

# The additional elements that need to be added to the list
list_2 = 's'

# Adding the suffix to each element of the list using reduce() function
suffix_list = reduce(lambda x, y: x + [y + list_2], list_1, [])

# Adding the prefix to each element of the list using reduce() function
prefix_list = reduce(lambda x, y: x + [list_2 + y], list_1, [])

# Printing the complete lists after adding suffix/prefix
print("suffix addition of element to list", suffix_list)
print("prefix addition of element to list", prefix_list)

輸出

suffix addition of element to list ['notes', 'books', 'pens']
prefix addition of element to list ['snote', 'sbook', 'spen']

結論

本文說明了兩種方法。第一種方法從 typing 庫匯入 map() 和 list() 函式。定義一個函式,用於向列表新增字首和字尾。稍後使用 map() 函式和 lambda 將列表的每個元素分別新增到字首和字尾中。

更新於: 2023-08-25

2K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.