在 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”方法將給定的元組列表轉換為字串之後,使用上述方法。
- 這被分配給一個值。
- 它顯示在控制檯中。
廣告