简体   繁体   中英

How to Replace Characters in a List in Python?

list = ["/x01", "/x30", "/x30", "/x53", "/x46", "/x46", "/x46", "/x30", "/x30", "/x30", "/x31", "/x46", "/x46", "/x0D"]

for x in list:
    x.replace("[","").replace("]","").replace('"','').replace(" ","").replace(",","").replace("[","")

This hasn't been working. Could you explain what I might be doing wrong, and tell me if there is a more effective way of getting the output: "/x01/x30/x30/x53/x46/x46/x46/x30/x30/x30/x31/x46/x46/x0D"

Use str.join :

>>> lis = ["/x01", "/x30", "/x30", "/x53", "/x46", "/x46", "/x46", "/x30", "/x30", "/x30", "/x31", "/x46", "/x46", "/x0D"]
>>> ''.join(lis)
'/x01/x30/x30/x53/x46/x46/x46/x30/x30/x30/x31/x46/x46/x0D'

Looking at your code, I think you were trying to apply str.replace on str version of the list. But that would be a weird way to do this, better use str.join :

>>> str(lis)
"['/x01', '/x30', '/x30', '/x53', '/x46', '/x46', '/x46', '/x30', '/x30', '/x30', '/x31', '/x46', '/x46', '/x0D']"

The above string is just a representation of the list object.

>>> str(lis).replace("[","").replace("]","").replace(" ","").replace(",","").replace("'","")
'/x01/x30/x30/x53/x46/x46/x46/x30/x30/x30/x31/x46/x46/x0D'

The first problem is that replace doesn't change a string in-place, it just returns a new string. And you're ignoring that new string.

What you want is:

new_list = []
for x in list:
    new_list.append(x.replace("[","").replace("]","").replace('"','').replace(" ","").replace(",","").replace("[",""))

You can simplify that with translate , or use a different way of filtering out the characters like a comprehension or a filter call. But the result will be the same.


The bigger problem is that what you're trying to do doesn't make any sense. None of the elements in your list have a [ , ] , " , etc. character in them. You're probably confusing the string representation of the list with the list itself.

If you want to join the members of a list, or to produce any representation of the list other than the default repr , just explicitly join them. For example, this gets what you seem to want:

''.join(list)

… and this gets a different representation:

' and '.join(list)

… and this gets roughly the same thing as repr :

'[' + ', '.join(map(repr, list)) + ']'

You should use translate(None, nuke_list_string)

Two reasons:

  1. replace performs a 1-to-1 substitution, each substitution results in the allocation of a new string which is then returned, resulting in a lot of overhead if you chain a bunch of replaces like you do in the example.

  2. translate usually requires a 1-to-1 mapping, ie translate('ABC','abc') would convert the string 'a brown cow' to 'A Brown Cow'. However, if you specify None as the new mapping, it will remove the characters it finds.

Here's an example:

a = 'this is [a] string with (stuff) in {it}'
print a.translate(None, '[]{}()')

Produces:

this is a string with stuff in it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM