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中处理unchecked未捕获异常实例
Jan 17 Python
python基于pygame实现响应游戏中事件的方法(附源码)
Nov 11 Python
python+requests+unittest API接口测试实例(详解)
Jun 10 Python
利用python解决mysql视图导入导出依赖的问题
Dec 17 Python
Python实现学校管理系统
Jan 11 Python
Python中pow()和math.pow()函数用法示例
Feb 11 Python
python批量查询、汉字去重处理CSV文件
May 31 Python
Python爬取个人微信朋友信息操作示例
Aug 03 Python
对python 合并 累加两个dict的实例详解
Jan 21 Python
Python 3.8 新功能大揭秘【新手必学】
Feb 05 Python
基于python计算并显示日间、星期客流高峰
May 07 Python
Python计算矩阵的和积的实例详解
Sep 10 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
解析thinkphp中的M()与D()方法的区别
2013/06/22 PHP
php图片缩放实现方法
2014/02/20 PHP
PHP中基本HTTP认证技巧分析
2015/03/16 PHP
JavaScript Date对象使用总结
2009/05/14 Javascript
JQuery 前台切换网站的样式实现
2009/06/22 Javascript
ModelDialog JavaScript模态对话框类代码
2011/04/17 Javascript
javascript字母大小写转换的4个函数详解
2014/05/09 Javascript
JavaScript实现ASC转汉字及汉字转ASC的方法
2016/01/23 Javascript
Javascript中的Prototype到底是什么
2016/02/16 Javascript
jQuery实现的网格线绘制方法
2016/06/20 Javascript
微信小程序 两种为对象属性赋值的方式详解
2017/02/23 Javascript
JavaScript和JQuery获取DIV值的方法示例
2017/03/07 Javascript
对vue事件的延迟执行实例讲解
2018/08/28 Javascript
Vue框架TypeScript装饰器使用指南小结
2019/02/18 Javascript
JS为什么说async/await是generator的语法糖详解
2019/07/11 Javascript
VUE页面中通过双击实现复制表格中内容的示例代码
2020/06/11 Javascript
vue+高德地图实现地图搜索及点击定位操作
2020/09/09 Javascript
[06:40]2014DOTA2西雅图国际邀请赛 DK战队巡礼
2014/07/07 DOTA
记一次python 内存泄漏问题及解决过程
2018/11/29 Python
python程序 创建多线程过程详解
2019/09/23 Python
使用Python串口实时显示数据并绘图的例子
2019/12/26 Python
pytorch 改变tensor尺寸的实现
2020/01/03 Python
pytorch:实现简单的GAN示例(MNIST数据集)
2020/01/10 Python
简单了解Python write writelines区别
2020/02/27 Python
印尼最大的在线购物网站:MatahariMall.com
2016/08/26 全球购物
家用个人磨皮机:Trophy Skin
2017/03/30 全球购物
全球领先的美容用品专卖店:Beauty Plus Salon
2018/09/04 全球购物
哄娃神器4moms商店:美国婴童用品品牌
2019/03/07 全球购物
*p++ 自增p 还是p所指向的变量
2016/07/16 面试题
药品质量检测应届生求职信
2013/11/14 职场文书
社区党总支书记先进事迹材料
2014/01/24 职场文书
党员干部三严三实心得体会
2014/10/13 职场文书
长江三峡导游词
2015/01/31 职场文书
护理专业自我评价
2015/03/11 职场文书
工地材料员岗位职责
2015/04/11 职场文书
vue如何实现关闭对话框后刷新列表
2022/04/08 Vue.js