Thanks Michael, this worked great, time to read up on collections!<br><br><div class="gmail_quote">On Jan 24, 2008 3:36 AM, Michael Langford <<a href="mailto:mlangford.cs03@gtalumni.org">mlangford.cs03@gtalumni.org</a>> wrote:
<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">You use a variation on bucket sort do to this:<br><a href="http://en.wikipedia.org/wiki/Bucket_sort" target="_blank">
http://en.wikipedia.org/wiki/Bucket_sort</a><br><br>You make a dict where the keys are the values in the lists, and a name<br>for each list is in the problem.<br><br>So you get something that looks like:<br><br>one: list1, list2
<br>two: list1, list2, list3<br>etc<br><br>Doing this with collections.defaultdict is a breeze:<br>import collections<br><br>dd = collections.defaultdict(list)<br>for eachlist in lists:<br> for each in eachlist:<br> dd[each].append(getListName(eachlist))
<br><br>Then to find the repeated elements you filter where the list is of length > 1.<br><br>for each in dd:<br> print "%s: %s" % (each, dd[each])<br><br>I'd provide code, but I'm not sure what an appropriate naming function
<br>is for you, nor am I sure what you're doing with this when you're<br>done.<br><br> --Michael<br><div class="Ih2E3d"><br><br><br>On Jan 24, 2008 3:15 AM, Fiyawerx <<a href="mailto:fiyawerx@gmail.com">
fiyawerx@gmail.com</a>> wrote:<br>> I have been able to find a few articles on comparing 2 lists, but I have 4<br>> lists that I need to compare. I need to find any repeated elements and the<br>> list that they came from. For example,
<br>><br>> list1 = ['one', 'two', 'three']<br>> list2 = ['one', 'two', 'four', 'five']<br>> list3 = ['two', 'three', 'six', 'seven']
<br>> list4 = ['three', 'five', 'six']<br></div>> <a href="https://mail.google.com/mail/#label/Pythontutor/117aadf8364dbf3b" target="_blank">https://mail.google.com/mail/#label/Pythontutor/117aadf8364dbf3b
</a><br>Gmail - [Tutor] Comparing more than 2 lists - <a href="mailto:michael.langford@gmail.com">michael.langford@gmail.com</a><br><div class="Ih2E3d">><br>> I need to be able to get along the lines of output:<br>
> Element 'one' contained in list1 and list2<br>> Element 'two' contained in list1 and list2 and list3<br>> ...<br>> Element 'five' contained in list2 and list4<br>><br>> etc.. and I can't quite figure out how to go about it
<br>><br>><br></div>> _______________________________________________<br>> Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>> <a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">
http://mail.python.org/mailman/listinfo/tutor</a><br>><br>><br><font color="#888888"><br><br><br>--<br>Michael Langford<br>Phone: 404-386-0495<br>Consulting: <a href="http://www.RowdyLabs.com" target="_blank">http://www.RowdyLabs.com
</a><br></font></blockquote></div><br>