Python 如何将元组转换为不带逗号和括号的值字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17426386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:10:21  来源:igfitidea点击:

How to transform a tuple to a string of values without comma and parentheses

python

提问by Below the Radar

I retrieved data from a sql query by using

我通过使用从 sql 查询中检索数据

bounds = cursor.fetchone()

And I get a tuple like:

我得到一个像这样的元组:

(34.2424, -64.2344, 76.3534, 45.2344)

And I would like to have a string like 34.2424 -64.2344 76.3534 45.2344

我想要一个像 34.2424 -64.2344 76.3534 45.2344

Does a function exist that can do that?

是否存在可以做到这一点的功能?

采纳答案by TerryA

Use str.join():

使用str.join()

>>> mystring = ' '.join(map(str, (34.2424, -64.2344, 76.3534, 45.2344)))
>>> print mystring
34.2424 -64.2344 76.3534 45.2344

You'll have to use map here (which converts all the items in the tuple to strings) because otherwise you will get a TypeError.

您必须在此处使用 map(它将元组中的所有项目转换为字符串),否则您将获得TypeError.



A bit of clarification on the map()function:

关于map()函数的一些说明:

map(str, (34.2424, -64.2344, 76.3534, 45.2344)is equivalent to [str(i) for i in (34.2424, -64.2344, 76.3534, 45.2344)].

map(str, (34.2424, -64.2344, 76.3534, 45.2344)相当于[str(i) for i in (34.2424, -64.2344, 76.3534, 45.2344)]

It's a tiny bit faster than using a list comprehension:

它比使用列表理解要快一点:

$ python -m timeit "map(str, (34.2424, -64.2344, 76.3534, 45.2344))"
1000000 loops, best of 3: 1.93 usec per loop
$ python -m timeit "[str(i) for i in (34.2424, -64.2344, 76.3534, 45.2344)]"
100000 loops, best of 3: 2.02 usec per loop

As shown in the comments to this answer, str.join()can take a generator instead of a list. Normally, this would be faster, but in this case, it is slower.

如对此答案的评论所示,str.join()可以使用生成器而不是列表。通常,这会更快,但在这种情况下,它会更慢

If I were to do:

如果我要这样做:

' '.join(itertools.imap(str, (34.2424, -64.2344, 76.3534, 45.2344)))

It would be slower than using map(). The difference is that imap()returns a generator, while map()returns a list (in python 3 it returns a generator)

它会比使用map(). 区别在于imap()返回一个生成器,而map()返回一个列表(在python 3中它返回一个生成器)

If I were to do:

如果我要这样做:

''.join(str(i) for i in (34.2424, -64.2344, 76.3534, 45.2344))

It would be slower than putting brackets around the listcomprehension, because of reasons explained here.

由于这里解释的原因,它会比在列表理解中加括号要慢。



In your (OP's) case, either option does not really matter, as performance doesn't seem like a huge deal here. But if you are ever dealing with large tuples of floats/integers, then now you know what to use for maximum efficiency :).

在您(OP)的情况下,任何一个选项都无关紧要,因为这里的性能似乎没什么大不了的。但是,如果您曾经处理过大的浮点数/整数元组,那么现在您知道如何使用以获得最大效率:)。

回答by Roman Pekar

Try this

尝试这个

>>> a = (34.2424, -64.2344, 76.3534, 45.2344)
>>> ' '.join(str(i) for i in a)
'34.2424 -64.2344 76.3534 45.2344

回答by utter_step

If I've got your message, you are getting tuple of floats, am I right?

如果我收到您的消息,您将收到浮点数元组,对吗?

If so, the following code should work:

如果是这样,以下代码应该可以工作:

In [1]: t = (34.2424 , -64.2344 , 76.3534 , 45.2344)

In [2]: ' '.join([str(x) for x in t])
Out[2]: '34.2424 -64.2344 76.3534 45.2344'

We're converting every value in tuple to string here, because str.joinmethod can work only with lists of string. If tis a tuple of strings the code will be just ' '.join(t).

我们在这里将元组中的每个值都转换为字符串,因为str.join方法只能处理字符串列表。如果t是字符串元组,则代码将只是' '.join(t).

In case you're getting string in format "(34.2424 , -64.2344 , 76.3534 , 45.2344)", you should firstly get rid of unnescessary parthensis and commas:

如果您在 format 中获得 string "(34.2424 , -64.2344 , 76.3534 , 45.2344)",您应该首先摆脱不必要的 parthensis 和逗号:

In [3]: t = "(34.2424 , -64.2344 , 76.3534 , 45.2344)"

In [4]: t.strip('()')
Out[4]: '34.2424 , -64.2344 , 76.3534 , 45.2344'

In [5]: numbers = t.strip('()')

In [6]: numbers.split(' , ')
Out[6]: ['34.2424', '-64.2344', '76.3534', '45.2344']

In [7]: ' '.join(numbers.split(' , '))
Out[7]: '34.2424 -64.2344 76.3534 45.2344'

回答by ohruunuruus

You can also use str.format()to produce any arbitrary formatting if you're willing to use *magic. To handle the specific case of this question, with a single separator, is actually a little cumbersome:

str.format()如果您愿意使用*魔法,也可以使用生成任意格式。处理这个问题的具体情况,用一个分隔符,其实有点麻烦:

>>> bounds = (34.2424, -64.2344, 76.3534, 45.2344)
>>> "{} {} {} {}".format(*bounds)

34.2424 -64.2344 76.3534 45.2344

A more robust version that handles any length, like join, is:

处理任何长度的更强大的版本,例如join,是:

>>> len(bounds)*"{} ".format(*bounds)

But the value added is that if you want to extend your formatting to something more involved you've got the option:

但增加的价值是,如果您想将格式扩展到更多涉及的内容,您可以选择:

>>> "{} --> | {:>10} | {:>10} | {:>10} |".format(*bounds)

34.2424 --> |   -64.2344 |    76.3534 |    45.2344 |

From here, your string formatting optionsare very diverse.

从这里开始,您的字符串格式选项非常多样化。

回答by Ethan Brimhall

You could simply use the following code:

您可以简单地使用以下代码:

i = 0
for row in data:
       print(row[i])
       i++