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 Mysql数据库操作 Perl操作Mysql数据库
Jan 12 Python
在Python的Bottle框架中使用微信API的示例
Apr 23 Python
PHP网页抓取之抓取百度贴吧邮箱数据代码分享
Apr 13 Python
对Python中9种生成新对象的方法总结
May 23 Python
利用python实现对web服务器的目录探测的方法
Feb 26 Python
Django项目后台不挂断运行的方法
Aug 31 Python
Django框架 Pagination分页实现代码实例
Sep 04 Python
Python通过递归获取目录下指定文件代码实例
Nov 07 Python
基于Tensorflow批量数据的输入实现方式
Feb 05 Python
python通过matplotlib生成复合饼图
Feb 06 Python
python 绘制国旗的示例
Sep 27 Python
Python-split()函数实例用法讲解
Dec 18 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
深入了解php4(1)--回到未来
2006/10/09 PHP
PHP获取指定月份第一天和最后一天的方法
2015/07/18 PHP
PHP版本升级到7.x后wordpress的一些修改及wordpress技巧
2015/12/25 PHP
深入浅出讲解:php的socket通信原理
2016/12/03 PHP
php读取XML的常见方法实例总结
2017/04/25 PHP
PHP中Static(静态)关键字功能与用法实例分析
2019/04/05 PHP
php使用Swoole实现毫秒级定时任务的方法
2020/09/04 PHP
nodejs win7下安装方法
2012/05/24 NodeJs
Javascript操作cookie的函数代码
2012/10/03 Javascript
用JavaScript计算在UTF-8下存储字符串占用字节数
2013/08/08 Javascript
详细解读Jquery各Ajax函数($.get(),$.post(),$.ajax(),$.getJSON())
2016/08/15 Javascript
关于vue.js弹窗组件的知识点总结
2016/09/11 Javascript
Angularjs中的页面访问权限怎么设置
2016/11/11 Javascript
js 中获取制定的cook信息实现方法
2016/11/19 Javascript
微信小程序 检查接口状态实例详解
2017/06/23 Javascript
网页中的图片查看器viewjs使用方法
2017/07/11 Javascript
js实现一个简易计算器
2020/03/30 Javascript
vue3实现v-model原理详解
2019/10/09 Javascript
JavaScript实现更换背景图片
2019/10/18 Javascript
python中zip和unzip数据的方法
2015/05/27 Python
wxPython中listbox用法实例详解
2015/06/01 Python
详解Python编程中基本的数学计算使用
2016/02/04 Python
利用Python如何制作好玩的GIF动图详解
2018/07/11 Python
Python实现的多叉树寻找最短路径算法示例
2018/07/30 Python
python matplotlib画盒图、子图解决坐标轴标签重叠的问题
2020/01/19 Python
python实现将列表中各个值快速赋值给多个变量
2020/04/02 Python
css3实现3D色子翻转特效
2014/12/23 HTML / CSS
HelloFresh澳大利亚:订购你的美味食品盒、健康餐食
2018/03/28 全球购物
上课迟到检讨书100字
2014/01/11 职场文书
自荐信需注意事项
2014/01/25 职场文书
学习雷锋精神活动总结
2015/02/06 职场文书
2015仓库保管员年终工作总结
2015/05/13 职场文书
消防验收申请报告
2015/05/15 职场文书
歌舞青春观后感
2015/06/10 职场文书
小学中队长竞选稿
2015/11/20 职场文书
Win11软件图标固定到任务栏
2022/04/19 数码科技