deleting unnecessary chars?

This is the place for queries that don't fit in any of the other categories.

deleting unnecessary chars?

Postby allonsy » Mon Sep 02, 2013 1:54 pm

Hi, I'm trying to create a program where if letters are entered that are not a part of a certain list, then those letters will be deleted and only letters in the list will be printed.

For example, if I set up some kind of tuple with the letters A, B, C and D
and if chars such as '%$' 'y' '!' and '?' were amongst the letters A, B, C and D in an input statement, the following line would simply print all the letters in the list and disregard all excess letters.


So:
Enter chars: AD#6^8CD #A%BC*D
Real letters: ADCD ABCD

Essentially, I want to create some sort of filter?
Last edited by Mekire on Mon Sep 02, 2013 2:31 pm, edited 1 time in total.
Reason: First post lock.
allonsy
 
Posts: 9
Joined: Sat Aug 31, 2013 3:42 pm

Re: deleting unnecessary chars?

Postby metulburr » Mon Sep 02, 2013 2:09 pm

New Users, Read this

and the code you have so far is?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1366
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: deleting unnecessary chars?

Postby allonsy » Mon Sep 02, 2013 2:22 pm

Well, I've been previously going about the question in a completely wrong manner...

I've been using the replace function
so something like:
Code: Select all
word = input("Chars: ")
word = word.replace('?', '')
..... etc
print(word)


Though it did technically 'work', I really felt like using 50 lines worth of code for a program as small as this would be unnecessary...
allonsy
 
Posts: 9
Joined: Sat Aug 31, 2013 3:42 pm

Re: deleting unnecessary chars?

Postby Mekire » Mon Sep 02, 2013 2:30 pm

Well first let's nail it down. Which is smaller; the set of characters that are allowed, or the set of characters that aren't allowed.

You will then create an empty list; and iterate (for loop) through your string. If the character is in the set of allowed characters, append it to the list (or if it is not in the set of illegal characters; whichever you choose).

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: deleting unnecessary chars?

Postby allonsy » Mon Sep 02, 2013 2:41 pm

Code: Select all
good = 'A' or 'B' or 'C' or 'D'
empty_list = []
word = input("Chars: ")
for empty_list in word:
    if word not in good:
      empty_list.append(word)
print(empty_list)


Obviously that's wrong.... But is that something like my final code should be?
Sorry, I'm extremely new to programming!
allonsy
 
Posts: 9
Joined: Sat Aug 31, 2013 3:42 pm

Re: deleting unnecessary chars?

Postby Mekire » Mon Sep 02, 2013 2:45 pm

Code: Select all
good = 'ABCD '
letter_list = []
word = input("Chars: ")
for letter in word:
    if letter in good:
        letter_list.append(letter)

print("".join(letter_list))

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: deleting unnecessary chars?

Postby allonsy » Mon Sep 02, 2013 2:51 pm

If for example, the inputed chars repeat itself multiple times, would a code like this work to only printing chars once?

For example:
Chars: ABCD AB AC ACD ABCD
ABCD AB AC ACD

Code: Select all
if word not in a_list:
    a_list.append(word)
print(a_list)
allonsy
 
Posts: 9
Joined: Sat Aug 31, 2013 3:42 pm

Re: deleting unnecessary chars?

Postby Mekire » Mon Sep 02, 2013 2:57 pm

If you want something like that you will need to look into str.split() to make a list of the separate chunks. Then if they are all hashable you could just convert that to a set to get only unique items; or otherwise, yes, you can use the previous technique.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: deleting unnecessary chars?

Postby allonsy » Mon Sep 02, 2013 3:25 pm

Code: Select all
input = input("What is it? ")
def unique_list(l):
  listing = []
  [listing.append(x) for x in l if x not in listing]
  return listing

print(''.join(unique_list(input.split())))


what about this?
allonsy
 
Posts: 9
Joined: Sat Aug 31, 2013 3:42 pm

Re: deleting unnecessary chars?

Postby allonsy » Mon Sep 02, 2013 3:50 pm

Actually, this would work better now that I think about it:

Code: Select all
print(' '.join(set(word.split())))



This is my current code:

Code: Select all
good = 'ABCD '
letter_list = []
word = input("Chars: ")
for letter in word:
    if letter in good:
        letter_list.append(letter)

print("".join(letter_list))
print(' '.join(set(word.split())))


Um, how would I merge the last two print lines...?
Last edited by allonsy on Mon Sep 02, 2013 3:53 pm, edited 1 time in total.
allonsy
 
