我正在处理两个单独的列表,一个是字符串的分割结果,另一个包含列表对象,列表对象中的第一项是字符串。
例如,
list_0: ["Hey", "hello?", "hi"]
list_1: [["Hey", 0.13, 0.14, 0, 0.58], ["hello?", 0.15, 0.16, 1, 0.23], ["hi", 0.17, 0.18, 0, 0.32]]
list_0
按正确的顺序排列并包含重复的值,它不能是一个集合,因为它是音频文件的翻译。在 list_1
中,第三个元素是每个发言者的 reference_number
。
我想对翻译进行排序并找到每个单词/短语的说话者,这样我就可以看到 reference_number_0
说“嘿”、“嗨”
和 reference_number_1
说“你好?”
。
由于可能存在重复值,我需要确保我没有错误地与第一对匹配。
如何找到每个完整短语的 ref_number
?
预期输出
0: "Hey", "hi"
1: "hello?"
作为更复杂的示例,假设我将其作为 list_0
和 list_1
:
list_0: ["Have", "we", "all", "had", "lunch" yes", "yes", "not", "yet"]
list_1: [["Have", 0.0, 0.1, 0, 0.12], ["we", 0.2, 0.3, 0, 0.48], ["all", 0.4, 0.5, 0, 0.85], ["had", 0.6, 0.7, 0, 0.82], ["lunch", 0.8, 0.9, 0, 0.35], ["yes", 0.9, 1.0, 0, 0.57], ["yes", 1.1, 1.2, 1, 0.56], ["not", 1.3, 1.4, 2, 0.25], ["yet", 1.5, 1.6, 2, 0.73]]
这里有三个扬声器 - 0、1、2。输出应该是:
0: "Have", "we", "all", "had", "lunch", "yes"
1: "yes"
2: "not", "yet"
干杯:)
最佳答案
只需构建一个列表字典:
di={}
for l0,l1 in zip(list_0,list_1):
di.setdefault(l1[3],[]).append(l0)
>>> di
{0: ['Have', 'we', 'all', 'had', 'lunch', 'yes'], 1: ['yes'], 2: ['not', 'yet']}
对于您的特定输出:
>>> '\n'.join("{}: {}".format(e, ", ".join(di[e])) for e in sorted(di))
0: Have, we, all, had, lunch, yes
1: yes
2: not, yet
关于python - 匹配两个单独列表中的子序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51275578/