2017-04-10 96 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

因爲你的字符串列表的格式不是標準的,所以沒有某種內置的方式來做到這一點,如果這就是你想要的。您可以設想創建一個函數來解析字符串,並通過注意括號出現的位置來創建列表列表。我真的建議修改任何函數來構建你當前的字符串列表。 –