Posts: 9
Joined: Sat Aug 31, 2013 3:42 pm

Re: deleting unnecessary chars?

Postby Mekire » Mon Sep 02, 2013 3:52 pm

Couple things. Don't ever overwrite builtins (unless you actually have a good reason). You can't use input as a variable name; or rather you can, but the next time you call input it won't work.

Next. List comprehensions. You aren't use them the way you are intended to.

In your initial problem of excluding characters I gave you the solution:
Code: Select all
good = 'ABCD '
letter_list = []
word = input("Chars: ")
for letter in word:
    if letter in good:
        letter_list.append(letter)

This could have equally been:
Code: Select all
good = 'ABCD '
word = input("Chars: ")
letter_list = [letter for letter in word if letter in good]

Back to this problem. You could write this:
Code: Select all
def unique_list(iterable):
    listing = []
    for item in iterable:
        if item not in listing:
            listing.append(item)
    return listing

user_input = input("What is it? ").split()
print(' '.join(unique_list(user_input)))

And, if all items are hashable (which they are if we are only dealing with text) then this is equivalent to:
Code: Select all
user_input = input("What is it? ").split()
print(' '.join(set(user_input)))
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: deleting unnecessary chars?

Postby Mekire » Mon Sep 02, 2013 4:02 pm

allonsy wrote:
Code: Select all
good = 'ABCD '
letter_list = []
word = input("Chars: ")
for letter in word:
    if letter in good:
        letter_list.append(letter)

print("".join(letter_list))
print(' '.join(set(word.split())))


Um, how would I merge the last two print lines...?

You probably wouldn't.

Also I assume you are wanting to filter out any garbage in the string and then use the split on it. That isn't what you are doing here. Here you filter the good characters and put them in word_list, but then you split up the non-filtered original string. Probably not what you wanted.

Anyway... don't try to write one liners just for the sake of it. Write two nice clean functions that have clear purposes.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: deleting unnecessary chars?

Postby allonsy » Mon Sep 02, 2013 4:06 pm

What I want my finished program to do is to simultaneously filter out any garbage in the string AND remove any duplicates.
allonsy
 
Posts: 9
Joined: Sat Aug 31, 2013 3:42 pm

Re: deleting unnecessary chars?

Postby Mekire » Mon Sep 02, 2013 4:09 pm

Yep. So write two functions (they are already written). One that you pass the input to, and return the string with the garbage filtered out; and another that you pass the now-clean string to, and return with no duplicates.

Yes, we could probably write a nice long convoluted one liner that did the whole thing. But don't.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: deleting unnecessary chars?

Postby DrakeMagi » Tue Sep 03, 2013 2:26 am

Mekire already gave you answers.
That you could just combine them into 1 function.

here my option
Code: Select all
enter = 'AD#6^8CD #A%BC*D'

def filter_string(string, iterable):
    storage = []
    def filter_(letter):
        if letter in storage:
            return False
        storage.append(letter)
        return letter in iterable
       
    return filter(filter_, string)
   
print filter_string(enter, 'ABCD ')
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 94
Joined: Sun May 12, 2013 8:36 pm

Re: deleting unnecessary chars?

Postby el_manu » Tue Sep 03, 2013 6:58 am

i"m interested in this..what would the actual answer be though
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: deleting unnecessary chars?

Postby Mekire » Tue Sep 03, 2013 8:54 am

el_manu wrote:i"m interested in this..what would the actual answer be though

Did you read this whole thread? What is it that you don't understand.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: deleting unnecessary chars?

Postby el_manu » Tue Sep 03, 2013 9:30 am

none of those codes ask for user input and remove all the junk letters and duplicates, and then print them out without junk and duplicates...i tried the codes and they don't do that
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: deleting unnecessary chars?

Postby allonsy » Tue Sep 03, 2013 9:33 am

This forum is not for people to give you answers. This is not even your question. Sorry, but if someone who has been programming for less than a week can work this out, I'm sure you can too.
allonsy
 
Posts: 9
Joined: Sat Aug 31, 2013 3:42 pm

Re: deleting unnecessary chars?

Postby el_manu » Tue Sep 03, 2013 11:03 am

well i can't figure it out.. and i can use this program for stuff. well the code i can adapt to make a program of my own. and i wanna know how the code looks like. i been programming for maybe 3 weeks, but its too complicated for me.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Next

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 2 guests