-2
我有一个特殊字符的列表#这是子分组我想从子列表的列表字典。Python列表到Python中的字典,与选定的列表
list1 = ['#model1','path1','path2','path4','path5','#model2','path12'
'path13','#model3','path4','path6']
## Just wanted to make dictionary such that whenever I see a string with # sign
## untill the next # sign here #model2 that is in dictionary entry
##(model1,('path1','path2','path4','path5')) and so on...
def get_me_hash_index(my_list):
index = [idx for idx,x in enumerate(my_list) if x.strip().find('#')!=-1]
return index
def get_new_dict(my_list,hash_position):
return {my_list[hash_position[idx-1]]:my_list[hash_position[idx-1]+1:hash_position[idx]] for idx in range(len(hash_position))}
hash_pos = get_me_hash_index(list1)
new_dict = get_new_dict(list1,hash_pos)
print hash_pos
print new_dict
结果是:
[0, 5, 7]
{'#model1': ['path1', 'path2', 'path4', 'path5'], '#model3': [], '#model2': ['path12path13']}
有点不工作model3和MODEL2。
我不太了解结构。你能把它描述得更好一点吗? – 2014-09-12 15:26:57
它的一个列表[first_three,a,b,c,second_three,d,e,f ....,last_three,x,y,z]只是想做first_three:(a,b,c)second_three:(d ,e,f)..所以在..使用字典..希望它没有明确.. – user2322154 2014-09-12 15:30:23
@wooble我没有期待的代码,但一些想法,我写这个问题的方式是让你感到困惑,我已经发布我工作的部分代码。谢谢 – user2322154 2014-09-12 17:30:32