在 Python 字符串列表中用方括号替换双引号

Replace double quotations with brackets in a Python List of Strings

这是我目前列表的格式:

["'There's no going back', 'pop'", "'Mark my words', 'pop'", "'This love will make you levitate', 'pop'", "'Like a bird, like a bird without a cage', 'pop'"]

我想将其转换为以下格式:

[('There\'s no going back', 'pop'), ('Mark my words', 'pop'), ('This love will make you levitate', 'pop'), ('Like a bird, like a bird without a cage', 'pop')]

所以我需要将输入字符串标记为元组。但我不确定如何做到这一点,因为存在“”,因为它主要是一个字符串。

如果需要额外的上下文,我将以上述格式抓取大量数据,并使用括号格式的朴素贝叶斯分类器对其进行处理。如果它更有效,我愿意尝试不同的方法。

使用replacesplit:

lst = ["'There's no going back', 'pop'", "'Mark my words', 'pop'", "'This love will make you levitate', 'pop'", "'Like a bird, like a bird without a cage', 'pop'"]

print([tuple(x.replace('\'', '').split(',')) for x in lst])

输出:

[('Theres no going back', ' pop'), ('Mark my words', ' pop'), ('This love will make you levitate', ' pop'), ('Like a bird', ' like a bird without a cage', ' pop')]