473,322 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to remove duplicates

Hi

I am unable to get the following code to work:
def remove_duplicates (strng):
"""
>>> remove_duplicates ('apple')
'aple'
>>> remove_duplicates ('Mississippi')
'Misp'
>>> remove_duplicates ('The quick brown fox jumps over the lazy dog')
'The quick brown fx jmps v t lazy dg'
>>> remove_duplicates ('121 balloons 2 u')
'121 balons 2 u'
"""
I am required to return a string which is the same as the argument except only the first occurrence of each letter is present. Upper and lower case letters are treated as different and only duplicate letters are removed, other characters such as spaces or numbers are not changed.

It has to pass in a doctest.

I cannot get this to work, please can someone help!
May 18 '10 #1
5 8569
dwblas
626 Expert 512MB
Shouldn't this be
>>> remove_duplicates ('121 balloons 2 u')
'12 balons 2 u'

Use a list to store each letter found. Parse the string one letter at a time. If the letter is not found in the list, then it is the first occurrence so you can append it to the list and add it to the re-formated string or list. If the letter is found in the list, do nothing.
May 18 '10 #2
Hi thanks for the reply but the last one needs to be '121 balons 2 u' as the script doesn't delete duplicate numbers. This is where I am getting stuck.
May 18 '10 #3
Glenton
391 Expert 256MB
Hi please can you post the code you've written so far (please use code tags or it's hard to read), and the problems you're getting with it.

Is your string always going to be just alphanumeric. And if not, is it just repeated letters that need removing?

Anyway, this interactive session should solve it for you:
Expand|Select|Wrap|Line Numbers
  1. In [6]: sfinal=""
  2.  
  3. In [7]: s
  4. Out[7]: 'asdfabwerawef'
  5.  
  6. In [8]: for c in s:
  7.    ...:     if c not in sfinal:
  8.    ...:         sfinal+=c
  9.    ...:         
  10.    ...:         
  11.  
  12. In [9]: sfinal
  13. Out[9]: 'asdfbwer'
  14.  
  15. In [10]: s="121 balloons 2 u"
  16.  
  17. In [11]: for c in s:
  18.    ....:     if c.lower() in "abcdefhijklmnopqrstuvwxyz":
  19.    ....:         if c not in sfinal:
  20.    ....:             sfinal+=c
  21.    ....:     else:
  22.    ....:         sfinal+=c
  23.    ....:         
  24.    ....:         
  25.  
  26. In [12]: sfinal
  27. Out[12]: 'asdfbwer121 lon 2 u'
  28.  
May 19 '10 #4
take each string or each elem in the string, list it and subject it to the set function and rejoin it
Expand|Select|Wrap|Line Numbers
  1. for x in '12 balons 2 u':
  2.                s=list(x)
  3.                r=set(s)
  4.                j=s.join()
May 19 '10 #5
Hi thanks for all the help!

I used the following code:
Expand|Select|Wrap|Line Numbers
  1.     result = ""
  2.     for char in strng:
  3.         if not char in result or not char.isalpha():
  4.             result += char
  5.     return result
This returns the result without removing duplicate whitespaces and numerical expressions. Thanks!
May 19 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ragnorack67 | last post by:
Hello again, I have one further question about improving a validation. Currently this will validate a string of words separated by commas, and if it's greater than 9 will do something, or something...
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
2
by: Mamatha | last post by:
Hi I have an application with listview.When i click on one button the data will be displayed like this in the listview: colA colB colC ----- ----- ------...
6
by: pooh80133 | last post by:
Hi! I am pasting my SQL code at the end of this message. I am trying to use SELECT DISTINCT in a query, but I am a beginner for using Access. Right now I have duplicate ID's (Indiv ID) in my...
6
by: topala1984 | last post by:
hello all excuse my english, I`m a beginer in java and I want to remove duplicate cities + rearrenging file`s lines elements from one 150 mb file, in my file I have that: ...
8
natalie99
by: natalie99 | last post by:
hi everyone i have reseached this topic and cannot seem to find a solution that suits my problem, please help!! I have two tables, "Inventory" which contains 30,000 or so records, with about...
2
by: vlkc | last post by:
Hi, i have this database: 1 one 2 one people have 3 people one have 4 people have how can i remove duplicated rows with word "one" ?
5
by: rjonam | last post by:
What is the easiest way to remove duplicates from a XML file?
6
by: Mike Naz | last post by:
I have to write a program to demonstrate using bitmaps and bitwise operators to sort and remove duplicates from a file of random phone numbers. a lot of them... im not new to programming and i...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.