如何在Python列表中添加后缀/前缀?

如何在Python列表中添加后缀/前缀?

介绍

在本文中,用户将了解如何在Python列表中添加后缀/前缀。列表会在这些“[]”括号中存储不同数据类型的元素。在Python语言中,可以进行字符串操作。Python提供了多种内置字符串方法,可以在Python列表中添加后缀/前缀,如reduce(),map()等。本文演示了两种方法,可以在字符串的起始或末尾添加指定字符。

可以使用不同的方法来实现这一操作,有些可能是简单的循环迭代,而其他可能是一些函数和方法。

语法

reduce()

Python中的集合模块有许多子类,如”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']

Approach 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分别将列表的每个元素添加到前缀和后缀中。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程