为什么一个项目的元组是没有元组的 [英] Why tuple with one item is no tuple

查看:69
本文介绍了为什么一个项目的元组是没有元组的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


type([''1''])
< type ''list''>

type(('''''))
type([''1'']) <type ''list''>
type((''1''))



< type'' str''>


我想知道为什么(''1'')没有元组????


因为我必须对待这个特殊的我的代码中的情况不同。


-

格雷格


<type ''str''>

I wonder why (''1'') is no tuple????

Because I have to treat this "special" case differently in my code.

--
Greg

推荐答案

(''1'')是一个元组...你需要逗号才能使它成为一个元组。


问候,


模糊
http://www.voidspace.org.uk /python/index.shtml

(''1'',) is a tuple... you need the comma to make it a tuple.

regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml


2005年3月15日星期二16:16:34 GMT,Gregor Horvath< g。** *****@mx.at>写道:
On Tue, 15 Mar 2005 16:16:34 GMT, Gregor Horvath <g.*******@mx.at> wrote:

>>> type([''1''])<输入''list''>
>>>类型((''1''))< type''str''>

我想知道为什么( ''1'')没有元组????


因为,从语法上讲,那些parens用于分组,而不是
明确定义一个元组。这是一个蟒蛇陷阱。要定义

一元组,在''1'后面加一个逗号:
>>>type([''1'']) <type ''list''>
>>>type((''1'')) <type ''str''>

I wonder why (''1'') is no tuple????
because, syntactically, those parens are for grouping, and do not
unambiguously define a tuple. It''s a python gotcha. To define a
one-tuple, put a comma after the ''1'':
type((''1''))
type((''1'',))



< type''tuple''>

因为我必须对待这个特殊的在我的代码中有不同的情况。


<type ''tuple''>

Because I have to treat this "special" case differently in my code.




你不应该这样做;如果你仍然认为你发布你的代码。


和平

Bill Mill

bill.mill at gmail.com



you shouldn''t have to; post your code if you still think you do.

Peace
Bill Mill
bill.mill at gmail.com


Gregor Horvath写道:
Gregor Horvath wrote:

>> ;>类型([''1''])<类型''列表''>
>>>类型((''1''))< type''str' '>

我想知道为什么(''1'')没有元组????

因为我必须对待这个特殊的我的代码中的情况有所不同。
>>>type([''1'']) <type ''list''>
>>>type((''1'')) <type ''str''>

I wonder why (''1'') is no tuple????

Because I have to treat this "special" case differently in my code.




你需要告诉python(''1'')里面没有字符串

a couple parens但是一个元组,看:



you need to tell python that (''1'') isn''t a string inside
a couple parens but a tuple, look:

t =(''1'',)
type(t)
< type''tuple''> ;


如果没有歧义你可以省略parens:

t =''1'',
type(t)
t = (''1'', )
type(t) <type ''tuple''>

if there''s no ambiguity you can omit the parens:
t = ''1'',
type(t)



< type''tuple''>


HTH,

deelan


-

@prefix foaf:< http://xmlns.com/foaf/0.1/> 。

< #me>一个人:人; foaf:nickdeelan ;

foaf:weblog< http://blog.deelan.com/> 。


<type ''tuple''>

HTH,
deelan

--
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> a foaf:Person ; foaf:nick "deelan" ;
foaf:weblog <http://blog.deelan.com/> .


这篇关于为什么一个项目的元组是没有元组的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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