如何将元组转换为不带逗号和括号的值字符串 [英] How to transform a tuple to a string of values without comma and parentheses

查看:95
本文介绍了如何将元组转换为不带逗号和括号的值字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 sql 查询检索数据

bounds = cursor.fetchone()

然后我得到一个元组:

(34.2424, -64.2344, 76.3534, 45.2344)

我想要一个像 34.2424 -64.2344 76.3534 45.2344

这样的字符串

是否存在可以做到这一点的函数?

解决方案

使用 str.join():

<预><代码>>>>mystring = ' '.join(map(str, (34.2424, -64.2344, 76.3534, 45.2344)))>>>打印我的字符串34.2424 -64.2344 76.3534 45.2344

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

<小时>

map() 的一些说明 功能:

map(str, (34.2424, -64.2344, 76.3534, 45.2344) 等价于 [str(i) for i in (34.2424, -64.2344, 76.3534, 45)]/代码>.

它比使用列表推导式要快一点:

$ python -m timeit "map(str, (34.2424, -64.2344, 76.3534, 45.2344))"1000000 个循环,最好的 3 个:每个循环 1.93 微秒$ python -m timeit "[str(i) for i in (34.2424, -64.2344, 76.3534, 45.2344)]"100000 个循环,最好的 3 个:每个循环 2.02 微秒

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

如果我要这样做:

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

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

如果我要这样做:

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

由于在here列表理解中加括号要慢>.

<小时>

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

I retrieved data from a sql query by using

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

Does a function exist that can do that?

解决方案

Use 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.


A bit of clarification on the map() function:

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)].

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.

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)

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 list comprehension, 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 :).

这篇关于如何将元组转换为不带逗号和括号的值字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