检查变量是unicode字符串列表还是只有1个unicode字符串

2024-03-29 05:54:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试确定是否存在一个unicode字符串列表,或者只是一个unicode字符串。当然,我想对两者都采取行动,打印列表中的项目或打印单个字符串。你知道吗

我试图使用if isinstance(variable_here, basestring):,但是由于变量是一个字符串列表,我认为它在两个计数中都被标识为True。任何帮助都将不胜感激

示例

变量可以有1项或多项。你知道吗

[u'one']

[u'one',u'two']


Tags: 项目字符串true示例列表ifhereunicode
1条回答
网友
1楼 · 发布于 2024-03-29 05:54:32

我想你是在尝试这样做:

if any(not isinstance(variable, unicode) for variable in variable_here):
     print 'Not a unicode list'
else:
     var_len = len(variable_here)
     if var_len == 1:
         print 'single object'
     elif var_len > 1:
         print 'multiple object'
     else:
         print 'empty list'

相关问题 更多 >