如何将一组中的条目连接到一个字符串中?

发布于 2024-12-03 00:03:12 字数 328 浏览 2 评论 0原文

基本上,我试图将一组中的条目连接在一起以输出一个字符串。我正在尝试使用类似于列表连接函数的语法。这是我的尝试:

list = ["gathi-109","itcg-0932","mx1-35316"]
set_1 = set(list)
set_2 = set(["mx1-35316"])
set_3 = set_1 - set_2
print set_3.join(", ")

但是我收到此错误: AttributeError: 'set' object has no attribute 'join'

对集合的等效调用是什么?

Basically, I am trying to join together the entries in a set in order to output one string. I am trying to use syntax similar to the join function for lists. Here is my attempt:

list = ["gathi-109","itcg-0932","mx1-35316"]
set_1 = set(list)
set_2 = set(["mx1-35316"])
set_3 = set_1 - set_2
print set_3.join(", ")

However I get this error: AttributeError: 'set' object has no attribute 'join'

What is the equivalent call for sets?

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

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

发布评论

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

评论(8

自此以后,行同陌路 2024-12-10 00:03:12
', '.join(set_3)

join 是一个字符串方法,而不是一个集合方法。

', '.join(set_3)

The join is a string method, not a set method.

放飞的风筝 2024-12-10 00:03:12

集合没有 join 方法,但您可以使用 改为 str.join

', '.join(set_3)

str.join 方法适用于任何可迭代对象,包括列表和集合。

注意:在包含整数的集合上使用此方法时要小心;您需要在调用 join 之前将整数转换为字符串。例如

set_4 = {1, 2}
', '.join(str(s) for s in set_4)

Sets don't have a join method but you can use str.join instead.

', '.join(set_3)

The str.join method will work on any iterable object including lists and sets.

Note: be careful about using this on sets containing integers; you will need to convert the integers to strings before the call to join. For example

set_4 = {1, 2}
', '.join(str(s) for s in set_4)
唔猫 2024-12-10 00:03:12

对字符串调用 join

print ", ".join(set_3)

The join is called on the string:

print ", ".join(set_3)
时光与爱终年不遇 2024-12-10 00:03:12

集合没有顺序 - 因此,当您将列表转换为集合时,您可能会丢失顺序,即:

>>> orderedVars = ['0', '1', '2', '3']
>>> setVars = set(orderedVars)
>>> print setVars
('4', '2', '3', '1')

通常,顺序将保留,但对于大型集合,几乎肯定不会。

最后,以防万一人们想知道,您不需要在连接中使用“,”。

只是: ''.join(set)

:)

Set's do not have an order - so you may lose your order when you convert your list into a set, i.e.:

>>> orderedVars = ['0', '1', '2', '3']
>>> setVars = set(orderedVars)
>>> print setVars
('4', '2', '3', '1')

Generally the order will remain, but for large sets it almost certainly won't.

Finally, just incase people are wondering, you don't need a ', ' in the join.

Just: ''.join(set)

:)

深巷少女 2024-12-10 00:03:12

setlist 都没有这样的方法 join,字符串有它:

','.join(set(['a','b','c']))

顺便说一下,你不应该使用名称 list 为你的变量。给它一个 list_my_list 或其他名称,因为 list 是经常使用的 python 函数。

Nor the set nor the list has such method join, string has it:

','.join(set(['a','b','c']))

By the way you should not use name list for your variables. Give it a list_, my_list or some other name because list is very often used python function.

夏至、离别 2024-12-10 00:03:12

您可以向后尝试 join 语句:

print ', '.join(set_3)

You have the join statement backwards try:

print ', '.join(set_3)
清醇 2024-12-10 00:03:12

我认为你只是把事情搞反了。

print ", ".join(set_3)

I think you just have it backwards.

print ", ".join(set_3)
浸婚纱 2024-12-10 00:03:12

我编写了一个处理以下边缘情况的方法:

  • 将大小设置为一。 ", ".join({'abc'}) 将返回 "a, b, c"。我想要的输出是“abc”
  • 包含整数的集合。
  • 空集应返回 ""
def set_to_str(set_to_convert, separator=", "):
        set_size = len(set_to_convert)
        if not set_size:
            return ""
        elif set_size == 1:
            (element,) = set_to_convert
            return str(element)
        else:
            return separator.join(map(str, set_to_convert))

I wrote a method that handles the following edge-cases:

  • Set size one. A ", ".join({'abc'}) will return "a, b, c". My desired output was "abc".
  • Set including integers.
  • Empty set should returns ""
def set_to_str(set_to_convert, separator=", "):
        set_size = len(set_to_convert)
        if not set_size:
            return ""
        elif set_size == 1:
            (element,) = set_to_convert
            return str(element)
        else:
            return separator.join(map(str, set_to_convert))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文