问题:.join()方法到底做什么?

我对Python来说还很陌生,并且完全不.join()理解所读内容是连接字符串的首选方法。

我试过了:

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

并得到类似:

5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5

为什么会这样工作?难道不595应该自动附加正义吗?

I’m pretty new to Python and am completely confused by .join() which I have read is the preferred method for concatenating strings.

I tried:

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

and got something like:

5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5

Why does it work like this? Shouldn’t the 595 just be automatically appended?


回答 0

仔细查看您的输出:

5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5
^                 ^                 ^

我突出显示了原始字符串的“ 5”,“ 9”,“ 5”。Python的join()方法是一个字符串的方法,而且占据了名单的事情,加入以字符串。一个更简单的示例可能有助于解释:

>>> ",".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

Look carefully at your output:

5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5
^                 ^                 ^

I’ve highlighted the “5”, “9”, “5” of your original string. The Python join() method is a string method, and takes a list of things to join with the string. A simpler example might help explain:

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

The “,” is inserted between each element of the given list. In your case, your “list” is the string representation “595”, which is treated as the list [“5”, “9”, “5”].

It appears that you’re looking for + instead:

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

回答 1

join以一个可迭代的东西作为参数。通常是列表。您遇到的问题是字符串本身是可迭代的,从而依次给出每个字符。您的代码分解为:

"wlfgALGbXOahekxSs".join("595")

其作用与此相同:

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

这样就产生了你的字符串:

"5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5"

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

join takes an iterable thing as an argument. Usually it’s a list. The problem in your case is that a string is itself iterable, giving out each character in turn. Your code breaks down to this:

"wlfgALGbXOahekxSs".join("595")

which acts the same as this:

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

and so produces your string:

"5wlfgALGbXOahekxSs9wlfgALGbXOahekxSs5"

Strings as iterables is one of the most confusing beginning issues with Python.


回答 2

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

例如

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

join将字符串与分隔符连接在一起。分隔符是您在之前放置的内容join。例如

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

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

To append a string, just concatenate it with the + sign.

E.g.

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

join connects strings together with a separator. The separator is what you place right before the join. E.g.

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

Join takes a list of strings as a parameter.


回答 3

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

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

join() is for concatenating all list elements. For concatenating just two strings “+” would make more sense:

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

回答 4

为了进一步说明别人在说什么,如果您想使用join简单地连接两个字符串,则可以执行以下操作:

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

To expand a bit more on what others are saying, if you wanted to use join to simply concatenate your two strings, you would do this:

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

回答 5

很好的解释了为什么在这里+串联大量字符串的开销很大

加号运算符是连接两个 Python字符串的完美解决方案。但是,如果您继续添加两个以上的字符串(n> 25),则可能需要考虑其他问题。

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

There is a good explanation of why it is costly to use + for concatenating a large number of strings here

Plus operator is perfectly fine solution to concatenate two Python strings. But if you keep adding more than two strings (n > 25) , you might want to think something else.

''.join([a, b, c]) trick is a performance optimization.


回答 6

在提供此作为输入时,

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

Python将其作为输出返回:

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

On providing this as input ,

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

Python returns this as output :

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

回答 7

list = ["my", "name", "is", "kourosh"]   
" ".join(list)

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

这是Python的输出

'my name is kourosh'
list = ["my", "name", "is", "kourosh"]   
" ".join(list)

If this is an input, using the JOIN method, we can add the distance between the words and also convert the list to the string.

This is Python output

'my name is kourosh'

回答 8

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

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

“”.join may be used to copy the string in a list to a variable

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。