【问题标题】:Extracting multiple string values of variable length before and after a delimiter in a list在列表中的分隔符之前和之后提取多个可变长度的字符串值
【发布时间】:2013-05-16 01:30:52
【问题描述】:

我有几个 Python 列表,格式如下:

rating = ['What is your rating for?: Bob', 'What is your rating for?: Alice', 'What is your rating for?: Mary Jane']

opinion = ['What is your opinion of?: Bob', 'What is your opinion of?: Alice', 'What is your opinion of?: Mary Jane']

我正在尝试编写一个函数来评估给定列表并从中生成两个数据结构:

  1. 冒号 (:) 后出现的名称列表
  2. 一个字符串变量,其中包含在冒号 (:) 之前重复的文本

理想情况下,这两个项目都将根据原始列表名称命名。此外,分隔符和它后面的第一个空格应该被忽略。

上述两个示例的所需样本输出:

rating_names = ['Bob', 'Alice', 'Mary Jane']
rating_text = 'What is your rating for?'

opinion_names = ['Bob', 'Alice', 'Mary Jane']
opinion_text = 'What is your opinion of?'

我已经能够通过从每个列表项中删除一个固定字符串来使其适用于单个列表,但还没有完全弄清楚如何使它适用于分隔符之前的可变数量的字符和潜在的后面有两个词的名字(例如“玛丽珍”)。

rating_names = ([s.replace('What is your rating for?': ','') for s in rating])

搜索后,看起来像前瞻(12)这样的正则表达式可能是解决方案,但我也无法让它工作。

【问题讨论】:

    标签: python regex string list delimiter


    【解决方案1】:

    使用str.split():

    >>> 'What is your rating for?: Bob'.split(': ')
    ['What is your rating for?', 'Bob']
    

    获取文本和名称:

    >>> def get_text_name(arg):
    ...     temp = [x.split(': ') for x in arg]
    ...     return temp[0][0], [t[1] for t in temp]
    ...
    >>> rating_text, rating_names = get_text_name(rating)
    >>> rating_text
    'What is your rating for?'
    >>> rating_names
    ['Bob', 'Alice', 'Mary Jane']
    

    获取“变量”(您可能指的是“dict”,正如这里所说的):

    >>> def get_text_name(arg):
    ...     temp = [x.split(': ') for x in arg]
    ...     return temp[0][0].split()[-2], [t[1] for t in temp]
    ... 
    >>> text_to_name=dict([get_text_name(x) for x in [rating, opinion]])
    >>> text_to_name
    {'rating': ['Bob', 'Alice', 'Mary Jane'], 'opinion': ['Bob', 'Alice', 'Mary Jane']}
    

    【讨论】:

    • 谢谢埃拉扎。关于根据函数的输入动态生成 _text 和 _names 变量的任何建议?
    【解决方案2】:
    import re
    def gr(l):
        dq, ds = dict(), dict()
        for t in l:
            for q,s in re.findall("(.*\?)\s*:\s*(.*)$", t): dq[q] = ds[s] = 1 
        return dq.keys(), ds.keys()
    
    l = [ gr(rating), gr(opinion) ]
    print l
    

    【讨论】:

      【解决方案3】:

      如果您要处理大量列表,您可以考虑将数据直接放入字典中。这可能有助于解决您向 Elazar 提出的问题。

      代码

      def dict_gen(d, l):
          for s in l:
              question, name = s.split(': ')
              if question not in d:
                  d[question] = []    
              d[question].append(name)
      

      用法

      rating = ['What is your rating for?: Bob', 'What is your rating for?: Alice', 'What is your rating for?: Mary Jane']
      opinion = ['What is your opinion of?: Bob', 'What is your opinion of?: Alice', 'What is your opinion of?: Mary Jane']
      
      results = {}
      dict_gen(results, rating)
      dict_gen(results, opinion)
      
      for key, value in results.items():
          print key, value
      

      产量

      你的评价是什么? ['鲍勃','爱丽丝','玛丽珍']
      你的意见是什么? ['鲍勃','爱丽丝','玛丽珍']

      【讨论】:

        猜你喜欢
        • 2016-09-05
        • 1970-01-01
        • 2018-06-27
        • 2023-03-10
        • 2014-11-24
        • 2020-03-11
        • 1970-01-01
        • 2016-03-13
        • 2015-06-06
        相关资源
        最近更新 更多