python判断变量是否为列表的方法
作者:爱喝马黛茶的安东尼 发布时间:2023-05-06 16:22:22
标签:python,变量,列表
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)))
返回:
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)))
返回:
isinstance() 与 type() 区别:
type() 不会认为子类是一种父类类型,不考虑继承关系。
isinstance() 会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()。
来源:https://www.py.cn/jishu/jichu/13026.html


猜你喜欢
- 本文实例分析了Python字符串和文件操作常用函数。分享给大家供大家参考。具体如下:# -*- coding: UTF-8 -*-'
- Tags# 普通for循环<ul>{% for user in user_list %} <li>{{
- 引言对 axios 二次封装,更加的可配置化、扩展性更加强大灵活通过 class 类实现,class 具备更强封装性(封装、继承、多态),通
- 一、类型1.变量没有类型,数据有类型例:num = 1 ---->num是没有类型的,1是int类型二、格式化输出2.na
- 本文主要介绍了Python3中PyQt5简单实现文件打开及保存,分享给大家,具体如下:# -*- coding: utf-8 -*-# Fo
- 最近想学习一些python数据分析的内容,就弄了个爬虫爬取了一些数据,并打算用Anaconda一套的工具(pandas, numpy, sc
- MySQL 数据表是由行和列构成的,通常把表的“列”称为字段(Field),把表的&
- 目录linux mysql5.5升级至mysql5.71.下载mysql5.7.322. 进入旧的mysql的bin目录下导出mysql的数
- 最近研究验证码识别,需要生成大量验证码,最方便的是使用captcha库来生成验证码,网上代码仅仅使用默认设置,但是它还有很多参数可以设定,于
- 1. 效果图自己画一张图,原图 VS 骨架效果图如下:opencv logo原图 VS 骨架化效果图如下:2. 源码# 图像骨架化~impo
- 在开发过程中我们需要将我们的数据通过图标的形式展现出来,接下来我为大家介绍一个有趣的框架:Echarts。这是一个使用JavaScript实
- 背景实现需求:批量下载联想某型号的全部驱动程序。一般在做网络爬虫的时候,都是保存网页信息为主,或者下载单个文件。当涉及到多文件批量下载的时候
- 实现效果示例代码import timefrom selenium import webdriverfrom selenium.webdriv
- 前言以前写过 vue项目中封装echarts的比较优雅的方式,大屏可视化里面,除了数据图表很常用,显示省市地图区域也是很常用到的,这是姐妹篇
- 一个朋友需要,所以写了这两个,话不多说,看代码中国电信号段 133、149、153、173、177、180、181、189、199中国联通号
- 最近的uniapp开发中遇到了H5调微信授权登录的业务,记录一下解决方法微信授权微信授权分为两种类型:静默授权:scope=snsapi_b
- python 数据的清理行为实例详解数据清洗主要是指填充缺失数据,消除噪声数据等操作,主要还是通过分析“脏数据”产生的原因和存在形式,利用现
- 简介前几天捣鼓了一下Ubuntu,正是想用一下我旧电脑上的N卡,可以用GPU来跑代码,体验一下多核的快乐。还好我这破电脑也是支持Cuda的:
- 本文实例讲述了Python设计模式之简单工厂模式。分享给大家供大家参考,具体如下:简单工厂模式(Simple Factory Pattern
- python打印当前文件的绝对路径并解决打印为空获取当前文件所在路径主要使用os.path.dirname(os.path.abspath(