简体   繁体   English

Python,我如何获得两个列表列表之间的百分比精度

[英]Python , how do I get the percentage accuracy between 2 list of lists

Supoose I have 2 matrices (list of lists)假设我有 2 个矩阵(列表列表)

list1 = [[1, 2, 3, 4], [2, 6, 7, 7], [3, 6, 2, 9], [4, 7, 4, 3]]

list2 = [[1, 2, 3, 4], [9, 4, 3, 5], [3, 5, 2, 7], [1, 9, 8, 3]]

How would I get the percentage accuracy between the 2. So if both matrices have exactly the same values (in exactly the same lists) then the accuracy is 100%我将如何获得 2 之间的百分比准确度。因此,如果两个矩阵具有完全相同的值(在完全相同的列表中),那么准确度为 100%

I have already tried using我已经尝试过使用

len(set(test_list1) & set(test_list2)) / float(len(set(test_list1) | set(test_list2))) * 100

but it only works for singular lists not list of lists但它只适用于单数列表而不是列表列表

This works fine这工作正常

list1 = [[1, 2, 3, 4], [2, 6, 7, 7], [3, 6, 2, 9], [4, 7, 4, 3]]

list2 = [[1, 2, 3, 4], [2, 6, 7, 7], [3, 6, 2, 9], [4, 7, 4, 3]]

first_list = [item for sublist in list1 for item in sublist] 
second_list = [item for sublist in list2 for item in sublist] 

common = set(first_list).intersection(second_list)
total = set(first_list).union(second_list)

print((len(common)/len(total))*100)

Hop it helps:跳它有帮助:

result = []
for test_list1, test_list2 in zip(list1, list2):
    result.append(len(set(test_list1) & set(test_list2)) / float(len(set(test_list1) | set(test_list2))) * 100)
print(sum(result) / len(result))

You can try你可以试试

print(sum([sum([1 if x == list2[inx1][inx2] else 0 for inx2, x in enumerate(i)]) for inx1, i in enumerate(list1)]) / sum([len(i) for i in list1]) * 100)

That output那output

43.75

This is counting the matches between the inner lists of list1 and list2 and for every match, it will count it and on miss-match it won't.这是计算 list1 和 list2 的内部列表之间的匹配,并且对于每个匹配,它都会计算它,而在未匹配时它不会。 Then it's dividing the results with the count of all the elements in the list1 and multiply it by 100.然后将结果除以 list1 中所有元素的计数,然后将其乘以 100。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以在python中以百分比精度执行“string contains X”吗? - Can I do a “string contains X” with a percentage accuracy in python? 如何找到列表中元素的百分比? (Python) - How do i find the percentage of the elements in a list? (Python) 如何使用 python 计算值列表中的百分比增加或减少 - How do i calculate the percentage increase or decrease in a list of values with python 如何打印 Python 中两个列表之间的唯一列表元素(又名。如何打印不在两个列表中的项目) - How do I print the unique list elements between two lists in Python (aka. how to print the items not in both lists) 如何从列表的第i个值的总和中获得列表中第i个值的百分比 - how to get percentage of the i th value from list from the total of i th values of lists 如何使用Python获取列表列表中最大列表的索引? - How do I get the index of the largest list inside a list of lists using Python? 如何在python中获取列的一部分的总百分比? - How do I get the total percentage of a portion of a column in python? python 中列表的性质,为什么我会得到重复列表? - The nature of lists in python, why do I get a repeating list? 如何将列表列表和 append 中的列表复制到 python 中的新列表? - How do I copy a list of lists and append it to a new list in python? 如何将列表中的元素分组为python 3中的列表 - How do I group elements in a list to be list of lists in python 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM