2012-08-13 42 views
0

我是python的新手,所以對於簡單的問題表示歉意。我當然錯過了讓我困惑的東西。Python:分割字符串多次;嵌套數據結構

這與嵌套,分裂有關,我猜循環,但它不適合我。

因此,這裏是我的原始字符串:

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

我試圖創建一個數據結構 - 字典與內它的名字和分數。

原來這就是我開始:

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全新的,所以我現在還不太瞭解。謝謝。

+0

你不應該命名您的變量列表,列表'()'是BIF(見http://docs.python.org/library/functions.html#列表) – LarsVegas 2012-08-13 13:56:27

回答

2

你的分裂是正確的,所有你需要做的是遍歷值並將其轉換爲快譯通,並輕鬆訪問字典關鍵的元素,你的價值應該是一個列表不是一個字符串...

注意:雖然分裂你已經使用變量名作爲'列表',這不是一個好主意。

檢查這...

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' 
+0

感謝您通過此步行我。這真的很有幫助。這裏我唯一的部分就是爲什麼你使用elem [0]和elem [1] - 當你遍歷字典時,你不想讓數字改變嗎? – user1589244 2012-08-13 21:51:35

+0

你分裂後,你會得到一個只包含2個索引爲0和1的元素的列表,它在這種情況下總是保持不變 – avasal 2012-08-14 03:28:21

+0

啊,是的,這很有道理!謝謝你的支持。 – user1589244 2012-08-14 04:02:01

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'])轉換字符串列表到整數列表中。

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] 
1

的字典可以用兩元組的列表進行初始化:

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 + ,你也可以使用字典理解:

test_scores = {k: v.split(",") for k, v in new_name_scr} 
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']} 
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("|")) 
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]}