import collections, sys, re filecount=0 dict1 = collections.defaultdict(list) for file1 in sys.argv[1:]: filecount+=1 f = open(file1, "r") linecount=1 for line in f: if linecount>1: p = re.search("^(.+),.+", line.strip()) if p: dict1[p.group(1)].append(filecount) linecount+=1 for the_key, the_value in dict1.items(): count='' sys.stdout.write(the_key + ',') c = collections.Counter(the_value) for i in range(1,filecount+1): if count: count = count + "," + str(c[i]) else: count = str(c[i]) print(count)