正規表現を使用して任意の数で分割することができます。そして、結果のリストで、単語とその意味を分けるために分割します。それを辞書に保存してください!ここでは、リストの内包表記を使用せずに
ある
>>> 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'
を使用することができます!
@Justin O'Brien try dict1 = {a.split( " - ")[0] .strip():a.split( " - ")[1] .strip()では、 a} – jimidime
これはうまくいきました –
このコードは質問に答えるかもしれませんが、** how **および** why **についての追加の文脈を提供することで、問題を解決し、回答の長期的価値を向上させます。 – Alexander