人物包含自己? [英] Characters contain themselves?

查看:76
本文介绍了人物包含自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在测试处理泛型列表的递归算法时,我偶然发现无限循环上的
,这是由(至少对于我的
$ b)触发的事实$ b版本的Pyton)字符包含自己。查看会话:


系统提示%python

Python 2。3。5(#2,2005年2月9日,00 :38:15)

[GCC 3.3.5(Debian 1:3.3.5-8)] on linux2

输入help,copyright, "币"或许可证或欲了解更多信息。

....

''''是''a' '
True''a''''a''
True''a''in [''a'']
True ....




导致悖论和循环包含自身的对象(以及其他类型的怪物)在Axiom的集合理论中被杀死

基金会:=)


但是,让我们回到更加尘世的事情。用以下Python字符串常见问题解答搜索后,我无法在

python FAQ中找到任何线索。

关于为什么这个设计选择以及如何避免掉落在这个陷阱中

,无需在任何地方乱丢我的代码,每次处理一个通用的项目列表时,我都会使用b * b



任何提示都将不胜感激。

解决方案

WENDUM Denis 47.76.11(代理人):

在测试处理泛型列表的递归算法时,我偶然发现无限循环,这是由(至少我的Pyton版本的Pyton)字符包含自身的事实触发的。




不,字符串包含字符。 ''a''是由一个

字符组成的字符串。


"字符串的项目是字符。没有单独的角色

类型;字符由一个项目的字符串表示。
http ://docs.python.org/ref/types.html


(一种类型,一种可能会问)


-
$ b $bRenéPijlman


事实上,不仅仅是字符,而是字符串包含自己:

''abc''in''abc''
True


这是一个非常好的(即清晰简洁的)快捷方式:

''西班牙的雨主要是''。找(''rain'')!= -1



真的


我总是发现扭曲和尴尬。


你能成为一个关于你的投诉更具体一点吗?


- George

[谢谢,我确实喜欢查找基础公理!]


Rene Pijlman< re ******************** @ my.address.is.invalid>写道:

WENDUM Denis 47.76.11(代理人):

在测试处理泛型列表的递归算法时,我偶然发现了无限循环,这是由事实引发的(至少对于我的
版本的Pyton)字符包含自己。



不,字符串包含字符。 ''a''是由一个
字符组成的字符串。

"字符串的项目是字符。没有单独的字符
类型;字符由一个项目的字符串表示。
http ://docs.python.org/ref/types.html

(可能会问什么类型的一个项目)




好​​点。 " ;. 。用一串长度为1的字符串表示。将是

更好。

-

Mark Jackson - http://www.alumni.caltech.edu/~mjackson

基于与现实隔绝的理论的信息系统

必然会失败。 - Mitch Kabay



While testing recursive algoritms dealing with generic lists I stumbled
on infinite loops which were triggered by the fact that (at least for my
version of Pyton) characters contain themselves.See session:

system prompt% python
Python 2.3.5 (#2, Feb 9 2005, 00:38:15)
[GCC 3.3.5 (Debian 1:3.3.5-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
....

''a'' is ''a'' True ''a'' in ''a'' True ''a'' in [''a''] True ....



Leading to paradoxes and loops objects which contain themselves (and
other kinds of monsters) are killed in set theory with the Axiom of
Foundation:=)

But let''s go back to more earthly matters. I couldn''t find any clue in a
python FAQ after having googled with the following "Python strings FAQ"
about why this design choice and how to avoid falling in this trap
without having to litter my code everywhere with tests for stringiness
each time I process a generic list of items.

Any hints would be appreciated.

解决方案

WENDUM Denis 47.76.11 (agent):

While testing recursive algoritms dealing with generic lists I stumbled
on infinite loops which were triggered by the fact that (at least for my
version of Pyton) characters contain themselves.



No, strings contain characters. And ''a'' is a string consisting of one
character.

"The items of a string are characters. There is no separate character
type; a character is represented by a string of one item."
http://docs.python.org/ref/types.html

(One item of what type, one might ask)

--
René Pijlman


In fact, not just characters, but strings contain themselves:

''abc'' in ''abc'' True

This is a very nice(i.e. clear and concise) shortcut for:
''the rain in spain stays mainly''.find(''rain'') != -1


True

Which I always found contorted and awkward.

Could you be a bit more concrete about your complaint?

-- George
[Thanks, I did enjoy looking up the Axiom of Foundation!]


Rene Pijlman <re********************@my.address.is.invalid> writes:

WENDUM Denis 47.76.11 (agent):

While testing recursive algoritms dealing with generic lists I stumbled
on infinite loops which were triggered by the fact that (at least for my
version of Pyton) characters contain themselves.



No, strings contain characters. And ''a'' is a string consisting of one
character.

"The items of a string are characters. There is no separate character
type; a character is represented by a string of one item."
http://docs.python.org/ref/types.html

(One item of what type, one might ask)



Good point. ". . .represented by a string of length one" would be
better.
--
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
An information system based on theory isolated from reality
is bound to fail. - Mitch Kabay


这篇关于人物包含自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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