Home > Back-end >  Replace dashes with whitespaces for all elements across a tuple?
Replace dashes with whitespaces for all elements across a tuple?

Time:07-07

I'm building off of these two questions because they don't quite answer my question: How to change values in a tuple? Python: Replace "-" with whitespace

If I have a tuple like this:

worldstuff = [('Hi', 'Hello-World', 'Earth), ('Hello-World', 'Hi), ...]

How do I replace dashes with whitespaces for all of the elements across all lists in a tuple? The previous Stack Overflow question covers changing the specific index of one list in a tuple, but not if there are multiple occurances of an element needing to be replaced.

I've tried doing the following, which doesn't quite work:

worldstuff_new = [x.replace('-', ' ') for x in tuple]

But if I do it for a specific list in the tuple, it works for that tuple list. I'm trying to avoid having to do separate lists and instead trying to do it all at once.

worldstuff_new = [x.replace('-', ' ') for x in tuple[0]]

I understand that tuples are immutable, which is why I am having trouble figuring this out. Is this possible? Would appreciate any help - thanks.

CodePudding user response:

Correct expression:

a = [('Hi', 'Hello-World', 'Earth'), ('Hello-World', 'Hi')]
b = [tuple([x.replace('-', ' ') for x in tup]) for tup in a]

>>> b
[('Hi', 'Hello World', 'Earth'), ('Hello World', 'Hi')]

A few notes:

  1. Please don't clobber builtins (tuple).
  2. What you have is actually not a tuple, but a list of tuples.
  3. As you note, tuples are immutable; but you can always build new tuples from the original ones.
  4. (Speed) Why tuple([x.replace ...]) (tuple of a list comprehension) instead of tuple(x.replace ...) (tuple of the output of a generator)? Because the former is slightly faster.

CodePudding user response:

first of everything, don't name any variable tuple it's a builtin function and when you name a variable tuple you miss that method

def changer(data):
    if type(data) == str:
        return data.replace("-", " ")
    elif type(data) == list:
        return [changer(x) for x in data]
    elif type(data) == tuple:
        return tuple(changer(x) for x in data)
tpl = [('Hi', 'Hello-World', 'Earth'), ('Hello-World', 'Hi')]

changer(tpl)

output:

[('Hi', 'Hello World', 'Earth'), ('Hello World', 'Hi')]

CodePudding user response:

tuple_old = [('Hi', 'Hello-World', 'Earth'), ('Hello-World', 'Hi')]
tuple_new = [
    tuple([x.replace('-', ' ') for x in tup]) for tup in tuple_old
]
print(tuple_new)

FWIW, tuples are the things in parentheses. Lists are in square brackets. So you have a list of tuples, not a tuple of lists.

CodePudding user response:

There are a few things that might help you to understand:

You cannot change a tuple or a string. you can only create a new one with different contents.

All the functions that "modify" a string are actually just creating a new string that has been modified from the original. Your original question that you referenced also slightly mis-understood one of the quirks of python where you can iterate over the characters in a string, but due to python not having a character datatype, they just end up as new strings. tldr; replacing "-" with " " looks just like this:

print("old-str".replace("-", " "))

This will generate a new string with all the dashes replaced.

Now you need to extend this to creating a new tuple of strings. You can create a new tuple with the built-in-function (which you had previously accidentally overwrote with a variable) tuple and passing in some sort of iterable. In this case I will use a generator expression (similar to list comprehension but without the square brackets) to create this iterable:

tuple(entry.replace("-", " ") for entry in old_tup)

finally you can apply this to each tuple in your list either by creating a new list, or by over-writing the values in the existing list (example shows creating a new list with a list comprehension):

[tuple(entry.replace("-", " ") for entry in old_tup) for old_tup in worldstuff ]

CodePudding user response:

This might help:

worldstuff_new = [tuple(x.replace('-', ' ') for x in t) for t in worldstuff]

CodePudding user response:

If you want a different way to do this you could use the map function like so.

tuples = [('Hi','Hello-World', 'Earth'), ('Hello-World', 'Hi'), ('Te-st', 'Te-st2')]

new_tuples = list(map(lambda tup: tuple(item.replace('-', ' ') for item in tup), tuples))

output:

[('Hi', 'Hello World', 'Earth'), ('Hello World', 'Hi'), ('Te st', 'Te st2')]
  • Related