将不同类型的项目列表作为字符串加入Python

问题:将不同类型的项目列表作为字符串加入Python

我需要加入项目清单。列表中的许多项目都是从函数返回的整数值;即

myList.append(munfunc()) 

如何将返回的结果转换为字符串,以便将其与列表连接?

我是否需要对每个整数值执行以下操作:

myList.append(str(myfunc()))

有没有更Python化的方法来解决铸造问题?

I need to join a list of items. Many of the items in the list are integer values returned from a function; i.e.,

myList.append(munfunc()) 

How should I convert the returned result to a string in order to join it with the list?

Do I need to do the following for every integer value:

myList.append(str(myfunc()))

Is there a more Pythonic way to solve casting problems?


回答 0

调用str(...)是将内容转换为字符串的Python方式。

您可能需要考虑为什么要使用字符串列表。您可以改为将其保存为整数列表,并且仅在需要显示整数时才将其转换为字符串。例如,如果您有一个整数列表,则可以在for循环中将它们一一转换,然后将它们与结合,

print(','.join(str(x) for x in list_of_ints))

Calling str(...) is the Pythonic way to convert something to a string.

You might want to consider why you want a list of strings. You could instead keep it as a list of integers and only convert the integers to strings when you need to display them. For example, if you have a list of integers then you can convert them one by one in a for-loop and join them with ,:

print(','.join(str(x) for x in list_of_ints))

回答 1

将整数传递给str并没有错。您可能不这样做的一个原因是myList实际上应该是整数列表,例如,将列表中的值相加是合理的。在这种情况下,在将int附加到myList之前,请勿将其传递给str。如果最终在追加之前没有转换为字符串,则可以通过执行以下操作来构造一个大字符串

', '.join(map(str, myList))

There’s nothing wrong with passing integers to str. One reason you might not do this is that myList is really supposed to be a list of integers e.g. it would be reasonable to sum the values in the list. In that case, do not pass your ints to str before appending them to myList. If you end up not converting to strings before appending, you can construct one big string by doing something like

', '.join(map(str, myList))

回答 2

可以使用python中的map函数。它有两个参数。第一个参数是必须用于列表中每个元素的函数。第二个论点是可迭代的

a = [1, 2, 3]   
map(str, a)  
['1', '2', '3']

将列表转换为字符串后,可以使用简单的连接功能将列表组合为单个字符串

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

map function in python can be used. It takes two arguments. First argument is the function which has to be used for each element of the list. Second argument is the iterable.

a = [1, 2, 3]   
map(str, a)  
['1', '2', '3']

After converting the list into string you can use simple join function to combine list into a single string

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

回答 3

a=[1,2,3]
b=[str(x) for x in a]
print b

上面的方法是将列表转换为字符串的最简单,最通用的方法。另一个简短的方法是-

a=[1,2,3]
b=map(str,a)
print b
a=[1,2,3]
b=[str(x) for x in a]
print b

above method is the easiest and most general way to convert list into string. another short method is-

a=[1,2,3]
b=map(str,a)
print b

回答 4

有三种方法可以做到这一点。

假设您有一个整数列表

my_list = [100,200,300]
  1. "-".join(str(n) for n in my_list)
  2. "-".join([str(n) for n in my_list])
  3. "-".join(map(str, my_list))

但是,如python网站https://docs.python.org/2/library/timeit.html上的timeit示例中所述,使用地图的速度更快。所以我建议您使用"-".join(map(str, my_list))

There are three ways of doing this.

let say you have a list of integers

my_list = [100,200,300]
  1. "-".join(str(n) for n in my_list)
  2. "-".join([str(n) for n in my_list])
  3. "-".join(map(str, my_list))

However as stated in the example of timeit on python website at https://docs.python.org/2/library/timeit.html using a map is faster. So I would recommend you using "-".join(map(str, my_list))


回答 5

您的问题很清楚。也许您正在寻找扩展,以便将另一个列表的所有元素添加到现有列表中:

