【问题标题】:Python: split strings and form dictionaries in the listPython:在列表中拆分字符串并形成字典
【发布时间】:2017-07-20 17:47:06
【问题描述】:

我有一个字符串列表如下:

e = ['Website: Alabama Office of the Attorney General',
 'Toll Free: 1-800-392-5658',
 'Website: State Banking Department',
 'Toll Free: 1-866-465-2279',
 'Website: Department of Insurance',
 'Phone Number: 334-241-4141',
 'Website: Securities Commission',
 'Phone Number: 334-242-2984',
 'Website: Public Service Commission',
 'Toll Free: 1-800-392-8050']

我想通过在“:”处拆分字符串来形成字典,并形成列表中每两个元素的字典,例如:

e = [{'Website': 'Alabama Office of the Attorney General',
 'Toll Free': '1-800-392-5658'},
 {'Website': 'State Banking Department',
 'Toll Free': '1-866-465-2279'},
 {'Website': 'Department of Insurance',
 'Phone Number': '334-241-4141'},
 {'Website': 'Securities Commission',
 'Phone Number': 334-242-2984'},
 {'Website': 'Public Service Commission',
 'Toll Free': '1-800-392-8050'}] 

感谢您一如既往的帮助。

【问题讨论】:

  • 为什么要它在列表中?
  • 请向我们展示您迄今为止为解决问题所做的尝试。你在哪里卡住了?
  • Stack Overflow 不是代码编写服务,请查看How to Ask 并向我们展示您的尝试。
  • 您能否详细说明列表的格式是如何工作的?
  • 对不起,@TemporalWolf 和大家,下次我会更加小心。我就是想不出办法。

标签: python list dictionary


【解决方案1】:

如果您想要每两行的字典。您可以使用:

ei = iter(e)
[{k:v for k,v in (x.split(':',1) for x in xs)} for xs in zip(ei,ei)]

生成:

>>> [{k:v for k,v in (x.split(':',1) for x in xs)} for xs in zip(ei,ei)]
[{'Website': ' Alabama Office of the Attorney General', 'Toll Free': ' 1-800-392-5658'}, {'Website': ' State Banking Department', 'Toll Free': ' 1-866-465-2279'}, {'Website': ' Department of Insurance', 'Phone Number': ' 334-241-4141'}, {'Website': ' Securities Commission', 'Phone Number': ' 334-242-2984'}, {'Website': ' Public Service Commission', 'Toll Free': ' 1-800-392-8050'}]

或者更好的格式:

>>> [{k:v for k,v in (x.split(':',1) for x in xs)} for xs in zip(ei,ei)]
[{'Website': ' Alabama Office of the Attorney General', 'Toll Free': ' 1-800-392-5658'},
 {'Website': ' State Banking Department', 'Toll Free': ' 1-866-465-2279'},
 {'Website': ' Department of Insurance', 'Phone Number': ' 334-241-4141'},
 {'Website': ' Securities Commission', 'Phone Number': ' 334-242-2984'},
 {'Website': ' Public Service Commission', 'Toll Free': ' 1-800-392-8050'}]

如果你想去掉值中的空格,我们可以使用strip():

ei = iter(e)
[{k:v.strip() for k,v in (x.split(':',1) for x in xs)} for xs in zip(ei,ei)]

如果每个字典有 n 行,我们可以使用:

n = 2
ei = iter(e)
[{k:v for k,v in (x.split(':',1) for x in xs)} for xs in zip(*((ei,)*n))]

【讨论】:

  • 我们怎么知道不会有网站、电话号码和免费电话号码?
  • @CoryMadden:我们可以很容易地概括这一点。正在努力。
  • @CoryMadden:修改:)
  • 哦,是的,毫无疑问,这会奏效。我的意思是如果列表是这样的['Website', 'Toll Free', 'Phone Number', 'Website', 'Toll Free', 'Website']。但我可能读得太多了。我认为你无法理解。
  • @CoryMadden:在这种情况下,可以使用iterable.groupby。问题是目前情况似乎并非如此,并且 OP 没有回答 cmets。
【解决方案2】:
# Assumption :  Total 2*n entries are present
ans = []
for i in xrange(0, len(e), 2):
    website = e[i].strip().split(':')
    toll = e[i+1].strip().split(':')
    ans.append({website[0]:website[1], toll[0]:toll[1]})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    相关资源
    最近更新 更多