如何用不同的翻译结果获得相同的翻译源

2024-09-30 20:20:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的字典里找到相同的翻译源却有不同的结果

我有两张单子

source = ['text1', 'text2', 'text3', 'text2', ...etc.]
target = ['trans1', 'trans2', 'trans3', 'trans4', ...etc.]

在这里,源“text2”重复了两次或多次,但在目标列表中,每一个或其中一些都有不同的值

更新

我想做的是:

当我把这两个数组传递给那个函数时,它会返回类似于

[{'source_list_index': ['target_list_index', etc.]}: {'', []}, ]

在我的例子中,输出应该是这样的:

[{1: [1, 3]}]

或任何其他可读输出 其中源值在目标数组中有许多不同的值

我的运行代码

import collections

source = [1, 1, 2, 3, 4, 4, 4, 5, 6]
target = [1, 2, 2, 3, 1, 2, 3, 5, 6]

duplicate_1 = [item for item, count in collections.Counter(source).items() if count > 1]
duplicate_2 = [item for item, count in collections.Counter(target).items() if count > 1]


def getIndexPositions(listOfElements, element):
    ''' Returns the indexes of all occurrences of give element in
    the list- listOfElements '''
    indexPosList = []
    indexPos = 0
    while True:
        try:
            # Search for item in list from indexPos to the end of list
            indexPos = listOfElements.index(element, indexPos)
            # Add the index position in list
            indexPosList.append(indexPos)
            indexPos += 1
        except ValueError as e:
            break

    return indexPosList

indexPosList=[]

for i in duplicate_1:
    indexPosList = getIndexPositions(source, i)

    print(indexPosList)

    for i in indexPosList:
        print(source[i])
        for x in indexPosList:

            if target[i] == target[x]:
                print('same target')
            else:
                print(source[i], 'different target :   first value is : ', target[i], '  ##### and ######  second value is: ', target[x])

但是有很多嵌套循环,我相信有一种方法比这更好 有人有主意吗?你知道吗

非常感谢


Tags: theinsourcetargetforindexcountetc
2条回答

我相信这会做一些类似于你想要的:

def get_translation(source, target):
    output = {}
    for name, trans in zip(source, target):
        if name in output:
            output[name].append(trans)
        else:
            output[name] = [trans]
    return output

这不会获取索引并创建整数字典,而是复制源和目标中包含的字符串。你知道吗

它同时遍历两个列表。如果名称不在词典output中,则它将作为包含trans的列表添加到词典中。如果名称已经在字典中,那么trans将添加到该列表的末尾。你知道吗

因此,输入:

source = ['text1', 'text2', 'text3', 'text2']
target = ['trans1', 'trans2', 'trans3', 'trans4']

将产生输出:

{'text1':['trans1'], 'text2':['trans2', 'trans4'], 'text3':['trans3']}

输入:

source = [1, 1, 2, 3, 4, 4, 4, 5, 6]
target = [1, 2, 2, 3, 1, 2, 3, 5, 6]

输出:

{1: [1, 2], 2: [2], 3: [3], 4: [1, 2, 3], 5: [5], 6: [6]}

第一步:找到重复的条目。 步骤2:为每个重复条目获取索引 步骤3检查是否相等,这里的示例text3使用了两次,但具有相同的翻译 第四步:追加字典列表?(我不懂那种格式)

source = ['text1', 'text2', 'text3', 'text2', 'text3', 'text1']
target = ['trans1', 'trans2', 'trans3', 'trans4', 'trans3', 'trans6']

def get_repeated_translations(source, target):
    double_translation_entries = []
    reapeated_entries = list(set([x for i, x in enumerate(source) if source.count(x)>1 and source.index(x) < i]))
    for repeated_entry in reapeated_entries:
        indices_of_repeated_entry = [i for i, x in enumerate(source) if x == repeated_entry]
        entry_translation = target[indices_of_repeated_entry[0]]
        for translated_index in indices_of_repeated_entry:
            if target[translated_index] != entry_translation:
                double_translation_entries.append({source.index(repeated_entry) : indices_of_repeated_entry})
                break

    return double_translation_entries

print(get_repeated_translations(source,target))

结果:

[{1: [1, 3]}, {0: [0, 5]}]
[Finished in 0.075s]

相关问题 更多 >