2017-04-10 93 views
0

我有一个列表,其中每个列表是一个句子。我试图将这个列表转换成一个列表字典,每个单独的句子都有一个关键字。我的这种尝试为每个单词提供了一个关键字,而不是每个单独的句子。这是我的输入当前的样子。将列表的列表换行为列表的字典中断行

{1: '[[try', 2: 'not', 3: 'become', 4: 'man', 5: 'success', 6: 'but', 7: 
'rather', 8: 'try', 9: 'become', 10: 'man', 11: 'value]', 12: '[look', 13: 
'deep', 14: 'into', 15: 'nature', 16: 'and', 17: 'then', 18: 'you', 19: 
'will', 20: 'understand', 21: 'everything', 22: 'better]', 23: '[the', 24: 
'true', 25: 'sign', 26: 'intelligence', 27: 'not', 28: 'knowledge', 29: 
'but', 30: 'imagination]', 31: '[we', 32: 'cannot', 33: 'solve', 34: 'our', 
35: 'problems', 36: 'with', 37: 'the', 38: 'same', 39: 'thinking', 40: 
'used', 41: 'when', 42: 'created', 43: 'them]', 44: '[weakness', 45: 
'attitude', 46: 'becomes', 47: 'weakness', 48: 'character]', 49: '["you', 
50: 'cant', 51: 'blame', 52: 'gravity', 53: 'for', 54: 'falling', 55: 
'love"]', 56: '[the', 57: 'difference', 58: 'between', 59: 'stupidity', 60: 
'and', 61: 'genius', 62: 'that', 63: 'genius', 64: 'has', 65: 'its', 66: 
'limits]]'} 

我想这个代替:提前

{1:'[[try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try', 
'become', 'man', 'value]', 2: '[look', 'deep', 'into', 'nature', 'and', 
'then', 'you', 'will', 'understand', 'everything', 'better]', 3:'[the', 
'true', 'sign', 'intelligence', 'not', 'knowledge', 'but', 'imagination]', 
4:'[we', 'cannot', 'solve', 'our', 'problems', 'with', 'the', 'same', 
'thinking', 'used', 'when', 'created', 'them]', 5: '[weakness', 'attitude', 
'becomes', 'weakness', 'character]', 6:'["you', 'cant', 'blame', 'gravity', 
'for', 'falling', 'love"]', '7: [the', 'difference', 'between', 'stupidity', 
'and', 'genius', 'that', 'genius', 'has', 'its', 'limits]]'} 

谢谢!

我的列表清单如下:

['[[try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try', 
'become', 'man', 'value]', '[look', 'deep', 'into', 'nature', 'and', 
'then', 'you', 'will', 'understand', 'everything', 'better]', '[the', 
'true', 'sign', 'intelligence', 'not', 'knowledge', 'but', 'imagination]', 
'[we', 'cannot', 'solve', 'our', 'problems', 'with', 'the', 'same', 
'thinking', 'used', 'when', 'created', 'them]', '[weakness', 'attitude', 
'becomes', 'weakness', 'character]', '["you', 'cant', 'blame', 'gravity', 
'for', 'falling', 'love"]', '[the', 'difference', 'between', 'stupidity', 
'and', 'genius', 'that', 'genius', 'has', 'its', 'limits]]'] 
+0

我认为你想要的输出是不正确的......除非我完全误解。你想要一个键(整数)映射到一个标记列表(而不是你显示的字符串)?即,'{1:['try',...,'value'],2:['look',...]}'或者那是错的? – Tgsmith61591

回答

0

试试这个:

i = "hi world\n this is python\nyo" 

sentences = i.split("\n") 
index = 1 
my_map = {} 
for sentence in sentences: 
    words = sentence.split(" ") 
    my_map[index] = words 
    index += 1 
print(my_map) 

而只是你的输入代替我。这将输入除以新行字符,然后为每个句子将单词列表映射到索引。

+0

我不确定你在说什么,它确实增加了。 'index + = 1' – CamJohnson26

+0

啊我现在看到了,我的不好。 – JacobIRR

+0

不用担心,枚举是一种更好的方式,肯定 – CamJohnson26

0

我认为你需要有你添加一个在每次创建一个关键时间整型变量,像这样

x = [['try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try','become', 'man', 'value'], 
    ['look', 'deep', 'into', 'nature', 'and','then', 'you', 'will', 'understand', 'everything', 'better']] 

newdct = {} 
dct_key = 1 
for sublist in x: 
    newdct [dct_key] = sublist 
    dct_key += 1 
dct_key = 1 

这样就会打印出清单,以便:

for k in newdct: 
    print (newdct [dct_key]) 
    dct_key += 1 

打印['try','not','become','man','success','but','rather','try','become','man','value'] ['look' ,'深','进','自然','和','然后','你','会','理解','所有','更好']

此打印整个字典

print (newdct) 

打印 {1: '尝试', '不是', '变成', '人', '成功', '但是', '宁',“尝试','成为','人','价值'],2:['look','deep','into','nature','and','then','you','will' “理解”,“一切”,“更好”]}

,使他们的单串每个辞典键:

for k in newdct: 
    newdct [k] = ' '.join(newdct [k]) 
print (newdct) 

打印{1:“尽量不要成为男人的成功,而是试图成为人类价值',2:'看看de ep进入大自然,然后你会更好地理解一切'}

0

不考虑标点符号...我看见你在以前的文章中进行了处理:

# This is whatever your list of sentences is (before its broken into words) 
sentence_list = ['hey there', 'how bout that', 'lookie here'] 
# Make a new dict based on the size of the sentence_list 
d = dict.fromkeys(range(1, len(sentence_list) + 1)) 
# Loop over the sentence_list to fill in the values for the dict 
for ix, sentence in enumerate(sentence_list): 
    # Assign each associated key to a list of words per sentence 
    d[ix+1] = sentence.split() 

# result is: 
{1: ['hey', 'there'], 2: ['how', 'bout', 'that'], 3: ['lookie', 'here']} 
0

为了简化@ new_to_coding的回答,请尝试使用zip方法,在这种情况下将需要两个列表,而“拉链”对应的元素汇集成一本字典:

>>> x = [['try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try','become', 'man', 'value'], 
     ['look', 'deep', 'into', 'nature', 'and','then', 'you', 'will', 'understand', 'everything', 'better']] 
>>> dict(zip(range(1, len(x) + 1), x)) 
{1: ['try', 'not', 'become', 'man', 'success', 'but', 'rather', 'try', 'become', 'man', 'value'], 2: ['look', 'deep', 'into', 'nature', 'and', 'then', 'you', 'will', 'understand', 'everything', 'better']} 
0

虽然你的问题并没有具体说明你的原始列表的样子,回答你的问题,我要承担它看起来像这样

sentence_list = [['this', 'is', 'sentence', 'one'], ['this', 'is', 'sentence', 'two']] 

创建一个字典,其中的关键是内部列表

word_dict = {} 


for i in range(len(sentence_list)): 
    word_dict[i] = sentence_list[i] 

到呼叫要打印的(word_dict)的数量将返回以下

{0: ['this', 'is', 'sentence', 'one'], 1: ['this', 'is', 'sentence', 'two']} 

,如果你想要的字典开始在一个代码是这样的:

word_dict = {} 

for i in range(1, len(sentence_list) + 1): 
    word_dict[i] = sentence_list[i - 1] 

到调用印刷(word_dict)返回以下内容:

{1: ['this', 'is', 'sentence', 'one'], 2: ['this', 'is', 'sentence', 'two']} 
+0

我发布了我的列表清单。我尝试了所有的答案,但我仍然得到不需要的输出。我的列表清单有问题吗? – Amaranthus

+0

是的,列表的列表以某种方式存储在您发布的帖子中的示例中的一个字符串中。在清单列表前后查看撇号。 – Trent

0

它看起来不像你有一个列表清单。而是你有一个字符串列表。注:

list_of_lists = [['hello','world'],['test','case']] 

是不一样的

list_of_strings = ['[hello','world]','[test','case]'] 

上市将很好地工作在list_of_lists的样式设置格式的句子的答案,但没有这么好字符串格式的列表。我会建议重做任何方法返回您当前的列表,以便它创建一个真正的列表清单。然后回来并应用此线程中使用的方法。这可能会导致最优雅的解决方案。

+0

啊,我明白了,非常感谢。将字符串列表转换为真正的列表列表的简单方法是什么? – Amaranthus

+0

因为你的字符串列表的格式不是标准的,所以没有某种内置的方式来做到这一点,如果这就是你想要的。您可以设想创建一个函数来解析字符串,并通过注意括号出现的位置来创建列表列表。我真的建议修改任何函数来构建你当前的字符串列表。 –