ホームページ  >  記事  >  バックエンド開発  >  Pythonでデータ型を判断する方法

Pythonでデータ型を判断する方法

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼オリジナル
2019-08-01 15:28:5942316ブラウズ

Pythonでデータ型を判断する方法

Python のデータ型には、数値 (int)、浮動小数点 (float)、文字列 (str)、リスト (list)、タプル (tuple)、辞書 (dict) が含まれます。 。

一般的には以下の方法で判定されます。

1, isinstance (パラメータ 1, パラメータ 2)

説明: この関数は、インスタンスを判定するために使用されます。変数 (パラメータ 1) 既知の変数タイプかどうか (パラメータ 2) type() と同様

#パラメータ 1: 変数

パラメータ 2: 直接または間接のクラス名にすることができます。基本型、またはそれらから構成されるタプル。

戻り値: オブジェクトの型がパラメーター 2 (classinfo) の型と同じである場合は True を返し、それ以外の場合は False を返します。

関連する推奨事項:「Python ビデオ チュートリアル

例:

#判断变量类型的函数
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)))

Return:

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)))

Return:

Pythonでデータ型を判断する方法

isinstance() と type() の違い:

type() は、サブクラスを親クラス型とはみなさず、継承関係も考慮しません。

isinstance() は、サブクラスを親クラス型とみなし、継承関係を考慮します。

2 つの型が同じかどうかを確認したい場合は、isinstance() を使用することをお勧めします。

以上がPythonでデータ型を判断する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。