【问题标题】:How to convert a string into list of tuples in Python如何在 Python 中将字符串转换为元组列表
【发布时间】:2019-12-23 12:28:44
【问题描述】:

我有以下格式的字符串,我发现很难将这些字符串转换为元组 -

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)]

我不知道该怎么做。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: python string list type-conversion


    【解决方案1】:

    您可以使用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)]
    

    【讨论】:

      【解决方案2】:
      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)
      
      1. 首先我们导入正则表达式模块。
      2. 定义要查找的正则表达式模式:两个括号之间的任何内容
      3. 在我们的 text 变量中搜索该模式并返回所有匹配项。
      4. 使用列表理解创建一个元组列表。

      【讨论】:

        【解决方案3】:

        你可以试试这个:

        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))
        

        【讨论】:

          【解决方案4】:

          使用正则表达式 --> 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)]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-03-18
            • 2011-05-16
            • 2014-07-14
            • 1970-01-01
            • 2014-10-18
            • 2014-04-22
            • 2018-07-20
            相关资源
            最近更新 更多