python判断变量是否为列表的方法


Posted in Python onSeptember 17, 2020

python的数据类型有:数字(int)、浮点(float)、字符串(str),列表(list)、元组(tuple)、字典(dict)、集合(set)。

一般通过以下方法进行判断:

1、isinstance(参数1,参数2)

描述:该函数用来判断一个变量(参数1)是否是已知的变量类型(参数2) 类似于type()

参数1:变量

参数2:可以是直接或间接类名、基本类型或者由它们组成的元组。

返回值:如果对象的类型与参数二的类型(classinfo)相同则返回 True,否则返回 False。

例子:

#判断变量类型的函数
def typeof(variate):
    type=None
    if isinstance(variate,int):
        type = "int"
    elif isinstance(variate,str):
        type = "str"
    elif isinstance(variate,float):
        type = "float"
    elif isinstance(variate,list):
        type = "list"
    elif isinstance(variate,tuple):
        type = "tuple"
    elif isinstance(variate,dict):
        type = "dict"
    elif isinstance(variate,set):
        type = "set"
    return type
# 返回变量类型
def getType(variate):
    arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
    vartype = typeof(variate)
    if not (vartype in arr):
        return "未知类型"
    return arr[vartype]
     
#判断变量是否为整数
money=120
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为字符串
money="120"
print("{0}是{1}".format(money,getType(money)))
money=12.3
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为列表
students=['studentA']
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为元组
students=('studentA','studentB')
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为字典
dictory={"key1":"value1","key2":"value2"}
print("{0}是{1}".format(dictory,getType(dictory)))
#判断变量是否为集合
apple={"apple1","apple2"}
print("{0}是{1}".format(apple,getType(apple)))

返回:

python判断变量是否为列表的方法

2、通过与已知类型的常量进行比较

例子:

#判断变量类型的函数
def typeof(variate):
    type1 = ""
    if type(variate) == type(1):
        type1 = "int"
    elif type(variate) == type("str"):
        type1 = "str"
    elif type(variate) == type(12.3):
        type1 = "float"
    elif type(variate) == type([1]):
        type1 = "list"
    elif type(variate) == type(()):
        type1 = "tuple"
    elif type(variate) == type({"key1":"123"}):
        type1 = "dict"
    elif type(variate) == type({"key1"}):
        type1 = "set"
    return type1
# 返回变量类型
def getType(variate):
    arr = {"int":"整数","float":"浮点","str":"字符串","list":"列表","tuple":"元组","dict":"字典","set":"集合"}
    vartype = typeof(variate)
    if not (vartype in arr):
      return "未知类型"
    return arr[vartype]

#判断变量是否为整数
money=120
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为字符串
money="120"
print("{0}是{1}".format(money,getType(money)))
money=12.3
print("{0}是{1}".format(money,getType(money)))
#判断变量是否为列表
students=['studentA']
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为元组
students=('studentA','studentB')
print("{0}是{1}".format(students,getType(students)))
#判断变量是否为字典
dictory={"key1":"value1","key2":"value2"}
print("{0}是{1}".format(dictory,getType(dictory)))
#判断变量是否为集合
apple={"apple1","apple2"}
print("{0}是{1}".format(apple,getType(apple)))

返回:

python判断变量是否为列表的方法

isinstance() 与 type() 区别:

type() 不会认为子类是一种父类类型,不考虑继承关系。

isinstance() 会认为子类是一种父类类型,考虑继承关系。

如果要判断两个类型是否相同推荐使用 isinstance()。

