元组的长度或仅包含一个元素的列表 [英] length of a tuple or a list containing only one element

查看:99
本文介绍了元组的长度或仅包含一个元素的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我对len的行为差异有疑问当应用于
元组或列表时。我的意思是:


$ len((''foo'',''bar''))

2

$ len((''foo''))

3

$ len([''foo'',''bar''])

2

$ len([''foo''])

1


为什么这种长度计算行为一个元组?

对于我的应用程序,我更喜欢列表的长度行为。如果我想

在元组中存储一些值,因为它们不应该被修改,那么元组

元组只包含一个元素困扰我。
< br $>
谢谢


Julien

-

python -c" print''''。加入([chr(154 - ord(c))for c in''* 9(9&(18%.9& 1 +,\''Z

(55l4(''] )


当一位杰出但年长的科学家表示某事可能是有价值的时候,他几乎肯定是对的。当他说某事是<不可能,他很可能是错的。(AC克拉克的第一定律)

解决方案

len(('' foo'',''bar''))

2


len((''foo''))

3


len([''foo'',''bar''])

2

Hi everybody,

I have a question about the difference of behavior of "len" when applied on
tuples or on lists. I mean:

$ len( ( ''foo'', ''bar'' ) )
2
$ len( ( ''foo'' ) )
3
$ len( [ ''foo'', ''bar'' ] )
2
$ len( [ ''foo'' ] )
1

Why this behavior for the length computation of a tuple?
For my application, I prefer the behavior of length for a list. If I want to
store some values in a tuple because they should not be modified, the case
where the tuple contains only one element bothers me.

Thanks

Julien
--
python -c "print ''''.join([chr(154 - ord(c)) for c in ''*9(9&(18%.9&1+,\''Z
(55l4(''])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)

解决方案

len( ( ''foo'', ''bar'' ) )
2


len( ( ''foo'' ) )
3


len( [ ''foo'', ''bar'' ] )
2


这篇关于元组的长度或仅包含一个元素的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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