-1

I have a problem wrapping my head around trying to convert strings into lists and producing the desired output. So far I have this code:

def func():

    characters = '?/><,.:;"[]{}=+()*&^%$#@!' # keeping ' and -
    new_lst = []
    user = input("String: ")
    for i in user:
        if (i not in characters):
            #new_lst = user.split(' ')
            new_lst += i # I know this is the problem but I just don't know where to go from here

    print(new_lst)

For example:

Keep. hyphen- and apostrophe's only.

Current output:

'K', 'e', 'e', 'p', ' ', 'h', 'y', 'p', 'h', 'e', 'n', '-', ' ', 'a', 'n', 'd', ' ', 'a', 'p', 'o', 's', 't', 'r', 'o', 'p', 'h', 'e', "'", 's', ' ', 'o', 'n', 'l', 'y']

Desired output:

['Keep', 'hyphen-', 'and', "apostrophe's", 'only']

Thanks for any help!

DwightD
  • 157
  • 8
  • Possible duplicate of [Concatenate item in list to strings](https://stackoverflow.com/questions/12453580/concatenate-item-in-list-to-strings) – Jesse Sep 24 '18 at 05:38

3 Answers3

1

Split the sentence into its words first, and then iterate over the words to rebuild (in the inside loop) the modified words and (in the outside loop) the list of words:

def func():

    characters = '?/><,.:;"[]{}=+()*&^%$#@!' # keeping ' and -
    new_lst = []
    user = input("String: ")
    for word in user.split():
        x = ''
        for i in word:
            if (i not in characters):
                x += i # I know this is the problem but I just don't know where to go from here
        new_lst.append(x)

    print(new_lst)

func()

Output:

['Keep', 'hyphen-', 'and', "apostrophe's", 'only']

This isn't necessarily a code-review site, but I would definitely consider naming your variables more meaningfully and using a list comprehension for the inner loop:

def get_stripped_word_list(s):
    """Return a list of words in string s, excluding certain characters."""

    # Sequence of characters to strip, keeping ' and -
    characters = '?/><,.:;"[]{}=+()*&^%$#@!'
    words = s.split()
    modified_words = []
    for word in words:
        modified_word_letters = [c for c in word if c not in characters]
        modified_words.append(''.join(modified_word_letters))
    return modified_words

s = input("String: ")
print(get_stripped_word_list(s))
xnx
  • 24,509
  • 11
  • 70
  • 109
1

you can replace the characters and you can split it

def func():

    characters = '?/><,.:;"[]{}=+()*&^%$#@!' # keeping ' and -
    user = raw_input("String: ")
    for i in characters:
        user=user.replace(i,"")
    new_lst=user.split(" ")
    new_lst = [i.strip('.') for i in new_lst]
    print(new_lst)
func()

output

['Keep', 'hyphen-', 'and', "apostrophe's", 'only']
vishalk
  • 182
  • 2
  • 10
0

You can use sub from the re module:

import re
string = "Keep. hyphen- and apostrophe's only."

re.sub("[^\w '-]", '', string).split()

Out[672]: ['Keep', 'hyphen-', 'and', "apostrophe's", 'only']
Onyambu
  • 67,392
  • 3
  • 24
  • 53