[英]How to convert a string into list of tuples in Python
我有以下格式的字符串,我发现很难将这些类型的字符串转换为元组 -
text = '[(Apple Fruit, 10.88), (Table Top, 1.09), (Kicks, 1.08), (La Liga, 1.05), (Camp Nou, 1.02), (Football Team, 0.82), (, 0.73), (Hattrick, 0.7), (Free kick, 0.68), (Ballon dOr, 0.6), (, 0.53), (Treble, 0.51), (Vinegar, 0.09), (Ronaldo, 0.07)]'
我想将此字符串转换为元组列表 -
output = [('Apple Fruit', 10.88), ('Table Top', 1.09), ('Kicks', 1.08), ('La Liga', 1.05), ('Camp Nou', 1.02), ('Football Team', 0.82), ('', 0.73), ('Hattrick', 0.7), ('Free kick', 0.68), ('Ballon dOr', 0.6), ('', 0.53), ('Treble', 0.51), ('Vinegar', 0.09), ('Ronaldo', 0.07)]
我不知道该怎么做。 有人可以帮我解决这个问题。
您可以使用convert
函数来splits
序列并构建元组列表。
text = '[(Apple Fruit, 10.88), (Table Top, 1.09), (Kicks, 1.08), (La Liga, 1.05), (Camp Nou, 1.02), (Football Team, 0.82), (, 0.73), (Hattrick, 0.7), (Free kick, 0.68), (Ballon dOr, 0.6), (, 0.53), (Treble, 0.51), (Vinegar, 0.09), (Ronaldo, 0.07)]'
text = text.replace("[","").replace("]","")
def is_digit(str):
return str.lstrip('-').replace('.', '').isdigit()
def convert(in_str):
result = []
current_tuple = []
for token in in_str.split(", "):
chunk = token.replace("(","").replace(")", "")
if is_digit(chunk):
chunk = float(chunk)
current_tuple.append(chunk)
if ")" in token:
result.append(tuple(current_tuple))
current_tuple = []
return result
输出
[('Apple Fruit', 10.88), ('Table Top', 1.09), ('Kicks', 1.08), ('La Liga', 1.05), ('Camp Nou', 1.02), ('Football Team', 0.82), ('', 0.73), ('Hattrick', 0.7), ('Free kick', 0.68), ('Ballon dOr', 0.6), ('', 0.53), ('Treble', 0.51), ('Vinegar', 0.09), ('Ronaldo', 0.07)]
import re
regex = re.compile(r'\((.*?)\)')
text = '[(Apple Fruit, 10.88), (Table Top, 1.09), (Kicks, 1.08), (La Liga, 1.05), (Camp Nou, 1.02), (Football Team, 0.82), (, 0.73), (Hattrick, 0.7), (Free kick, 0.68), (Ballon dOr, 0.6), (, 0.53), (Treble, 0.51), (Vinegar, 0.09), (Ronaldo, 0.07)]'
pairs = regex.findall(text)
list_of_tuples = [tuple(p.split(',')) for p in pairs]
print(list_of_tuples)
text
变量中搜索该模式并返回所有匹配项。你可以试试这个:
import ast
text = '[(Apple Fruit, 10.88), (Table Top, 1.09), (Kicks, 1.08), (La Liga, 1.05), (Camp Nou, 1.02), (Football Team, 0.82), (, 0.73), (Hattrick, 0.7), (Free kick, 0.68), (Ballon dOr, 0.6), (, 0.53), (Treble, 0.51), (Vinegar, 0.09), (Ronaldo, 0.07)]'
comma_added = True
for char in text:
if char == '(' and comma_added:
new_text+='("'
comma_added = False
continue
if char == ',' and not comma_added:
new_text+='"'
comma_added = True
new_text += char
print(ast.literal_eval(new_text))
输出:
[('Apple Fruit', 10.88),
('Table Top', 1.09),
('Kicks', 1.08),
('La Liga', 1.05),
('Camp Nou', 1.02),
('Football Team', 0.82),
('', 0.73),
('Hattrick', 0.7),
('Free kick', 0.68),
('Ballon dOr', 0.6),
('', 0.53),
('Treble', 0.51),
('Vinegar', 0.09),
('Ronaldo', 0.07)]
或者(非常丑!!!):
new_text = text.replace('), ','},').replace('(','("').replace(', ','", ').replace('},','), ')
print(ast.literal_eval(new_text))
使用 Regex --> Lookbehind & Lookahead
。
前任:
import re
import ast
text = '[(Apple Fruit, 10.88), (Table Top, 1.09), (Kicks, 1.08), (La Liga, 1.05), (Camp Nou, 1.02), (Football Team, 0.82), (, 0.73), (Hattrick, 0.7), (Free kick, 0.68), (Ballon dOr, 0.6), (, 0.53), (Treble, 0.51), (Vinegar, 0.09), (Ronaldo, 0.07)]'
text = re.sub(r"(?<=\()([A-Za-z\s]+)", r'"\1"', text) #Convert letters to string
text = re.sub(r"(?<=\()(?=,)", r'""', text) #Replace empty space with empty string.
print(ast.literal_eval(text))
输出:
[('Apple Fruit', 10.88),
('Table Top', 1.09),
('Kicks', 1.08),
('La Liga', 1.05),
('Camp Nou', 1.02),
('Football Team', 0.82),
('', 0.73),
('Hattrick', 0.7),
('Free kick', 0.68),
('Ballon dOr', 0.6),
('', 0.53),
('Treble', 0.51),
('Vinegar', 0.09),
('Ronaldo', 0.07)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.