Python 嵌套列表/字典理解如何添加缺少的元素
我需要在我的输出中添加一个天气字符串,我该怎么做Python 嵌套列表/字典理解如何添加缺少的元素,python,nested,nltk,Python,Nested,Nltk,我需要在我的输出中添加一个天气字符串,我该怎么做 weather\u data\u train=list() 对于范围内的j(0,len(天气发送列车)): 天气令牌=天气发送列车[j].split() 天气预报 对于键入的天气_令牌: 天气预报[钥匙]=正确 天气数据列车附加(天气指令) 我得到的输出 [{'today':True,'it':True,'is':True,'raining':True}, {'looking':真的,'cloudy':真的,'today':真的}, {'it'
weather\u data\u train=list()
对于范围内的j(0,len(天气发送列车)):
天气令牌=天气发送列车[j].split()
天气预报
对于键入的天气_令牌:
天气预报[钥匙]=正确
天气数据列车附加(天气指令)
我得到的输出
[{'today':True,'it':True,'is':True,'raining':True},
{'looking':真的,'cloudy':真的,'today':真的},
{'it':真的,'is':真的,'nice':真的,'weather':真的}]
我想要得到的输出
[({'today':True,'it':True,'is':True,'raining':True},'weather'),
({'looking':真的,'cloudy':真的,'today':真的},'weather'),
({'it':True,'is':True,'nice':True,'weather':True},'weather'))
您可以将dict封装在一个元组中,第二项是'weather'
更改:
weather_data_train.append(weather_dict)
致:
或者,您可以使用列表重写代码:
weather_data_train = [(dict.fromkeys(s.split(), True), 'weather') for s in weather_sents_train]
您可以将dict封装在一个元组中,并将
'weather'
作为第二项
更改:
weather_data_train.append(weather_dict)
致:
或者,您可以使用列表重写代码:
weather_data_train = [(dict.fromkeys(s.split(), True), 'weather') for s in weather_sents_train]