dictionary - Finding out matches between two dictionaries in Python -


i have 2 files 'seen.txt' , 'members.txt'. first file 'seen.txt' lists people have seen post. file structured following:

friend/not friend name #1 number of mutual friends  friend/not friend name #2 number of mutual friends 

second file 'members.txt' lists details of people in group. file structured following:

name #1 info person more info person  name #2 info person more info person 

now, want create program show name of people member not in seen list. that, create 2 dictionaries stores names each of files. after done, iterate on each member in members_list , see whether in seen_list or not. if not, print out name in console.

this code have written:

seen = open('seen.txt').readlines() members = open('members.txt').readlines()  = 0 j = 0  seen_list = {} members_list = {}  lines in seen:     if == 1:         seen_list[lines.strip()] = 1         = 0     else:         += 1  lines in members:     if j == 0 or j == 3: # first line , every third line extract name         members_list[lines.strip()] = 1         j = 6     else:         j -= 1  member in members_list:     if member not in seen_list:         print member 

i believe solution elaborate , can done in shorter , faster way. can tell me cool python hacks possible on program in order make more efficient , shorter?

i use sets instead of dictionaries because seems you're throwing away lot of stored info in files , care names.

i first restructured way import names files. used izip_longest read files 4 lines @ time (3 lines of text plus blank line).

from itertools import izip_longest  seen = set() open('seen.txt', 'r') seen_file:     lines in izip_longest(*[seen_file]*4):         name = lines[1].strip()         seen.add(name)  members = set() open('members.txt', 'r') members_file:     lines in izip_longest(*[members_file]*4):         name = lines[0].strip()         members.add(name) 

then take set difference. see set operations here.

not_seen = members - seen member in not_seen: print member 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -