Python 如何从列表创建逗号分隔的字符串

Python 如何从列表创建逗号分隔的字符串

在Python中,列表是一种有序的序列,可以包含多种对象类型,如整数、字符或浮点数。在其他编程语言中,列表等同于数组。方括号[]用于表示它,逗号(,)用于分隔列表中的两个项。

在本文中,我们将展示如何使用Python从列表中获取逗号分隔的字符串。−

  • 使用join()方法

  • 使用join()和map()函数

  • 使用join()和列表推导式

假设我们已经有一个包含一些随机元素的列表。我们将返回由给定列表中的逗号分隔的字符串。

方法1:使用join()方法

步骤

以下是执行所需任务的算法/步骤−

  • 创建一个列表并向其中添加一些虚拟字符串。

  • 通过将列表作为参数传递给join()函数(join()是Python中用于连接由字符串分隔的序列元素以形成字符串的字符串函数)来获取逗号分隔的字符串,并创建一个变量来存储它。在这里,我们将逗号(,)作为分隔符将字符串分隔开。

  • 打印逗号分隔字符串的结果。

示例

以下程序使用join()函数从列表中创建逗号分隔的字符串−

# input list
lst = ['hello', 'hi', 'TutorialsPoint', 'python']

# getting the comma-separated string from the list
resultString = ','.join(lst)

# printing the result comma-separated string
print('The result string after joining the list with comma separated: ')
print(resultString)

输出

在执行时,上述程序将生成以下输出 –

The result string after joining the list with comma separated:
hello,hi,TutorialsPoint,python

方法2:使用join()和map()函数

map()函数接受类似于列表、元组、字符串等可迭代对象。随后,map()函数使用指定的函数映射可迭代对象的项。

map(function, iterable)

请考虑下面的列表,

list = ['hello', 'hi', 'TutorialsPoint', 'python', 1, 45, 2.45]

其中包含字符串、整数和浮点数。当我们使用join()函数时,会出现以下类型错误。

TypeError: sequence item 4: expected str instance, int found

让我们来看看如何解决这个问题。

步骤

以下是执行所需任务的算法/步骤:

  • 创建一个变量来存储输入列表。

  • 通过将 str 函数、list 作为参数传递给 map() 函数,从列表中获取逗号分隔的字符串。它将输入列表的每个元素映射到给定的 str 函数并返回结果列表项。使用 join() 函数(它是 Python 中一个字符串函数,用于连接通过字符分隔的序列的元素。此函数将序列元素连接起来形成一个字符串),然后将结果转换为以逗号分隔的字符串。

  • 打印结果逗号分隔的字符串。

示例

以下程序使用 for 循环(朴素方法)从列表中创建一个逗号分隔的字符串 –

# input list
lst = ['hello', 'hi', 'TutorialsPoint', 'python', 1, 45, 2.45]

# getting the comma-separated string from the list
resultString = ','.join(map(str, lst))

# printing the result comma-separated string
print('The result string after joining the list with comma separated: ')
print(resultString)

输出

在执行上述程序后,将生成以下输出:

The result string after joining the list with comma separated:
hello,hi,TutorialsPoint,python,1,45,2.45

我们以一个带有一些随机字符串、整数和浮点数值的列表为例。然后,使用map()方法,我们将列表的每个元素转换为字符串,并使用join()函数获得结果。

方法3:使用join()和列表推导

步骤

以下是执行所需任务的算法/步骤- 创建一个变量来存储输入列表。 使用for循环和列表推导将列表中的元素转换为字符串,并使用str()函数获取逗号分隔的字符串(str()函数将提供的值转换为字符串。join()函数是Python中的一个字符串函数,用于连接由字符串分隔符分隔的序列元素,将结果转换为字符串)。 打印逗号分隔的字符串结果。

示例

以下程序使用join()和列表推导将列表转换为逗号分隔的字符串-

# input list
lst = ['hello', 'hi', 'TutorialsPoint', 'python', 1, 45, 2.45,True]

# getting the comma-separated string from the list
resultString = ",".join([str(item) for item in lst if item])

# printing the result comma-separated string
print('The result string after joining the list with comma separated: ')
print(resultString)

输出

执行上述程序后,将生成以下输出结果:

The result string after joining the list with comma separated:
hello,hi,TutorialsPoint,python,1,45,2.45,True

我们使用一个例子列表,其中包含一些随机的字符串、整数、浮点数或布尔值。然后,使用列表推导式和for循环将列表的每个元素转换为字符串,并使用join()函数获取结果。

结论

在本教程中,我们介绍了三种不同的方法将字符串列表转换为逗号分隔的字符串。我们还学习了如何使用列表推导式对列表项进行精确操作。随后我们学习了如何使用str()函数将异构列表中的每个项转换为字符串,以及如何使用map()方法对列表应用特定的函数,如本文中的str()函数将列表的每个项转换为字符串。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程