用元组的所有元素替换破折号?

发布于 2025-02-13 07:32:38 字数 806 浏览 1 评论 0原文

我是在这两个问题的基础上的,因为它们没有完全回答我的问题: 如何在元组中更改值? python:python:replace' - “ with

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

whitespace 带有元组所有列表中所有元素的白色空间的破折号?以前的堆栈溢出问题涵盖了元组中一个列表的特定索引,但如果需要更换元素的多次事件。

我已经尝试执行以下操作,这是不太有用的:

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

但是,如果我为元组中的特定列表进行操作,则适用于该元组列表。我试图避免必须执行单独的列表,而是尝试一次执行所有操作。

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

我知道元素是不可变的,这就是为什么我难以弄清楚这一点。这可能吗?感谢任何帮助 - 谢谢。

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 worldstuff]

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 worldstuff[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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

沧桑㈠ 2025-02-20 07:32:38

正确的表达式:

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')]

一些注释:

  1. 请不要凝固内置(tuple)。
  2. 您所拥有的实际上不是一个元组,而是一个元素清单。
  3. 如您所指出的那样,元组是不可变的;但是您总是可以从原始元素中构建新的元素。
  4. (speed)为什么元组([X.replace ...])(列表理解的元组)而不是tuple(X.Replace ...)(tuple>发电机的输出)?因为前者稍快。

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.
落叶缤纷 2025-02-20 07:32:38

首先,请勿命名任何变量元组这是一个内置功能,当您命名变量tuple时,您会错过该method> 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)

输出:

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

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')]
花心好男孩 2025-02-20 07:32:38
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,元素是括号中的东西。列表在方括号中。因此,您有一个元组列表,而不是列表的元组。

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.

极致的悲 2025-02-20 07:32:38

有几件事可能会帮助您理解:

您不能更改元组或字符串。您只能创建一个具有不同内容的新产品。

“修改”字符串的所有函数实际上只是创建了一个已从原始的新字符串。您引用的最初问题也有些错误地理解了Python的一个怪癖之一,您可以在字符串中迭代字符,但是由于Python没有字符数据类型,它们最终才能成为新字符串。 tldr;用“ - ”替换“ - ”看起来像这样:

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

这将生成一个新的字符串,并更换所有破折号。

现在,您需要将其扩展到创建新的字符串元组。您可以使用内置功能(以前曾意外用变量覆盖)创建一个新的元组,tuple 并以某种觉得效果传递。在这种情况下,我将使用发电机表达式(类似于 a>但没有方括号)来创建这个迭代:

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

最后,您可以通过创建新列表或通过过度编写现有列表中的值(示例显示创建具有列表理解的新列表):

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

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 ]
各自安好 2025-02-20 07:32:38

这可能会有所帮助:

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

This might help:

worldstuff_new = [tuple(x.replace('-', ' ') for x in t) for t in worldstuff]
贱人配狗天长地久 2025-02-20 07:32:38

如果您想要另一种方法来这样做,则可以这样使用地图功能。

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))

输出:

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

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')]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文