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中的字典来处理索引统计的方法
May 05 Python
python图像处理之反色实现方法
May 30 Python
详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数
Apr 18 Python
Python爬虫抓取代理IP并检验可用性的实例
May 07 Python
异步任务队列Celery在Django中的使用方法
Jun 07 Python
Python中flatten( )函数及函数用法详解
Nov 02 Python
解决pytorch DataLoader num_workers出现的问题
Jan 14 Python
Python 2种方法求某个范围内的所有素数(质数)
Jan 31 Python
解决tensorflow 释放图,删除变量问题
Jun 23 Python
python包的导入方式总结
Mar 02 Python
python 下载文件的几种方式分享
Apr 07 Python
python opencv旋转图片的使用方法
Jun 04 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
一个连接两个不同MYSQL数据库的PHP程序
2006/10/09 PHP
php curl常见错误:SSL错误、bool(false)
2011/12/28 PHP
php判断终端是手机还是电脑访问网站的思路及代码
2013/04/24 PHP
PHP整数取余返回负数的相关解决方法
2014/05/15 PHP
PHP实现时间比较和时间差计算的方法示例
2017/07/24 PHP
javascript五图轮播切换实用版
2012/08/17 Javascript
js图片模糊切换显示特效的方法
2015/02/17 Javascript
简介JavaScript中strike()方法的使用
2015/06/08 Javascript
JavaScript中模拟实现jsonp
2015/06/19 Javascript
jquery中ajax处理跨域的三大方式
2016/01/05 Javascript
jQuery继承extend用法详解
2016/10/10 Javascript
JavaScript定时器实现的原理分析
2016/12/06 Javascript
JavaScript之浏览器对象_动力节点Java学院整理
2017/07/03 Javascript
AjaxUpLoad.js实现文件上传
2018/03/05 Javascript
JS监听组合按键思路及实现过程
2020/04/17 Javascript
vue-cli4.x创建企业级项目的方法步骤
2020/06/18 Javascript
vue3+typeScript穿梭框的实现示例
2020/12/29 Vue.js
python爬虫获取淘宝天猫商品详细参数
2020/06/23 Python
浅谈DataFrame和SparkSql取值误区
2018/06/09 Python
pycharm 实现显示project 选项卡的方法
2019/01/17 Python
python re库的正则表达式入门学习教程
2019/03/08 Python
关于pycharm中pip版本10.0无法使用的解决办法
2019/10/10 Python
Python超越函数积分运算以及绘图实现代码
2019/11/20 Python
python语言的优势是什么
2020/06/17 Python
HTML5打开手机扫码功能及优缺点
2017/11/27 HTML / CSS
西雅图的买手店:Totokaelo
2019/10/19 全球购物
说出你对remoting 和webservice的理解和应用
2014/06/08 面试题
酒店保安领班职务说明书
2014/03/04 职场文书
工程承包协议书
2014/04/22 职场文书
大型主题婚礼活动策划方案
2014/09/15 职场文书
车间主任岗位职责
2015/02/03 职场文书
大学生求职自荐信范文
2015/03/04 职场文书
教师求职简历自我评价
2015/03/10 职场文书
红歌会主持词
2015/07/02 职场文书
幼儿园教师教学反思
2016/03/02 职场文书
go xorm框架的使用
2021/05/22 Golang