-1

I want to remove troubled test_data list elements, which contains bad_characters and fixed characters to append into the new list stripped_test_data but the script does not work.

The following code:

        test_data = ["1912", "1929", "1913-1923",
                 "(1951)", "1994", "1934",
                 "c. 1915", "1995", "c. 1912",
                 "(1988)", "2002", "1957-1959",
                 "c. 1955.", "c. 1970's", 
                 "C. 1990-1999"]

    bad_chars = ["(",")","c","C",".","s","'", " "]

    def strip_characters(data):
        stripped_test_data = []
        for each in data:
            if bad_chars in each:
                tostr = str(each)
                adjusted = tostr.replace(bad_chars, "")
                stripped_test_data.append(tostr)
            else:
                adjusted = each
                stripped_test_data.append(each)
        return stripped_test_data

adjsuted_data = strip_characters(test_data)

when run throws the error:

> > TypeErrorTraceback (most recent call last) <ipython-input-1-d9d5a3a4542a> in <module>()
>      20     return stripped_test_data
>      21 
> ---> 22 adjsuted_data = strip_characters(test_data)
>      23 
>      24 
> 
> <ipython-input-1-d9d5a3a4542a> in strip_characters(data)
>      11     stripped_test_data = []
>      12     for each in data:
> ---> 13         if bad_chars in each:
>      14             tostr = str(each)
>      15             adjusted = tostr.replace(bad_chars, "")

TypeError: 'in <string>' requires string as left operand, not list

Can you please help what is wrong with the code and how to proceeds the operation?

jjj
  • 35
  • 1
  • 8
  • 2
    Possible duplicate of [How to check if a string contains an element from a list in Python](https://stackoverflow.com/questions/6531482/how-to-check-if-a-string-contains-an-element-from-a-list-in-python) – Georgy Sep 29 '19 at 09:07
  • 1
    `each in bad_chars` and not `bad_chars in each`. Because you search a string in list and not a list in string. – Vishnudev Krishnadas Sep 29 '19 at 09:08

2 Answers2

1

str.strip can handle multiple characters:

bad_chars_joined = ''.join(bad_chars)
[t.strip(bad_chars_joined) for t in test_data]

Output:

['1912',
 '1929',
 '1913-1923',
 '1951',
 '1994',
 '1934',
 '1915',
 '1995',
 '1912',
 '1988',
 '2002',
 '1957-1959',
 '1955',
 '1970',
 '1990-1999']
Chris
  • 29,127
  • 3
  • 28
  • 51
  • Could've linked, for example, this question instead of answering: [How can I remove multiple characters in a list?](https://stackoverflow.com/questions/14215338/how-can-i-remove-multiple-characters-in-a-list) – Georgy Sep 29 '19 at 10:05
-1

your code is trying to compare the entire list of bad chars when using the in bad_chars try this:

test_data = ["1912", "1929", "1913-1923",
                 "(1951)", "1994", "1934",
                 "c. 1915", "1995", "c. 1912",
                 "(1988)", "2002", "1957-1959",
                 "c. 1955.", "c. 1970's", 
                 "C. 1990-1999"]

bad_chars = ["(",")","c","C",".","s","'", " "]

def strip_characters(data):
    stripped_test_data = []
    for char in bad_chars:
        for each in data:
            if char in each:
                tostr = str(each)
                adjusted = tostr.replace(char, "")
                stripped_test_data.append(adjusted)
            else:
                stripped_test_data.append(each)
    return stripped_test_data

adjsuted_data = strip_characters(test_data)
Asad Rauf
  • 743
  • 9
  • 17
  • The code works sir, but does not remove all characters needed. For example the loop removes first bad sign in the list but then (after adjustment) it moves to other element. I need to remove all the bad characters and not let the script to switch to other element after it removes only one bad character. – jjj Sep 29 '19 at 09:42