Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


removing duplicates from a list

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
AverageAzn247
Grandmaster Cheater
Reputation: 34

Joined: 01 Oct 2007
Posts: 909
Location: Austin,TX with 72 virgins

PostPosted: Wed Aug 15, 2012 12:35 pm    Post subject: removing duplicates from a list Reply with quote

I am writing a program to remove duplicates from a list however i also want to remove duplicates that vary with extra white spaces like foo foo and foo foo. What I tried to doing was first removing all the duplicates using python's build in set. Then I tried creating two lists, one without any whitespaces and the original. Next I compared the two without any whitespaces and deleted duplicates. For some reason some duplicates still remain. why?
Code:
import string
def main():
   inputfile=raw_input("file name?: ")
   file1=open(inputfile, "r")
   output=raw_input("output: ")
   sorted_list1=sort1(file1)
   sorted_list2=sort2(sorted_list1)
   
   write_file(sorted_list2,output)
def sort1(file1):
   
   temp=""
   list1=[]
   for rawline in file1:#scan line by line
      temp+=rawline#stores as string untill added
      key=rawline.strip()#removes whitespace and tabs for checkign
     
      if key == "endmodule":# if end of mod add to list
         
         list1.append(temp)
         #print temp
         temp=""# clears the string until next mod
         #print len(list1),"list 1"
   
         
   
   list1=list(set(list1))
   


   return list1
   
def sort2(sorted1):
   temp=""
   
   list1=sorted1
   list1.sort()
   list2=[]
   for mod in list1:
      list2.append(mod.strip())
     
   list2.sort()
   
   
     
   last=list2[-1]
   
   for i in range(len(list2)-2,-1,-1):
      if last == list2[i]:
            #print list2[i]
            del list2[i]
            del list1[i]
           
         
      else:
            last = list2[i]
     
         
         
         
   return list1

def write_file(sorted_list2,output):
   file4=open(output,"w")
   print "new module",len(sorted_list2)
   for mod in sorted_list2:
      file4.write(mod)
      #print mod
   file4.close()
   
   
   
main()

_________________


Waxxup wrote:
What are Night Elves?
A girl group?
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites