在 Python 中扁平化元组列表为字符串


当需要将元组列表扁平化为字符串格式时,可以使用“str”方法和“strip”方法。

可以使用列表来存储异构值(即任何数据类型的数据,如整数、浮点数、字符串等)。

元组列表基本上包含包含在列表中的元组。“strip”方法将移除特定的字符/值。“str”方法将把给定的数据类型转换为字符串数据类型。

以下是该方法的演示:

示例

在线演示

my_list = [(11, 14), (54, 56), (98, 0), (13, 76)]

print("The list is : ")
print(my_list)

my_result = str(my_list).strip('[]')

print("The list of tuple flattened to string is: ")
print(my_result)

输出

The list is :
[(11, 14), (54, 56), (98, 0), (13, 76)]
The list of tuple flattened to string is:
(11, 14), (54, 56), (98, 0), (13, 76)

说明

  • 定义了一个元组列表,并显示在控制台中。
  • strip 方法用于移除“[]”括号。
  • 在使用“str”方法将给定的元组列表转换为字符串之后,使用上述方法。
  • 这被分配给一个值。
  • 它显示在控制台中。

更新时间:12-Mar-2021

285 次浏览

开启你的 事业

通过完成课程来获得认证

开始
广告
© . All rights reserved.