Python:多次拆分字符串;嵌套数据结构

时间:2012-08-13 07:02:51

标签: python

我是python的新手,所以为简单的问题道歉。我当然错过了令我困惑的事情。

这与嵌套,分裂有关,我猜测循环,但它不适合我。

所以这是我原来的字符串:

name_scr = 'alvinone-90,80,70,50|simonthree-99,80,70,90|theotwo-90,90,90,65'

我正在尝试创建一个数据结构 - 带有名称和分数的dict。

所以这就是我的开始:

    test_scr = { } 
    new_name_scr = [list.split('-') for list in name_scr.split('|')]
# [['alvinone', '90,80,70,50'], ['simonthree', '99,80,70,90'], ['theotwo', '90,90,90,65']]

# this is not  right, and since the output is a list of lists, I cannot split it again 

我在逗号中第三次陷入困境。那么我尝试以下方法:

test_scores = {}
for student_string in name_scr.split('|'):
  for student in student_string.split('-'):
    for scores in student.split(','):
      test_scores[student] = [scores]

#but my result for test_scores (below) is wrong
#{'alvinone': ['alvinone'], '99,80,70,90': ['90'], 'theotwo': ['theotwo'], '90,80,70,50': ['50'], '90,90,90,65': ['65'], 'simonthree': ['simonthree']}

我希望它看起来像这样:

  {'alvinone': [99 80 70 50], 'simonthree': [90 80 70 90], 'theotwo': [90 90 90 65]} 

所以当我这样做时:

print test_scores['simonthree'][2] #it's 70

请在这里帮助我。请记住,我是python的新手,所以我还不太了解。谢谢。

7 个答案:

答案 0 :(得分:2)

你的分裂是正确的,你需要做的就是迭代值并将其转换为dict,并且要轻松访问dict键的元素,你的值应该是一个列表而不是字符串......

注意:在拆分时,您使用变量名称作为“列表”,这不是一个好主意。

检查一下......

In [2]: str = 'alvinone-90,80,70,50|simonthree-99,80,70,90|theotwo-90,90,90,65'

In [3]: str_list = [l.split('-') for l in str.split('|')]

In [4]: str_list
Out[4]:
[['alvinone', '90,80,70,50'],
 ['simonthree', '99,80,70,90'],
 ['theotwo', '90,90,90,65']]

In [5]: elem_dict = {}

In [6]: for elem in str_list:
   ...:     elem_dict[elem[0]] = [x for x in elem[1].split(',')]
   ...:

In [7]: print elem_dict
{'simonthree': ['99', '80', '70', '90'], 'theotwo': ['90', '90', '90', '65'], 'alvinone': ['90
', '70', '50']}

In [8]: elem_dict['simonthree'][2]
Out[8]: '70'

答案 1 :(得分:1)

name_scr = 'alvinone-90,80,70,50|simonthree-99,80,70,90|theotwo-90,90,90,65'
test_scr = {}

for persinfo in name_scr.split('|'):
    name,scores = persinfo.split('-')
    test_scr[name] = map(int,scores.split(','))

print test_scr

name,scores = [elm0, elm1]将列表解压缩为两个变量,以便名称包含列表元素0和分数列表元素1.

map(int,['0', '1', '2'])将字符串列表转换为整数列表。

答案 2 :(得分:1)

>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> for i in s.split('|'):
...   k = i.split('-')
...   d[k[0]] = k[1].split(',')
... 
>>> d['simonthree']
['99', '80', '70', '90']

如果您想将它们设为int

>>> for i in s.split('|'):
...   k = i.split('-')
...   d[k[0]] = map(int,k[1].split(','))
... 
>>> d['simonthree']
[99, 80, 70, 90]

答案 3 :(得分:1)

可以使用两元组列表初始化dict:

new_name_scr = [x.split('-') for x in name_scr.split('|')]
test_scores = dict((k, v.split(",")) for k, v in new_name_scr)

如果你使用python2.7 +,你也可以使用dict comprehensions:

test_scores = {k: v.split(",") for k, v in new_name_scr}

答案 4 :(得分:0)

>>> splitter = lambda ch: lambda s: s.split(ch)
>>> name_scr = 'alvinone-90,80,70,50|simonthree-99,80,70,90|theotwo-90,90,90,65'
>>> dict( (k,v.split(',')) for k,v in map(splitter('-'), name_scr.split('|')))

{'simonthree': ['99', '80', '70', '90'], 'theotwo': ['90', '90', '90', '65'], 'alvinone': ['90', '80', '70', '50']}

答案 5 :(得分:0)

str = 'alvinone-90,80,70,50|simonthree-99,80,70,90|theotwo-90,90,90,65'
a = dict((i.split("-")[0], [x for x in i.split("-")[1].split(',')])  for i in str.split("|"))

答案 6 :(得分:0)

这看起来像PyYAML的完美用例。

import yaml

# first bring the string into shape
name_scr_yaml = (
    name_scr.replace("|","\n").replace("-",":\n  - ").replace(",","\n  - "))
print name_scr_yaml

# parse string:
print yaml.load(name_scr_yaml)

输出是:

alvinone:
  - 90
  - 80
  - 70
  - 50
simonthree:
  - 99
  - 80
  - 70
  - 90
theotwo:
  - 90
  - 90
  - 90
  - 65
{'simonthree': [99, 80, 70, 90], 'theotwo': [90, 90, 90, 65], 'alvinone': [90, 80, 70, 50]}
相关问题