测试结构 [英] Test for structure

查看:58
本文介绍了测试结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


如何检查变量是否为结构(即列表)?对于我的

特殊问题,变量是字符串或

字符串行列表[''word1'',''word2'',... ]


那么如何测试变量''a''是单个字符串

还是列表?我试过了:


如果是列表:

但这不起作用。我也查看了教程,并使用google

来找到答案,但我没有。


有没有人对此有所了解?


Alex

Hi there,

how can I check if a variable is a structure (i.e. a list)? For my
special problem the variable is either a character string OR a list of
character strings line [''word1'', ''word2'',...]

So how can I test if a variable ''a'' is either a single character string
or a list? I tried:

if a is list:
but that does not work. I also looked in the tutorial and used google
to find an answer, but I did not.

Has anyone an idea about that?

Alex

推荐答案

导入类型


v = [ ]

如果type(v)是types.ListType:

pass

-

问候,


Diez B. Roggisch
import types

v = []
if type(v) is types.ListType:
pass
--
Regards,

Diez B. Roggisch


2005年2月16日星期三07:11:08 -0800(太平洋标准时间),alex

< al ************* @ mpi-hd.mpg.de>写道:
On Wed, 16 Feb 2005 07:11:08 -0800 (PST), alex
<al*************@mpi-hd.mpg.de> wrote:
如何检查变量是否为结构(即列表)?对于我的
特殊问题,变量是字符串或
字符串列表[''word1'',''word2'',...]
那么如何测试变量''a'是单个字符串
还是列表?我试过了:

如果是列表:

但是这不起作用。我也查看了教程并使用google
找到了答案,但我没有。

有没有人对此有所了解?
how can I check if a variable is a structure (i.e. a list)? For my
special problem the variable is either a character string OR a list of
character strings line [''word1'', ''word2'',...]

So how can I test if a variable ''a'' is either a single character string
or a list? I tried:

if a is list:

but that does not work. I also looked in the tutorial and used google
to find an answer, but I did not.

Has anyone an idea about that?




< http://www.brunningonline.net/simon/blog/archives/001349.html>


-

干杯,

Simon B,
si***@brunningonline.net
< a rel =nofollowhref =http://www.brunningonline.net/simon/blog/target =_ blank> http://www.brunningonline.net/simon/blog/


也许你正在寻找内置函数的type()和类型模块?
Perhaps you''re looking for the type() built in function and the types modules?
type(''aaa'')
< type''str''> type([])
< type''list''>导入类型
如果type([])是types.ListType:
type(''aaa'') <type ''str''> type([]) <type ''list''> import types
if type([]) is types.ListType:


.... print''是一个列表''

。 ...

是一个清单


克里斯


2005年2月16日星期三07:10:56 -0800(太平洋标准时间),亚历克斯

< al ************* @ mpi-hd.mpg.de>写道:您好,

如何检查变量是否为结构(即列表)?对于我的
特殊问题,变量是字符串或
字符串列表[''word1'',''word2'',...]
那么如何测试变量''a'是单个字符串
还是列表?我试过了:

如果是列表:

但是这不起作用。我也查看了教程并使用google
找到答案,但我没有。

有没有人对此有所了解?

Alex

-
http:// mail .python.org / mailman / listinfo / python-list

.... print ''is a list''
....
is a list

Chris

On Wed, 16 Feb 2005 07:10:56 -0800 (PST), alex
<al*************@mpi-hd.mpg.de> wrote: Hi there,

how can I check if a variable is a structure (i.e. a list)? For my
special problem the variable is either a character string OR a list of
character strings line [''word1'', ''word2'',...]

So how can I test if a variable ''a'' is either a single character string
or a list? I tried:

if a is list:

but that does not work. I also looked in the tutorial and used google
to find an answer, but I did not.

Has anyone an idea about that?

Alex

--
http://mail.python.org/mailman/listinfo/python-list



-

我们的责任,而不是我们自己,我们应该认真对待

。 - Peter Ustinov


--
"It is our responsibilities, not ourselves, that we should take
seriously." -- Peter Ustinov


这篇关于测试结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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