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学习资料
Feb 08 Python
Python3网络爬虫之使用User Agent和代理IP隐藏身份
Nov 23 Python
浅谈Python Opencv中gamma变换的使用详解
Apr 02 Python
python 将print输出的内容保存到txt文件中
Jul 17 Python
Tesserocr库的正确安装方式
Oct 19 Python
python实现屏保程序(适用于背单词)
Jul 30 Python
django 单表操作实例详解
Jul 30 Python
Python中base64与xml取值结合问题
Dec 22 Python
python 实现屏幕录制示例
Dec 23 Python
python生成大写32位uuid代码
Mar 03 Python
PyQt5如何将.ui文件转换为.py文件的实例代码
May 26 Python
教你如何用python开发一款数字推盘小游戏
Apr 14 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加密解密的代码
2007/07/16 PHP
优化PHP代码的53条建议
2008/03/27 PHP
PHP 批量更新网页内容实现代码
2010/01/05 PHP
php+xml实现在线英文词典之添加词条的方法
2015/01/23 PHP
thinkphp命名空间用法实例详解
2015/12/30 PHP
textContent在Firefox下与innerText等效的属性
2007/05/12 Javascript
Jquery为a标签的href赋值实现代码
2013/05/03 Javascript
如何获取select下拉框的值(option没有及有value属性)
2013/11/08 Javascript
JS简单实现元素复制示例附图
2013/11/19 Javascript
jQuery中:eq()选择器用法实例
2014/12/29 Javascript
使用javascript实现json数据以csv格式下载
2015/01/09 Javascript
jQuery实现的简洁下拉菜单导航效果代码
2015/08/26 Javascript
Javascript基础回顾之(一) 类型
2017/01/31 Javascript
JS中mouseup事件丢失的原因与解决办法
2017/06/14 Javascript
浅谈vue项目如何打包扔向服务器
2018/05/08 Javascript
spring+angular实现导出excel的实现代码
2019/02/27 Javascript
js实现一款简单踩白块小游戏(曾经很火)
2019/12/02 Javascript
vue-preview动态获取图片宽高并增加旋转功能的实现
2020/07/29 Javascript
[01:19:34]2014 DOTA2国际邀请赛中国区预选赛 New Element VS Dream time
2014/05/22 DOTA
在Python程序中操作文件之flush()方法的使用教程
2015/05/24 Python
Python多进程原理与用法分析
2018/08/21 Python
flask利用flask-wtf验证上传的文件的方法
2020/01/17 Python
Python实现AI自动抠图实例解析
2020/03/05 Python
python如何调用php文件中的函数详解
2020/12/29 Python
HTML5 使用 sessionStorage 进行页面传值的方法
2018/07/02 HTML / CSS
欧洲顶级的童装奢侈品购物网站:Bambini Fashion(面向全球)
2018/04/24 全球购物
广告学专业毕业生自荐信
2013/09/24 职场文书
应届毕业生就业自荐信
2013/10/26 职场文书
毕业生实习鉴定
2013/12/11 职场文书
投标人法定代表人授权委托书格式
2014/09/28 职场文书
三孔导游词
2015/02/05 职场文书
2015年全国爱眼日活动小结
2015/02/27 职场文书
检讨书怎么写
2015/05/07 职场文书
《当代神农氏》教学反思
2016/02/23 职场文书
《山中访友》教学反思
2016/02/24 职场文书
宫崎骏十大动画电影,宫崎骏好看的动画电影排名
2022/03/22 日漫