以上就是python判断变量是否为列表的方法的详细内容,更多关于python如何判断变量是否为列表的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
使用Python获取CPU、内存和硬盘等windowns系统信息的2个例子
Apr 15 Python
Python使用urllib模块的urlopen超时问题解决方法
Nov 08 Python
windows下Python实现将pdf文件转化为png格式图片的方法
Jul 21 Python
Python之批量创建文件的实例讲解
May 10 Python
利用pandas读取中文数据集的方法
Jul 25 Python
Python 数值区间处理_对interval 库的快速入门详解
Nov 16 Python
Django MEDIA的配置及用法详解
Jul 25 Python
python的常见矩阵运算(小结)
Aug 07 Python
Python 经典算法100及解析(小结)
Sep 13 Python
python 日志模块 日志等级设置失效的解决方案
May 26 Python
Python基于pillow库实现生成图片水印
Sep 14 Python
Anaconda安装pytorch和paddle的方法步骤
Apr 03 Python
Django实现文章详情页面跳转代码实例
Sep 16 #Python
如何基于Django实现上下文章跳转
Sep 16 #Python
Python通过类的组合模拟街道红绿灯
Sep 16 #Python
python如何绘制疫情图
Sep 16 #Python
如何用Python绘制3D柱形图
Sep 16 #Python
Python Merge函数原理及用法解析
Sep 16 #Python
简单了解Python字典copy与赋值的区别
Sep 16 #Python
You might like
php 表单提交大量数据发生丢失的解决方法
2014/03/03 PHP
PHP微信开发之二维码生成类
2015/06/26 PHP
yii2分页之实现跳转到具体某页的实例代码
2016/06/02 PHP
基于jQueryUI和Corethink实现百度的搜索提示功能
2016/11/09 PHP
Thinkphp5结合layer弹窗定制操作结果页面
2017/07/07 PHP
php中加密解密DES类的简单使用方法示例
2020/03/26 PHP
jQuery 源码分析笔记(5) jQuery.support
2011/06/19 Javascript
js判断上传文件的类型和大小示例代码
2013/10/18 Javascript
IE中JS跳转丢失referrer问题的2个解决方法
2014/07/18 Javascript
jQuery使用append在html元素后同时添加多项内容的方法
2015/03/26 Javascript
JQuery插件Marquee.js实现无缝滚动效果
2016/04/26 Javascript
JQuery解析XML数据的几个简单实例
2016/05/18 Javascript
jQuery中map函数的两种方式
2017/04/07 jQuery
微信小程序开发中的疑问解答汇总
2017/07/03 Javascript
jQuery的时间datetime控件在AngularJs中的使用实例(分享)
2017/08/17 jQuery
JavaScript闭包原理与用法实例分析
2018/08/10 Javascript
点击按钮弹出模态框的一系列操作代码实例
2019/03/29 Javascript
微信小程序 多行文本显示...+显示更多按钮和收起更多按钮功能
2019/09/26 Javascript
Vue使用Three.js加载glTF模型的方法详解
2020/06/14 Javascript
JavaScript实现京东快递单号查询
2020/11/30 Javascript
Python的装饰器模式与面向切面编程详解
2015/06/21 Python
python得到电脑的开机时间方法
2018/10/15 Python
python+opencv实现摄像头调用的方法
2019/06/22 Python
Python CVXOPT模块安装及使用解析
2019/08/01 Python
详解CSS3的opacity属性设置透明效果的用法
2016/05/09 HTML / CSS
详解css3中的伪类before和after常见用法
2020/11/17 HTML / CSS
Myprotein荷兰官网:欧洲第一运动营养品牌
2020/07/11 全球购物
MIKI HOUSE美国官方网上商店:日本领先的婴儿和儿童高级时装品牌
2020/06/21 全球购物
php优化查询foreach代码实例讲解
2021/03/24 PHP
大型活动策划方案
2014/01/12 职场文书
市场部规章制度
2014/01/24 职场文书
幼儿园元旦亲子活动方案
2014/02/17 职场文书
校庆活动策划方案
2014/06/05 职场文书
2014年加油站站长工作总结
2014/12/23 职场文书
2019XX公司员工考核管理制度!
2019/08/07 职场文书
分享:关于学习的励志名言赏析
2019/08/16 职场文书