Sunday, 15 January 2012

Compare two sets of strings and then return whole strings that are different in Python 3.4 -



Compare two sets of strings and then return whole strings that are different in Python 3.4 -

i'm writing little api listening program, , i'm trying figure out when new has been published. i've figured out of it, i'm having problem on lastly step -- want print out new. can compare 2 lists of items sets , set of letters that's in right answer, can't seem actual strings print.

here's code wrote compare 2 lists (both new_revised_stuff , old_revised_stuff lists of strings, "bob likes eat breakfast @ http://bobsburgers.com" few dozen items per list).

new_stuff = set(new_revised_stuff) - set(old_revised_stuff).intersection(new_revised_stuff)

which returns:

set('b','o','l'...)

i can rid of 'set' notation writing:

list(new_stuff)

but doesn't help. i'd print out "bob likes..." if that's new line.

i've tried:

new_stuff = [] in new_revised_stuff: b in old_revised_stuff: if != b: ''.join(a) new_stuff.append(a)

which results in actual stack overflow, it's bad code.

if want bring together iterable of single characters string, ''.join(new_stuff). example:

>>> new_stuff = ['b','o','l'] >>> ''.join(new_stuff) 'bol'

however, there 2 problems here, inherent in design:

sets hold unique elements. so, if string diffs "hello, bob", there's going 1 o , 1 l in set of diffs. sets arbitrarily ordered. so, if string diffs "bob likes", converting set , string 'k iboebls'.

if either of problem (and suspect are), need rethink algorithm. can solve sec 1 using orderedset (there's recipe in collections docs), first 1 going more of problem.

so, how could this?

well, don't need new_revised_stuff set; if iterate on characters , maintain ones aren't in old_revised_stuff, long old_revised_stuff set, that's efficient intersecting 2 sets.

but making old_revised_stuff set eliminate duplicates there, don't think want. want "multiset". in python, best way represent counter.

so, think want (maybe) this:

old_string = ' eat breakfast @ http://bobsburgers.com' new_string = 'bob likes eat breakfast @ http://bobsburgers.com' old_chars = collections.counter(old_string) new_chars = [] ch in new_string: if old_chars[ch]: old_chars[ch] -= 1 else: new_chars.append(ch) new_string = ''.join(new_chars)

python python-3.x set

No comments:

Post a Comment