2017-10-28 129 views
1

我有一個需要學習的50個術語的列表,而不是將每個術語複製並粘貼到flashcard應用程序中我試圖將每個術語及其定義放入Python字典中。使用python字典從字符串中創建flashcards

我有以下代碼:

terms = """1 Alliteration- the repetition of a speech sound in a sequence of nearby words 2 Term - definition...""" 

definitions = {} 


for word in text.split(): 
    if word.isdigit(): 
     definitions[word+1] = ??? 

我想要的代碼,通過串讀,如果它擊中一個數字應該保存它運行到例如下一個單詞「Alliteration」作爲關鍵字並且後面的所有單詞直到下一個數字爲止,例如數值

如何編寫解決問題的代碼的任何想法「在附近的字序列的一個語音復讀機」?

回答

0

嘗試下面的代碼:

import re 

str1 = """1 Alliteration- the repetition of a speech sound in a sequence of 
    nearby words 2 Term - definition...""" 
str2 = re.split("\d+",str1) 
dict1 = {a.split("-")[0].strip():a.split("-")[1].strip() for a in str2 if '-' in a} 
+0

@Justin O'Brien嘗試dict1 = {a.split(「 - 」)[0] .strip():a.split(「 - 」)[1] .strip()in str2 if' - 'in a} – jimidime

+0

這個工作,謝謝 –

+0

雖然這段代碼可能會回答這個問題,提供關於**如何**和**爲什麼**解決問題的額外上下文將提高​​答案的長期價值。 – Alexander

0

您可以使用正則表達式將所有數字替換爲某個特定的值並將該字符串拆分爲該值。

In [24]: terms = """1 Alliteration- the repetition of a speech sound in a sequence of nearby words 2 Term - definition...""" 

In [25]: new_term = re.sub("\d+", "?-", terms) 

In [26]: new_term 
Out[26]: '?- Alliteration- the repetition of a speech sound in a sequence of nearby words ?- Term - definition...' 

In [27]: li =new_term.split('?-') 
In [28]: li 
Out[28]: 
['', 
' Alliteration- the repetition of a speech sound in a sequence of nearby words ', 
' Term - definition...'] 
In [29]: list(map(lambda t: t.strip(), li[1:])) 
Out[29]: 
['Alliteration- the repetition of a speech sound in a sequence of nearby words', 
'Term - definition...' 

可以使用任何內容,而不是?-,你不會有其他地方的的字符串中。

0

我會把條款中嵌套列表,像這樣,

terms = [ [word, definition], [word, definition], etc ] 

然後遍歷terms

definitions = {} 


for item in terms: 
    word = item[0] 
    meaning = item[1] 
    definitions[word] = meaning 

編輯: 我想,如果你可以把條件數組中的你可以很容易地手動將它們放入字典中,所以我猜測術語列表就是它的方式並沒有改變,在這種情況下,我的答案沒有幫助。

0

你可以使用正則表達式來分割任何數字。然後,在結果列表中,分割以獲取單詞及其含義。將它存儲到您的字典!

>>> terms 
'1 Alliteration- the repetition of a speech sound in a sequence of nearby words 2 Term - definition...' 
>>> d={} 
>>> import re 
>>> dict(l.strip().split('-') for l in re.split('[0-9]',terms) if l) 
{'Alliteration': ' the repetition of a speech sound in a sequence of nearby words', 'Term ': ' definition...'} 
>>> d 
{' Alliteration': ' the repetition of a speech sound in a sequence of nearby words ', ' Term ': ' definition...'} 

也就是說,

>>> re.split('[0-9]',terms) 
['', ' Alliteration- the repetition of a speech sound in a sequence of nearby words ', ' Term - definition...'] 
>>> [l for l in re.split('[0-9]',terms)] 
['', ' Alliteration- the repetition of a speech sound in a sequence of nearby words ', ' Term - definition...'] 
>>> [l for l in re.split('[0-9]',terms) if l] 
[' Alliteration- the repetition of a speech sound in a sequence of nearby words ', ' Term - definition...'] 
>>> [l.strip().split('-') for l in re.split('[0-9]',terms) if l] 
[['Alliteration', ' the repetition of a speech sound in a sequence of nearby words'], ['Term ', ' definition...']] 
>>> dict(l.strip().split('-') for l in re.split('[0-9]',terms) if l) 
{'Alliteration': ' the repetition of a speech sound in a sequence of nearby words', 'Term ': ' definition...'} 

不使用列表解析,

>>> for l in re.split('[0-9]',terms): 
...  if l: 
...    key,value = l.strip().split('-') 
...    d[key]=value 
... 

在這裏,而不是d[key]=value可以使用d[key]=d.get(key,'')+value+'\n'如果條件一個字可以有多種含義!

+0

謝謝你的回答,你能解釋其中'l'在'l.strip()'從何而來? –

+0

'l'是用於列表理解的for循環的迭代變量。 –