【问题标题】:What exactly does the .join() method do?.join() 方法到底是做什么的?
【发布时间】:2023-04-11 02:43:02
【问题描述】:

我对 Python 还是很陌生,完全被 .join() 弄糊涂了,我读过它是连接字符串的首选方法。

我试过了:

strid = repr(595)
print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
    .tostring().join(strid)

得到类似的东西:

5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5

为什么会这样? 595 不应该自动附加吗?

【问题讨论】:

  • 我想你在程序运行时点击了你的 Yubikey

标签: python list string


【解决方案1】:

仔细查看您的输出:

5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5
^                 ^                 ^

我已突出显示原始字符串的“5”、“9”、“5”。 Python join() 方法是一个字符串方法,它需要一个 list 的东西来加入字符串。一个更简单的例子可能有助于解释:

>>> ",".join(["a", "b", "c"])
'a,b,c'

“,”插入给定列表的每个元素之间。在您的情况下,您的“列表”是字符串表示“595”,它被视为列表 [“5”、“9”、“5”]。

您似乎正在寻找+

print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
.tostring() + strid

【讨论】:

  • 为什么不是: 5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5wlfgALGbXOahekxSs ?将字符串附加到最后一个元素?
  • 一个原因是这给了join一个有用的属性,它是split的倒数(docs.python.org/library/stdtypes.html#str.split
  • 如果您想要另一个分隔符,请在列表末尾放置一个空字符串。 ','.join(['a', 'b', 'c', '']) 给出“a,b,c”
  • OP 可能将 string.join 与确实连接路径的 os.path.join 混淆了
【解决方案2】:

join 将可迭代的事物作为参数。通常它是一个列表。您的问题是字符串本身是可迭代的,依次给出每个字符。您的代码分解为:

"wlfgALGbXOahekxSs".join("595")

作用与此相同:

"wlfgALGbXOahekxSs".join(["5", "9", "5"])

然后生成你的字符串:

"5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5"

作为可迭代对象的字符串是 Python 最令人困惑的开始问题之一。

【讨论】:

  • 赞成指出可能是混乱的症结所在:字符串是可迭代的,因此它们的行为类似于字符列表。
【解决方案3】:

要附加一个字符串,只需将其与+ 符号连接即可。

例如

>>> a = "Hello, "
>>> b = "world"
>>> str = a + b
>>> print str
Hello, world

join 使用分隔符将字符串连接在一起。分隔符就是你 放在join 之前。例如

>>> "-".join([a,b])
'Hello, -world'

Join 将字符串列表作为参数。

【讨论】:

  • 这种直接简洁的回答应该得到我们社区的提倡。 @丹
【解决方案4】:

join() 用于连接所有列表元素。对于仅连接两个字符串“+”会更有意义:

strid = repr(595)
print array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
    .tostring() + strid

【讨论】:

    【解决方案5】:

    要进一步扩展其他人所说的内容,如果您想使用 join 简单地连接两个字符串,您可以这样做:

    strid = repr(595)
    print ''.join([array.array('c', random.sample(string.ascii_letters, 20 - len(strid)))
        .tostring(), strid])
    

    【讨论】:

      【解决方案6】:

      很好地解释了为什么使用+ 连接大量字符串here 成本很高

      Plus 运算符是连接 两个 Python 的完美解决方案 字符串。但是如果你继续添加两个以上的字符串 (n > 25) ,你 可能会想点别的。

      ''.join([a, b, c]) 技巧是一种性能优化。

      【讨论】:

        【解决方案7】:

        在提供此作为输入时,

        li = ['server=mpilgrim', 'uid=sa', 'database=master', 'pwd=secret']
        s = ";".join(li)
        print(s)
        

        Python 将其作为输出返回:

        'server=mpilgrim;uid=sa;database=master;pwd=secret'
        

        【讨论】:

          【解决方案8】:
          list = ["my", "name", "is", "kourosh"]   
          " ".join(list)
          

          如果这是一个输入,使用 JOIN 方法,我们可以添加单词之间的距离,也可以将列表转换为字符串。

          这是 Python 输出

          'my name is kourosh'
          

          【讨论】:

            【解决方案9】:

            "".join 可用于将列表中的字符串复制到变量中

            >>> myList = list("Hello World")
            >>> myString = "".join(myList)
            >>> print(myList)
            ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']
            >>> print(myString)
            Hello World
            

            【讨论】:

              猜你喜欢
              • 2013-11-29
              • 2014-10-13
              • 2015-07-04
              • 2015-01-23
              • 2015-08-06
              • 2013-09-02
              • 2014-01-02
              • 2013-10-10
              相关资源
              最近更新 更多