基本上我有一个这样的列表:
[' ROOT S . ', ' ROOT S ! ', ' ROOT is it true that S ? ', ' ', ' S NP VP ', ' VP Verb NP ', ' NP DT Noun ', ' NP NP PP ', ' PP Prep NP ', ' Noun Adj Noun ', ' ', ' Verb ate ', ' Verb wanted ', ' Verb kissed ', ' Verb understood ', ' Verb pickled ', ' ', ' DT the ', ' DT a ', ' DT every ', ' ', ' Noun president ', ' Noun sandwich ', ' Noun pickle ', ' Noun chief of staff ', ' Noun floor ', ' ', ' Adj fine ', ' Adj delicious ', ' Adj perplexed ', ' Adj pickled ', ' ', ' Prep with ', ' Prep on ', ' Prep under ', ' Prep in ']
我想将第一个值与每个条目的其余部分分开,并将其放入字典中,例如列表中的第一个值将输入到字典行中:
Key=ROOT
Value=S .
如果有多个相同类型的ID,请用|分隔它们。 这就是我喜欢的字典的样子
ROOT = 'S . | S ! | is it true that S ?',
S = 'NP VP',
VP = 'Verb NP',
NP = 'DT Noun | NP PP',
PP = 'Prep NP',
Noun = 'Adj Noun | president | sandwich | pickle | chief of staff | floor',
DT = 'the | a | every',
Verb = 'ate | wanted | kissed | understood | pickled',
Adj = 'fine | delicious | perplexed | pickled',
Prep = 'with | on | under | in'
有没有办法在不使用外部库的情况下做到这一点? 谢谢
最佳答案
不使用任何库或模块:
x = [' ROOT S . ', ' ROOT S ! ', ' ROOT is it true that S ? ', ' ', ' S NP VP ', ' VP Verb NP ', ' NP DT Noun ', ' NP NP PP ', ' PP Prep NP ', ' Noun Adj Noun ', ' ', ' Verb ate ', ' Verb wanted ', ' Verb kissed ', ' Verb understood ', ' Verb pickled ', ' ', ' DT the ', ' DT a ', ' DT every ', ' ', ' Noun president ', ' Noun sandwich ', ' Noun pickle ', ' Noun chief of staff ', ' Noun floor ', ' ', ' Adj fine ', ' Adj delicious ', ' Adj perplexed ', ' Adj pickled ', ' ', ' Prep with ', ' Prep on ', ' Prep under ', ' Prep in ']
d = {}
for k, v in (s.lstrip().split(' ',1) for s in x if ' ' in s.lstrip()):
if k in d:
d[k]+='|' + v
else:
d[k]=v
这会生成字典:
{'Adj': 'fine |delicious |perplexed |pickled ',
'DT': 'the |a | every ',
'NP': 'DT Noun |NP PP ',
'Noun': 'Adj Noun |president |sandwich |pickle |chief of staff |floor ',
'PP': 'Prep NP ',
'Prep': ' with |on |under | in ',
'ROOT': 'S . |S ! |is it true that S ? ',
'S': ' NP VP ',
'VP': ' Verb NP ',
'Verb': 'ate |wanted |kissed |understood |pickled '}
它是如何工作的
这会初始化一个空字典:
d = {}
这将开始循环遍历列表x
中的所有项目:
for k, v in (s.lstrip().split(' ',1) for s in x if ' ' in s.lstrip()):
此列表生成器具有以下形式
(function(s) for s in x if condition(s))
因此,它依次从列表 x
中提取每个字符串 s
。字符串 s
将被忽略,除非它们传递条件,在我们的例子中是:' ' in s.lstrip()
。此条件只是确保 s
中的第一个单词后至少有一个空格。换句话说,这会删除格式错误或空的条目。
生成器使用以下方法返回键和值:s.lstrip().split(' ',1)
。这会从 s
中提取第一个单词作为键,而该单词之后剩下的就是值。
以下内容将找到的项目添加到字典中:
if k in d:
d[k]+='|' + v
else:
d[k]=v
关于python - 列表到词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28145180/