4 Replies - 1601 Views - Last Post: 13 October 2011 - 03:44 PM Rate Topic: -----

#1 labradorguy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 25-July 11

Remove Duplicates in list

Posted 13 October 2011 - 05:29 AM

I have the list
list = [aaa,bbb,ccc,ddd]
list2 = [aaa,bbb,ccc,eee]
.
.
.
answer = [ddd,eee]



I can only figure out how to
list = [aaa,bbb,ccc,ddd]
list2 = [aaa,bbb,ccc,eee]
.
.
.
answer = [aaa,bob,ccc,ddd,eee]


please help here is the code
import sys

if len(sys.argv) < 2:
    print "usage: python", sys.argv[0], "filename file1name file2name ..."
    sys.exit(1)

letters = []
counttwo = 0



f = open(sys.argv[1], "r")
line = f.read()
f.close()

line = line.split()

for x in line:
	if(x != line):
		counttwo += 1
		letters.append(x)
letters.sort()

#print ">>>", counttwo
#print ">>>", letters

for filename in sys.argv[2:]:
	letters2 = []
	stuff = []
	count = 0
	for data in open(filename):
		for y in data.split():
			if(y != data):
				count += 1
				letters2.append(y)
	stuff=(letters+letters2)
	
	#length of orginal letters+letters2
	num2 = len(stuff)
	
	if stuff:
		stuff.sort()
		last = stuff[-1]
		for i in range(len(stuff)-2,-1,-1):
			if last == stuff[i]:
				del stuff[i]
			else:
				last = stuff[i]
	
	#length of letters+letters2 with duplicates take out
	num = len(stuff)
	
	print ">>>", num2
	print ">>>", num
	print ">>>", counttwo
	print ">>>", count
	letters2.sort()	
	#print ">>>", count
	#print ">>>", letters2 
	cfilename = filename
	answer = 1.0-((num2-num)/(counttwo+count))		
	print "Sim(\"",sys.argv[1],"\",\"",cfilename, "\") = ", 1.0-((num2-num)/(counttwo+count))



Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Remove Duplicates in list

#2 Motoma   User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 798
  • Joined: 08-June 10

Re: Remove Duplicates in list

Posted 13 October 2011 - 06:24 AM

results = []
for item in list_a:
    if not item in list_b:
        results.append(item)
for item in list_b:
    if not item in list_a:
        results.append(item)


Was This Post Helpful? 1
  • +
  • -

#3 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 322
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Remove Duplicates in list

Posted 13 October 2011 - 07:36 AM

The last Python challenge was on removing duplicates from a list. Take your pick (but only pick one you understand how it works) :)

This post has been edited by Simown: 13 October 2011 - 07:39 AM

Was This Post Helpful? 1
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7505
  • View blog
  • Posts: 15,553
  • Joined: 16-October 07

Re: Remove Duplicates in list

Posted 13 October 2011 - 08:07 AM

First try:
>>> list = ['aaa','bbb','ccc','ddd']
>>> list2 = ['aaa','bbb','ccc','eee']
>>> 
>>> ans = [ i for i in list if i not in list2 ]
>>> ans.extend([ i for i in list2 if i not in list ])
>>> ans
['ddd', 'eee']
>>> 



Also, probably more amusing:
>>> set(list).symmetric_difference(set(list2))
set(['eee', 'ddd'])
>>> 


Was This Post Helpful? 1
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5509
  • View blog
  • Posts: 14,497
  • Joined: 18-April 07

Re: Remove Duplicates in list

Posted 13 October 2011 - 03:44 PM

View Postbaavgai, on 13 October 2011 - 08:07 AM, said:

Also, probably more amusing:
>>> set(list).symmetric_difference(set(list2))
set(['eee', 'ddd'])
>>> 



Sets for the win!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1