使用python词典从字符串

时间:2017-10-28 16:45:23

标签: python dictionary

我有一个我需要学习的50个术语的列表,而不是将每个术语复制并粘贴到flashcard应用程序中,我试图将每个术语及其定义放入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] = ???

我想让代码读取字符串,如果它碰到一个数字,它应该保存它所运行的下一个单词,例如“头韵”作为关键词及其后的所有词语,直至下一个数字,作为值,例如。

  

“在一系列附近的单词中重复发出语音”

有关如何编写解决问题的代码的想法吗?

4 个答案:

答案 0 :(得分: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...'

你可以使用任何你不会在字符串中的其他任何地方使用的?-

答案 1 :(得分:0)

我会将这些术语放在嵌套列表中,如此,

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

然后循环terms

definitions = {}


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

编辑: 我想如果你可以将这些术语放在一个数组中,你就可以轻松地将它们手动放入字典中,所以我猜测术语列表是它的方式而且没有改变,在这种情况下我的答案是'有帮助。

答案 2 :(得分: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'

答案 3 :(得分: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}