拆分,编辑和替换列表中的值

时间:2014-08-22 20:23:52

标签: python list

无法做一些电子邮件文本。我在这样的列表中有参与者注册:

body=['Study: Study 1', 'Date:  Friday, March 28, 2014 3:15 PM - 4:00 PM', 
      'Location: Some Place','Participant: John Doe','Study: Study 1', 
      'Date: Friday, March 28, 2014 4:00 PM - 4:40 PM',
      'Location: Some Place','Participant: Mary Smith']

我是使用python的新手,所以我不确定是否有我想要的操作的特定名称。实际上,我想要的是使用'Participant:标记获取列表项,删除该标记,并将名称拆分为单独的列表项,用于名和姓。所以,像这样:

 body=['Study: Study 1', 'Date:  Friday, March 28, 2014 3:15 PM - 4:00 PM', 
       'Location: Some Place','John' ,'Doe']

我尝试使用类似于here的列表理解:

[item.split(' ')[1:] for item in body if re.match('Participant:*', item)]

给了我一个像这样的嵌套列表:

[['John', 'Doe'],['Mary','Smith']]

但是,我不知道如何将带有名字和姓氏的嵌套列表组成单个列表项,并且不知道如何将它们插回到原始列表中。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:2)

你可以吃蛋糕,然后吃:

[elem
 for line in body
 for elem in (line.split()[1:] if line.startswith('Participant:') else (line,))]

这会在嵌套循环中生成输出,其中内部循环遍历拆分输出或具有一个元素的元组,unsplit list元素:

>>> from pprint import pprint
>>> body=['Study: Study 1', 'Date:  Friday, March 28, 2014 3:15 PM - 4:00 PM', 
...       'Location: Some Place','Participant: John Doe','Study: Study 1', 
...       'Date: Friday, March 28, 2014 4:00 PM - 4:40 PM',
...       'Location: Some Place','Participant: Mary Smith']
>>> [elem
...  for line in body
...  for elem in (line.split()[1:] if line.startswith('Participant:') else (line,))]
['Study: Study 1', 'Date:  Friday, March 28, 2014 3:15 PM - 4:00 PM', 'Location: Some Place', 'John', 'Doe', 'Study: Study 1', 'Date: Friday, March 28, 2014 4:00 PM - 4:40 PM', 'Location: Some Place', 'Mary', 'Smith']
>>> pprint(_)
['Study: Study 1',
 'Date:  Friday, March 28, 2014 3:15 PM - 4:00 PM',
 'Location: Some Place',
 'John',
 'Doe',
 'Study: Study 1',
 'Date: Friday, March 28, 2014 4:00 PM - 4:40 PM',
 'Location: Some Place',
 'Mary',
 'Smith']

答案 1 :(得分:1)

恕我直言,这种事情最简洁,有一个功能:

def do_whatever(lst):
    for item in lst:
        if item.startswith('Participant:'):
           head, tail = item.split(':', 1)
           for name in tail.split():
               yield name
        else:
           yield item

body = list(do_whatever(body))

e.g:

>>> def do_whatever(lst):
...     for item in lst:
...         if item.startswith('Participant:'):
...            head, tail = item.split(':', 1)
...            for name in tail.split():
...                yield name
...         else:
...            yield item
... 
>>> body=['Study: Study 1', 'Date:  Friday, March 28, 2014 3:15 PM - 4:00 PM', 
...       'Location: Some Place','Participant: John Doe','Study: Study 1', 
...       'Date: Friday, March 28, 2014 4:00 PM - 4:40 PM',
...       'Location: Some Place','Participant: Mary Smith']
>>> body = list(do_whatever(body))
>>> body
['Study: Study 1', 'Date:  Friday, March 28, 2014 3:15 PM - 4:00 PM', 'Location: Some Place', 'John', 'Doe', 'Study: Study 1', 'Date: Friday, March 28, 2014 4:00 PM - 4:40 PM', 'Location: Some Place', 'Mary', 'Smith']

对于非常糟糕的功能名称感到抱歉 - 我现在感觉不是很有创意......