[英]What's the difference between these codes? Why do they have different outputs?
list = ['first', 'second', 'third', 'fourth']
s = '/'
newList = s.join(list)
print(newList)
This code outputs 该代码输出
"first/second/third/fourth"
list = ['first', 'second', 'third', 'fourth']
s = '/'
newList = s.join(str(list))
print(newList)
This code outputs 该代码输出
"[/'/f/i/r/s/t/'/,/ /'/s/e/c/o/n/d/'/,/ /'/t/h/i/r/d/'/,/ /'/f/o/u/r/t/h/'/]"
What does str()
do here that causes the list to separate by every letter? str()
在这里做了什么,导致列表由每个字母分开?
The str()
create a string like "['first', 'second', 'third', 'fourth']"
. str()
创建一个类似于"['first', 'second', 'third', 'fourth']"
的字符串。
And s.join()
treat the string as a char array. s.join()
将字符串视为char数组。 Then it put '/'
between every element in the array. 然后将'/'
放在数组中的每个元素之间。
str
converts it's argument to its string representation. str
将其参数转换为其字符串表示形式。 The string representation of a list is a single string starting with an opening square bracket and ending with a closing one. 列表的字符串表示形式是一个单个字符串,其开头为方括号,结尾为结尾。 The elements in between are converted using repr
and separated by ,
. 中间的元素使用repr
转换,并以,
分隔。 That string is in itself an iterable of characters. 该字符串本身就是一个可迭代的字符。 As such, join
will place a /
between each element of the iterable, ie, between every character of the string. 这样, join
将在可迭代的每个元素之间(即,字符串的每个字符之间)放置一个/
。
The equivalent to your first code would be to convert every string element of the list into a separate string: 与您的第一个代码等效的是将列表中的每个字符串元素转换为单独的字符串:
s.join(str(x) for x in list)
In your particular case, str
is a no-op because it will return the argument if the input is already a str
. 在您的特定情况下, str
是no-op,因为如果输入已经是str
,它将返回参数。
For arbitrary lists, the approach shown here is better than just using s.join(list)
, because join
requires all the elements of the iterable to be str
s, but makes no attempt to convert them, as say print
would. 对于任意的列表,这里显示的方法是比只用更好s.join(list)
,因为join
需要迭代的所有元素是str
S,但并没有试图将它们转换,如说print
会。 Instead, it raises a TypeError
when it encounters a non- str
. 而是在遇到非str
时引发TypeError
。
Another, less pythonic, but nevertheless very common, way of expressing the same conversion is 表示相同转换的另一种方法,较少使用pythonic,但仍然非常常见,
s.join(map(str, list))
And of course, the insert the mandatory admonition against naming variables after common builtins here for yourself. 当然,在您自己的常用内建函数之后,插入针对命名变量的强制性警告。
The join
function is a string method which returns a string concatentated with the elements of a iterable. join
函数是一个字符串方法,它返回一个与iterable元素相联系的字符串。
Now, in the first case: 现在,在第一种情况下:
list_1 = ['first', 'second', 'third', 'fourth'] #I changed the name to list_1 as list is a keyword
s = '/'
newList = s.join(list_1)
print(newList)
Every string in the list is a iterable, so, join outputs by concatenating all elements of the list by adding '/'
between each element. 列表中的每个字符串都是可迭代的,因此,通过在每个元素之间添加'/'
来连接列表的所有元素,可以连接输出。
In the second case: 在第二种情况下:
list_1 = ['first', 'second', 'third', 'fourth']
s = '/'
newList = s.join(str(list_1))
print(newList)
Since, str
converts the entire list including the [
and ]
braces as a string. 由于str
会将包括[
和]
大括号的整个列表转换为字符串。 Due to this every character in the new string becomes a iterable and join returns a new string by adding '/'
between every element. 因此,新字符串中的每个字符都变为可迭代,并且join通过在每个元素之间添加'/'
来返回新字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.