简体   繁体   English

在列表中,Python中映射为字符串和转换为字符串有什么区别?

[英]In a list , What is the difference between mapping to string and convert to string in Python?

Using Python 3, I tried to join the elements of list by converting it to string.使用 Python 3,我尝试通过将其转换为字符串来加入列表的元素。 It is said that to join all the elements of a list all the elements must be converted to a string.据说要连接列表中的所有元素,必须将所有元素转换为字符串。 So I just converted the whole list to a string and tried to join the list but it did not give desired output.所以我只是将整个列表转换为一个字符串并尝试加入列表但它没有给出所需的 output。

l = [1,2,3]
print("L type ",type(l))
print("Type of element ",type(l[0]))
d = str(l)
print("D type ",type(d))
print("Joining d ","".join(d))
print("D[0] type ",type(d[0]))
f = "".join(map(str,(l)))
print("F is ",f)
e = "".join(str(l))
print("E is ",e)

The output is: output 是:

L type  <class 'list'>
Type of element  <class 'int'>
D type  <class 'str'>
Joining d  [1, 2, 3]
D[0] type  <class 'str'>
F is  123
E is  [1, 2, 3]

But when I mapped each of the elements of the list to the string it gave the proper answer by joining the string.但是当我将列表的每个元素映射到字符串时,它通过连接字符串给出了正确的答案。 But if I converted the whole list to string, and each element of list is a string Eg:(D[0] type <class 'str'>) why it didnt join the elements?但是,如果我将整个列表转换为字符串,并且列表的每个元素都是一个字符串,例如:(D[0] type <class 'str'>) 为什么它没有连接元素? And if I converted elements to string at the time of joining it just returns the original list back again.如果我在加入时将元素转换为字符串,它只会再次返回原始列表。

Where it differs to use map?使用 map 有何不同? So can anyone please explain What is the difference between mapping to string and convert to string?那么谁能解释一下映射到字符串和转换为字符串有什么区别? What is the basic criteria to join a list?加入名单的基本标准是什么?

I need anyone to explain What is the difference between mapping to string and convert to string?我需要任何人来解释映射到字符串和转换为字符串之间的区别是什么? What is the basic criteria to join a list?加入名单的基本标准是什么?

Converting a list to a string gives you a single string -- the same string that you get when you print a list, with [ on the ends and , separating the elements.将列表转换为字符串会得到一个字符串——与print列表时得到的字符串相同,两端用[分隔元素。

>>> a
[1, 2, 3]
>>> str(a)
'[1, 2, 3]'

In the above REPL session, a is a list, and str(a) is a string that represents the list.在上面的REPL session中, a是一个列表, str(a)是表示列表的字符串。 (Note the quotes!) (注意引号!)

If you join a string with '' as the seperator, you just get the same string back.如果你用''作为分隔符join一个字符串,你只会得到相同的字符串。 It doesn't matter what the string is.字符串是什么并不重要。

>>> ''.join(str(a))
'[1, 2, 3]'
>>> ''.join("foo")
'foo'

If you join it with a separator that's not an empty string, it's easier to see what's going on -- we're just taking each character of the string and inserting the separators in between them.如果你用一个不是空字符串的分隔符join它,就更容易看到发生了什么——我们只是获取字符串的每个字符并在它们之间插入分隔符。 The reason join behaves this way with a string is because a string is itself an iterable of strings (its individual characters). join以这种方式处理字符串的原因是因为字符串本身就是一个可迭代的字符串(它的各个字符)。

>>> '-'.join("foo")
'f-o-o'
>>> '-'.join('[1, 2, 3]')
'[-1-,- -2-,- -3-]'
>>> '-'.join(str(a))
'[-1-,- -2-,- -3-]'

If you map the str function over a list, you get a list with each individual element transformed into a string.如果你map str function 在一个列表上,你会得到一个列表,每个元素都转换成一个字符串。 A map object itself doesn't have a useful string representation, but you can convert a map into a list , which does. map object 本身没有有用的字符串表示形式,但您可以将map转换为list ,它有。

>>> a
[1, 2, 3]
>>> map(str, a)
<map object at 0x000002D6118DA9E0>
>>> list(map(str, a))
['1', '2', '3']

Again, note the quotes -- 1 is an int , and '1' is a str .再次注意引号1是一个int ,而'1'是一个str

If we join a list of strings, we get all of the individual strings combined into a single string:如果我们join一个字符串列表,我们会将所有单独的字符串组合成一个字符串:

>>> ''.join(['1', '2', '3'])
'123'

and we can join a map in exactly the same way without converting it to a list first:我们可以用完全相同的方式join map而无需先将其转换为list

>>> ''.join(map(str, a))
'123'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM