0

我有几个以下格式的 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])

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

4

3 回答 3

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']}
于 2013-05-16T01:34:39.127 回答
1
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
于 2013-05-16T02:12:30.203 回答
0

如果您有大量列表要处理,您可以考虑将数据直接放入字典中。这可能有助于解决您向 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

产量

你的评价是什么?['Bob', 'Alice', 'Mary Jane']
你怎么看?['鲍勃','爱丽丝','玛丽珍']

于 2013-05-16T03:50:43.053 回答