0

我是 python 新手,所以对简单的问题表示歉意。我当然错过了一些让我感到困惑的东西。

这与嵌套、拆分有关,我猜是 for 循环,但它不适合我。

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

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 的新手,所以我还不太了解。谢谢你。

4

7 回答 7

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'
于 2012-08-13T07:09:30.627 回答
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]
于 2012-08-13T07:15:24.380 回答
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理解:

test_scores = {k: v.split(",") for k, v in new_name_scr}
于 2012-08-13T07:16:18.190 回答
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]将列表解压缩为两个变量,以便 name 包含列表元素 0,并对列表元素 1 进行评分。

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

于 2012-08-13T07:13:08.787 回答
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']}
于 2012-08-13T07:17:39.640 回答
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("|"))
于 2012-08-13T07:21:31.483 回答
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]}
于 2012-08-13T07:25:35.657 回答