如何强制单个数字成为元组 [英] How to force a single number to be a tuple

查看:432
本文介绍了如何强制单个数字成为元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,


我有一个数字序列,它被放入这样的元组:


y = 2,3.0, 4.5


当序列超过1时,我可以将序列作为元组进行操作。但是当序列只有1个数字时,

,比如


y = 2


我很难操纵它作为一个元组。我想必须要有一种方法可以将一个数字作为一个元组来代替
。有人可以告诉我吗?


谢谢,

金明


__________________________________________________ _______________

使用新的MSN工具栏免费弹出窗口阻止?立即获取!
http:/ /toolbar.msn.click-url.com/go/...ave/direct/01/

解决方案

在文章< ma ************************************** @ python.o rg> ,

金明旭 < CY ******** @ hotmail.com>写道:

嗨伙计,

我有一个数字序列,它被放入这样的元组:

y = 2,3.0,4.5

当序列超过1时,我可以将序列作为元组进行操作。但是当序列只有1个数字时,就好了,我很难把它当作一个元组来操纵它。我想必须有办法让一个数字成为一个元组。有人可以告诉我吗?




你会得到一个很好的回应。


语法有点时髦:


y =(2,)


你是对这个问题的回应很多。

语法有点时髦:

y =(2,)



时髦但是,非常符合逻辑。您也可以写:


(1,2,3,4,)


而不是


(1,2,3,4)

语法非常清晰合理。 (像往常一样使用

python。)尝试为每个元素添加一个逗号 - 这将完成这些工作。

大多数语言都不是那么简单 - 他们禁止上一个

逗号。

Python是最好的。 :-)


Gandalf写道:

y =(2,)
时髦但是,非常符合逻辑。你也可以这样写:


()==(,)
File" ;< stdin>",第1行

()==(,)

^

语法错误:语法无效(1)= =(1,)
False(1,2)==(1,2,)
True




非常合乎逻辑......

Python是最好的。 : - )




当然,以上不能承受:-)


彼得


Hi Folks,

I have a number sequence, which is put into a tuple like this:

y=2, 3.0, 4.5

I can manipulate the sequence as a tuple when it has more than 1 number. But
when the sequence has only 1 number, like

y=2

I have trouble to manipulate it as a tuple. I guess there must be a way to
forece a single number to be a tuple. Could anyone please tell me that?

Thanks,

Jinming

__________________________________________________ _______________
FREE pop-up blocking with the new MSN Toolbar ? get it now!
http://toolbar.msn.click-url.com/go/...ave/direct/01/

解决方案

In article <ma**************************************@python.o rg>,
"Jinming Xu" <cy********@hotmail.com> wrote:

Hi Folks,

I have a number sequence, which is put into a tuple like this:

y=2, 3.0, 4.5

I can manipulate the sequence as a tuple when it has more than 1 number. But
when the sequence has only 1 number, like

y=2

I have trouble to manipulate it as a tuple. I guess there must be a way to
forece a single number to be a tuple. Could anyone please tell me that?



You''re going to get a zillion responses to this one.

The syntax is a bit funky:

y = (2,)


You''re going to get a zillion responses to this one.

The syntax is a bit funky:

y = (2,)


Funky. However, very logical. You can also write:

(1,2,3,4,)

instead of

(1,2,3,4)

The syntax is very clear and logical. (As usual when working with
python.) Try to add one comma for each element - that will do the stuff.
Most of the languages are not so straightforward - they forbid the last
comma.
Python is the best. :-)


Gandalf wrote:

y = (2,)
Funky. However, very logical. You can also write:


() == (,) File "<stdin>", line 1
() == (,)
^
SyntaxError: invalid syntax (1) == (1,) False (1, 2) == (1, 2,) True



Very logical indeed...
Python is the best. :-)



Of course, the above not withstanding :-)

Peter


这篇关于如何强制单个数字成为元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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