3 Replies - 184 Views - Last Post: 02 September 2013 - 06:39 PM Rate Topic: -----

#1 allonsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-September 13

Filtering out unnecessary chars and deleting duplicates?

Posted 02 September 2013 - 09:36 AM

Basically, I'm trying to create a program that if characters are entered as input, the program filters out letters that are not A, B, C or D and only prints out the letters A, B, C and D. I also want to ensure that if there are any strings that are repeated within the input, the following print line only prints input that has been stated once.

So for example my program might look like this:
Characters: A%#CD AB%!$uCD A^)(CD
Real characters: ACD ABCD
(filters out all unnecessary characters and doesn't count ACD as part of real characters because it's a duplicate)


The code I have right now is this:

user_input = input("What is it? ").split()
good = 'ABCD '
def unique_list(iterable):
    listing = []
    for item in iterable:
        if item not in listing:
            listing.append(item)
    return listing

for letter in user_input:
    if letter in good:
        listing.append(letter)
print("".join(unique_list)) + " " +' '.join(unique_list(user_input))



Erm... I'm pretty new to programming, so be gentle :P/>
Basically, I have two different functions in my code and I want to merge them so it acts as one thing... If that makes sense?

This post has been edited by andrewsw: 02 September 2013 - 11:44 AM
Reason for edit:: Added CODE tags


Is This A Good Question/Topic? 0
  • +

Replies To: Filtering out unnecessary chars and deleting duplicates?

#2 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Filtering out unnecessary chars and deleting duplicates?

Posted 02 September 2013 - 11:25 AM

So you want to filter out un-wanted characters and create a list without duplicates?

You can use a set for a un-ordered collection of unique elements.

And to get the characters you want, seems like you have found a solution. There are many more solutions including the reverse of what you have done to get the uniques (providing you dont have duplicates in your 'good' list)

...

theStr = 'A%#CD AB%!$uCD A^)(CD '
good = 'ABCD '
for c in good:
    if c in theStr:
        #add to list,set,w.e.
        print c + ' is in the string'


Was This Post Helpful? 0
  • +
  • -

#3 allonsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-September 13

Re: Filtering out unnecessary chars and deleting duplicates?

Posted 02 September 2013 - 04:31 PM

My code basically does what it's supposed to do, the first print line filters out the garbage and the second gets rid of duplicates. I wanted to know how I could merge the two print lines so it filters out the garbage and removes duplicates in one action on line.
Was This Post Helpful? 0
  • +
  • -

#4 BlueMelon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 187
  • Joined: 27-April 10

Re: Filtering out unnecessary chars and deleting duplicates?

Posted 02 September 2013 - 06:39 PM

You could look into list comprehensions

You can also take a look at this

This should get you on the right track:

user_input = 'A%#CD AB%!$uCD A^)(CD '
good = 'ABCD '

filtered = [c for c in good if c in user_input]
print ''.join(unique)

#ABCD


This post has been edited by BlueMelon: 02 September 2013 - 06:40 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1