2017-10-28 10 views
1

私は学ぶ必要がある50語のリストを持っており、それぞれをフラッシュカードアプリにコピーして貼り付ける代わりに、各用語とその定義をPython辞書に入れようとしています。私は、コードが文字列を読みすると、それは数字に当たった場合、それは例えばにそれが実行される次の単語を保存する必要がありますしたいPython辞書を使用して文字列からフラッシュカードを作成する

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 try dict1 = {a.split( " - ")[0] .strip():a.split( " - ")[1] .strip()では、 a} – jimidime

+0

これはうまくいきました –

+0

このコードは質問に答えるかもしれませんが、** how **および** why **についての追加の文脈を提供することで、問題を解決し、回答の長期的価値を向上させます。 – Alexander

0

regexを使用すると、すべての数字を特定の値に置き換えて、その値に文字列を分割できます。

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 ] 

次にループ

definitions = {} 


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

termsオーバーEDIT、ネストされたリストに用語を置く: あなたは、配列あなたの中に用語を置くことができれば、私は推測します手作業で簡単に手で入れることができるので、用語のリストはそれが変わっていない方法であると推測しています。その場合、私の答えは役に立たない。

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.strip 'でどこ 'L'を説明することができ、答えてくれてありがとう)'から来ていますか? –

+0

'l'はリストの理解に使用されるforループの反復変数です。 –

関連する問題