In python is there an easy way to tell if something is not a sequence? I tried to just do:

if x is not sequence but python did not like that

解决方案

iter(x) will raise a TypeError if x cannot be iterated on -- but that check "accepts" sets and dictionaries, though it "rejects" other non-sequences such as None and numbers.

On the other hands, strings (which most applications want to consider "single items" rather than sequences) are in fact sequences (so, any test, unless specialcased for strings, is going to confirm that they are). So, such simple checks are often not sufficient.

In Python 2.6 and better, abstract base classes were introduced, and among other powerful features they offer more good, systematic support for such "category checking".

>>> import collections

>>> isinstance([], collections.Sequence)

True

>>> isinstance((), collections.Sequence)

True

>>> isinstance(23, collections.Sequence)

False

>>> isinstance('foo', collections.Sequence)

True

>>> isinstance({}, collections.Sequence)

False

>>> isinstance(set(), collections.Sequence)

False

You'll note strings are still considered "a sequence" (since they are), but at least you get dicts and sets out of the way. If you want to exclude strings from your concept of "being sequences", you could use collections.MutableSequence (but that also excludes tuples, which, like strings, are sequences, but are not mutable), or do it explicitly:

import collections

def issequenceforme(obj):

if isinstance(obj, basestring):

return False

return isinstance(obj, collections.Sequence)

Season to taste, and serve hot!-)

python 判断数字序列是否合法_Python:检查对象是否是序列相关推荐

  1. python判断ip地址是否合法_python实现判断一个字符串是否是合法IP地址的示例

    一个刚结束的笔试题目,简单贴一下吧,下面是具体实现: #!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 功能:判断一个字符串是否是合法I ...

  2. python判断ip地址是否合法_Python课堂:判定IP地址合法性的三种方法

    IP IP合法性校验是开发中非常常用的,看起来很简单的判断,作用确很大,写起来比较容易出错,今天我们来总结一下,看一下3种常用的IP地址合法性校验的方法. IPv4的ip地址格式:(1~255).(0 ...

  3. python判断变量名是否合法_Python判断变量名是否合法的方法示例

    问题: 变量名是否合法: 1.变量名可以由字母,数字或者下划线组成 2.变量名只能以字母或者下划线开头 s = 'hello@' 判断变量名的第一个元素是否为字母或者下划线 s[0] 如果第一个元素符 ...

  4. python判断ip能否ping通_python 检查IP是否能ping通,并且分别导入两个文件

    #!/usr/bin/python #-*- coding:utf-8 -*- ''' 此脚本主要实现网页的点击量,除了实现次功能点外,还有三个知识点: 1.随机获取代理ip,通过代理ip访问指定站点 ...

  5. python判断输入数字大于0,对python 判断数字是否小于0的方法详解

    对python 判断数字是否小于0的方法详解 为了精度更准确 可以使用数字的绝对值 < 1.0e-16  或者 < 1.0e-8来对比 abs(Num) 以上这篇对python 判断数字是 ...

  6. python判断数字奇偶_python如果快速判断数字奇数偶数

    python如果快速判断数字奇数偶数 这篇文章主要介绍了python如果快速判断数字奇数偶数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用 按 ...

  7. python判断数字位数_python求数字位数的方法

    第一种: 利用str()函数将数字转化成字符串,再利用len()函数判断位长. 1 a=Int(raw_input("the number you want type in:") ...

  8. python判断数字_Python判断字符串是否为数字

    Python判断一个字符串是否为数字,可以分为两种情况考虑. 字符为纯数字 如果需要检测的数字为纯数字,不包括负号,小数点等,可以使用字符串的isdigit()函数来判断>>> a ...

  9. python docx 合并文档 图片_Python检查Word文件中包含特定关键字的所有页码

    推荐教材:<Python程序设计基础与应用>(ISBN:9787111606178),董付国,机械工业出版社图书详情:配套资源:用书教师可以联系董老师获取教学大纲.课件.源码.教案.考试系 ...

  10. python判断一个对象是否可迭代_python - 在Python中,如何确定对象是否可迭代? - includeStdio...

    有没有像isiterable?我目前找到的唯一解决方案就是打电话 hasattr(myObj, '__iter__') 但我不确定这是多么的愚蠢. 检查__iter__序列类型的工作,但是它会在例如P ...

最新文章

  1. ubuntu 14.04 16.04 18.04使用阿里源
  2. 12月26日二周二次【Python基础语法】
  3. python属性使用教程_Python对象的属性访问过程详解
  4. c语言复制后无法运行,刚学C语言,在Linux下写的代码能正常编译,复制到VC下就无法运行...
  5. Android--Activity四种启动模式
  6. 漫谈边缘计算(三):5G的好拍档
  7. mfc globalalloc能分配多大空间_办公家具定制充分利用办公室空间,提高员工之间工作效率...
  8. 5G终端难占用锚点小区导致无法驻留5G
  9. java创建一个自己的类库_建立并使用自己的类库
  10. Redis高可用群集——主从复制+哨兵模式
  11. Windows Core Audio APIs(一)介绍
  12. 对有序表的查找(快步搜索算法)
  13. C语言限时答题小程序
  14. Swin-Unet跑自己的数据集(Transformer用于语义分割)
  15. Python自动化办公——生成Excel入库统计表
  16. Java.SE01.多线程_案例01
  17. 考研复试专业课面试——C++
  18. 合肥工业大学 慕课 梦溪笔谈 习题答案
  19. 互怼、IPO、雷潮、寒冬,2018 互联网圈的那些事儿
  20. 【运维管理】IT管理员常用的管理、运维工具有哪些?

热门文章

  1. Fiddler快速入门
  2. Windows7下VS2008试用版到期的解决办法
  3. linux培训课程第十三天:ppt以及笔记
  4. 程序员常挂在嘴边的10句话:刚刚还是好的啊!
  5. NLP算法工程师对NLP的一些看法
  6. 【IT旁观者】喜羊羊与唐三藏
  7. 今天的这可是有原因的飞鸽传书
  8. 我花了十多分钟的i698源代码时间
  9. 没事研究下C#虚拟光驱,有所收获!
  10. 【飞秋】关于结构体和结构体指针的P-INVOKE