>>> x = [1,2]
>>> x.extend([3,4,5])
>>> x
[1, 2, 3, 4, 5]

如果要将整数转换为字符串,请使用str()或字符串插值,可能与列表推导结合使用,即

>>> x = ['1', '2']
>>> x.extend([str(i) for i in range(3, 6)])
>>> x
['1', '2', '3', '4', '5']

所有这些都被认为是pythonic的(好吧,生成器表达式更像是pythonic的,但让我们保持话题简单)

Your problem is rather clear. Perhaps you’re looking for extend, to add all elements of another list to an existing list:

>>> x = [1,2]
>>> x.extend([3,4,5])
>>> x
[1, 2, 3, 4, 5]

If you want to convert integers to strings, use str() or string interpolation, possibly combined with a list comprehension, i.e.

>>> x = ['1', '2']
>>> x.extend([str(i) for i in range(3, 6)])
>>> x
['1', '2', '3', '4', '5']

All of this is considered pythonic (ok, a generator expression is even more pythonic but let’s stay simple and on topic)


回答 6

例如:

lst_points = [[313, 262, 470, 482], [551, 254, 697, 449]]

lst_s_points = [" ".join(map(str, lst)) for lst in lst_points]
print lst_s_points
# ['313 262 470 482', '551 254 697 449']

对于我来说,我想str在每个str列表之前添加一个:

# here o means class, other four points means coordinate
print ['0 ' + " ".join(map(str, lst)) for lst in lst_points]
# ['0 313 262 470 482', '0 551 254 697 449']

或单个列表:

lst = [313, 262, 470, 482]
lst_str = [str(i) for i in lst]
print lst_str, ", ".join(lst_str)
# ['313', '262', '470', '482'], 313, 262, 470, 482

lst_str = map(str, lst)
print lst_str, ", ".join(lst_str)
# ['313', '262', '470', '482'], 313, 262, 470, 482

For example:

lst_points = [[313, 262, 470, 482], [551, 254, 697, 449]]

lst_s_points = [" ".join(map(str, lst)) for lst in lst_points]
print lst_s_points
# ['313 262 470 482', '551 254 697 449']

As to me, I want to add a str before each str list:

# here o means class, other four points means coordinate
print ['0 ' + " ".join(map(str, lst)) for lst in lst_points]
# ['0 313 262 470 482', '0 551 254 697 449']

Or single list:

lst = [313, 262, 470, 482]
lst_str = [str(i) for i in lst]
print lst_str, ", ".join(lst_str)
# ['313', '262', '470', '482'], 313, 262, 470, 482

lst_str = map(str, lst)
print lst_str, ", ".join(lst_str)
# ['313', '262', '470', '482'], 313, 262, 470, 482

回答 7

也许您不需要数字作为字符串,只需执行以下操作:

functaulu = [munfunc(arg) for arg in range(loppu)]

稍后,如果您需要将其作为字符串,则可以使用字符串或格式字符串来实现:

print "Vastaus5 = %s" % functaulu[5]

Maybe you do not need numbers as strings, just do:

functaulu = [munfunc(arg) for arg in range(loppu)]

Later if you need it as string you can do it with string or with format string:

print "Vastaus5 = %s" % functaulu[5]


回答 8

没人会喜欢repr吗?
python 3.7.2:

>>> int_list = [1, 2, 3, 4, 5]
>>> print(repr(int_list))
[1, 2, 3, 4, 5]
>>> 

请注意,这是一个明确的表示。一个示例显示:

#Print repr(object) backwards
>>> print(repr(int_list)[::-1])
]5 ,4 ,3 ,2 ,1[
>>> 

pydocs-repr上的更多信息

How come no-one seems to like repr?
python 3.7.2:

>>> int_list = [1, 2, 3, 4, 5]
>>> print(repr(int_list))
[1, 2, 3, 4, 5]
>>> 

Take care though, it’s an explicit representation. An example shows:

#Print repr(object) backwards
>>> print(repr(int_list)[::-1])
]5 ,4 ,3 ,2 ,1[
>>> 

more info at pydocs-repr