[英]If I have two sets of data with the same amount of values, what's the best approach to finding out the values in the sets that do not match?
例如,假设我有这两组数据:
套装1:
Host: 123 Name: Name1 Value:1234 Date: 06/19/2019 School: MIT Color: Black
套装2:
Host: 123 Name: Name2 Value:1234 Date: 06/19/2019 School: Harvard Color: Black
python 中比较这两个列表并返回如下结果的最佳方法是什么:
结果
Host 123 in set 1 and set 2 do not match!
Set1-Name: Name1
Set2-Name: Name2
Set1-School: MIT
Set2-School: Harvard
任何帮助都会很棒,非常感谢
set1 = '''
Host: 123
Name: Name1
Value:1234
Date: 06/19/2019
School: MIT
Color: Black'''
set2 = '''
Host: 123
Name: Name2
Value:1234
Date: 06/19/2019
School: Harvard
Color: Black'''
import re
for g1, g2 in zip(re.findall(r'(.*?):\s*(.*?)$', set1, flags=re.M), \
re.findall(r'(.*?):\s*(.*?)$', set2, flags=re.M)):
if g1[1] == g2[1]:
print('{}: {}'.format(g1[0], g1[1]))
else:
print('Set1-{}: {}'.format(g1[0], g1[1]))
print('Set2-{}: {}'.format(g2[0], g2[1]))
印刷:
Host: 123
Set1-Name: Name1
Set2-Name: Name2
Value: 1234
Date: 06/19/2019
Set1-School: MIT
Set2-School: Harvard
Color: Black
您可以读取数据集并将每个值逐行添加到列表中。 这将突出显示两个列表之间的任何差异。
在 if 语句之后,您可以随意格式化它,以使其更具可读性。
for x, y in zip(Set1, Set2):
if x != y:
print(x, y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.