0

尝试使用 TrialHandler 编写实验,我成功地制作并打印了以下形式的字典列表:

[
    {
        'sentence': 'I am currently working',
        'variable1': 1,
        'variable2': 10
    },
    {
        'sentence': 'How are you today?',
        'variable1': 2,
        'variable2': 20
    }, # ... etc.
]

每部字典都描述了试验的特征。整个字典列表包含实验的所有试验。是否可以选择每个词典的句子部分并在新窗口中一一显示句子?

4

2 回答 2

0

您可以使用列表推导获取句子列表:

listOfDict = [{'variable1':1, 'variable2':10, 'sentence':'I am currently working'},
              {'variable1':2, 'variable2':20, 'sentence':'How are you today?'}]

sentences = [d['sentence'] for d in listOfDict]
print(sentences)

打印出来:

['I am currently working', 'How are you today?']
于 2013-08-09T14:59:00.780 回答
0

看这个:

>>> d = [
...     {
...         'sentence': 'I am currently working',
...         'variable1': 1,
...         'variable2': 10
...     },
...     {
...         'sentence': 'How are you today?',
...         'variable1': 2,
...         'variable2': 20
...     },
... ]
>>>
>>> "\n".join(x['sentence'] for x in d)
'I am currently working\nHow are you today?'
>>> print "\n".join(x['sentence'] for x in d)
I am currently working
How are you today?
>>>
于 2013-08-09T15:30:57.233 回答