假设我有两个csv输出,如下所示:
mark1:
[['a','','','','',''],['b','','','','',''],['c' ,'','','','',''],['d','','','','',''],['e','','',' ','',''],['f','','','','',''',['g','','','','',''] ]
和
MARK2:
[['a','10','','','',''],['b','','','','',''],['c ','10','','','',''],['d','','','','',''',['e','7',' ','','',''],[''','9','','','',''],['n','','','',''' ,'']]
在这种情况下,在第一个csv输出中找不到m和n。 所以我写的代码是为了找出这两个代码:
mark1 [row] [0]!= mark2 [row1] [0]:
print 'some marks are not the same - no match found'
上面的代码无效。
有没有具体方法可以解决这个问题?
感谢。
答案 0 :(得分:3)
我会先说这是我投票给@ ThaneBrimhall的答案,但是为了表达各种方式(假设你只关心一个元素在另一个元素中,不管位置如何):
# Get a set of unique first elements for each list
set1 = set([n[0] for n in mark1])
set2 = set([n[0] for n in mark2])
# Items in mark1 but not mark2
if set1.difference(set2):
print 'some marks are not the same - no match found'
# Items in mark2 but not mark1
if set2.difference(set1):
print 'some marks are not the same - no match found'
如果你想看看哪些引起了错误:
# Get a set of unique first elements for each list
set1 = set([n[0] for n in mark1])
set2 = set([n[0] for n in mark2])
# Items in mark1 but not mark2
set1_diffs = set1.difference(set2)
if set1_diffs:
print 'Marks {0} in mark1 but not mark2'.format(list(set1_diffs))
# Items in mark2 but not mark1
set2_diffs = set2.difference(set1)
if set2_diffs:
print 'Marks {0} in mark2 but not mark1'.format(list(set2_diffs))
答案 1 :(得分:2)
我发现这样做的最简单(也是计算速度最快)的方法就是做这样的事情:
if [n for n in mark2 if n[0] not in [m[0] for m in mark1]]:
print 'some marks are not found...'
需要两个for循环来比较要迭代的每个值。上面的代码粗略地翻译成:
keys = []
for m in mark1:
keys.append(m[0])
for n in mark2:
if n not in keys:
print 'some marks not found...'
break
答案 2 :(得分:0)
鉴于您想要比较整个列表列表,这应该比较它们:
if mark1 != mark2:
print 'some are not the same...'
如果您只想比较列表中每个列表的第一个字段,就可以这样做。
def compare_first(mark1,mark2):
for row in mark1:
for row2 in mark2:
if row[0] != row2[0]:
return false;
return true
然后主要使用该功能
if !equal_firs(mark1,mark2):
print('some are not the same...')
答案 3 :(得分:0)
你的例子没有多大意义。
mark1= [['a', '', '', '', '', ''], ['b', '', '', '', '', ''], ['c', '', '', '', '', ''], ['d', '', '', '', '', ''], ['e', '', '', '', '', ''], ['f', '', '', '', '', ''], ['g', '', '', '', '', '']]
mark2= [['a', '10', '', '', '', ''], ['b', '', '', '', '', ''], ['c', '10', '', '', '', ''], ['d', '', '', '', '', ''], ['e', '7', '', '', '', ''], ['m', '9', '', '', '', ''], ['n', '', '', '', '', '']]
if mark1[row][0] != mark2[row1][0]:
print 'some marks are not the same - no match found'
在if语句中,你正在寻找row和row1 - 为什么你不在同一行?
除此之外,这是不正确的 -
mark1[row][0] != mark2[row1][0]:
我相信你的意思是:
if mark1[row][0] != mark2[row1][0]: