【发布时间】:2018-12-29 22:37:44
【问题描述】:
我有两个列表,一个带有 id,一个带有每个 id 对应的 cmets。
list_responseid = ['id1', 'id2', 'id3', 'id4']
list_paragraph = [['I like working and helping them reach their goals.'],
['The communication is broken.',
'Information that should have come to me is found out later.'],
['Try to promote from within.'],
['I would relax the required hours to be available outside.',
'We work a late night each week.']]
ResponseID“id1”与段落相关(“我喜欢工作并帮助他们实现目标。”)等等。
我可以使用以下函数将段落分成句子:
list_sentence = list(itertools.chain(*list_paragraph))
获取最终结果的语法是什么,即数据框(或列表)具有单独的句子条目并具有与该句子关联的 ID(现在链接到段落)。最终结果将如下所示(最后我会将列表转换为熊猫数据框)。
id1 'I like working with students and helping them reach their goals.'
id2 'The communication from top to bottom is broken.'
id2 'Information that should have come to me is found out later and in some cases students know more about what is going on than we do!'
id3 'Try to promote from within.'
id4 'I would relax the required 10 hours to be available outside of 8 to 5 back to 9 to 5 like it used to be.'
id4 'We work a late night each week and rarely do students take advantage of those extended hours.'
谢谢。
【问题讨论】:
-
所有这些单元素列表是怎么回事?
-
当你想要一个像这样的“关联”时,我建议使用像
dict这样的映射类,所以像dict(zip(list_responseid, [lp[0] for lp in list_paragraph]))这样的东西可能会起作用。 -
dict 确实返回了一些结果,但它不完整,它跳过了一些句子。不过感谢您的提示,我会继续寻找。
-
那么就
dict(zip(list_responseid, list_paragraph)) -
我可以看到,当我删除 0 时,它会显示所有句子。现在,如果我需要它的结构像一个数据框,每个句子有一条记录并与之关联 ID,我需要遍历该字典中的每个值,对吗?