我想在我的字典里找到相同的翻译源却有不同的结果
我有两张单子
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])
但是有很多嵌套循环,我相信有一种方法比这更好 有人有主意吗?你知道吗
非常感谢
我相信这会做一些类似于你想要的:
这不会获取索引并创建整数字典,而是复制源和目标中包含的字符串。你知道吗
它同时遍历两个列表。如果名称不在词典
output
中,则它将作为包含trans
的列表添加到词典中。如果名称已经在字典中,那么trans
将添加到该列表的末尾。你知道吗因此,输入:
将产生输出:
输入:
输出:
第一步:找到重复的条目。 步骤2:为每个重复条目获取索引 步骤3检查是否相等,这里的示例
text3
使用了两次,但具有相同的翻译 第四步:追加字典列表?(我不懂那种格式)结果:
相关问题 更多 >
编程相关推荐