日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線(xiàn)溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:python如何判斷數(shù)據(jù)類(lèi)型是不是字典

python的數(shù)據(jù)類(lèi)型有:數(shù)字(int)、浮點(diǎn)(float)、字符串(str),列表(list)、元組(tuple)、字典(dict)、集合(set)。

10多年的延壽網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷(xiāo)的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整延壽建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“延壽網(wǎng)站設(shè)計(jì)”,“延壽網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

python學(xué)習(xí)網(wǎng),大量的免費(fèi)python視頻教程,歡迎在線(xiàn)學(xué)習(xí)!

一般通過(guò)以下方法進(jìn)行判斷:

1、isinstance(參數(shù)1,參數(shù)2)

描述:該函數(shù)用來(lái)判斷一個(gè)變量(參數(shù)1)是否是已知的變量類(lèi)型(參數(shù)2) 類(lèi)似于type()

參數(shù)1:變量

參數(shù)2:可以是直接或間接類(lèi)名、基本類(lèi)型或者由它們組成的元組。

返回值: 如果對(duì)象的類(lèi)型與參數(shù)二的類(lèi)型(classinfo)相同則返回True,否則返回False。

例子:

#判斷變量類(lèi)型的函數(shù)
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
# 返回變量類(lèi)型
def getType(variate):
    arr = {"int":"整數(shù)","float":"浮點(diǎn)","str":"字符串","list":"列表","tuple":"元組","dict":"字典","set":"集合"}
    vartype = typeof(variate)
    if not (vartype in arr):
        return "未知類(lèi)型"
    return arr[vartype]
     
#判斷變量是否為整數(shù)
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、通過(guò)與已知類(lèi)型的常量進(jìn)行比較

例子:

#判斷變量類(lèi)型的函數(shù)
  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
#返回變量類(lèi)型
  def getType(variate):
    arr = {"int":"整數(shù)","float":"浮點(diǎn)","str":"字符串","list":"列表","tuple":"元組","dict":"字典","set":"集合"}
    vartype = typeof(variate)
    if not (vartype in arr):
        return "未知類(lèi)型"
    return arr[vartype]
     
#判斷變量是否為整數(shù)
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() 區(qū)別:

type() 不會(huì)認(rèn)為子類(lèi)是一種父類(lèi)類(lèi)型,不考慮繼承關(guān)系。

isinstance() 會(huì)認(rèn)為子類(lèi)是一種父類(lèi)類(lèi)型,考慮繼承關(guān)系。

如果要判斷兩個(gè)類(lèi)型是否相同推薦使用 isinstance()。

python學(xué)習(xí)網(wǎng),免費(fèi)的在線(xiàn)學(xué)習(xí)python平臺(tái),歡迎關(guān)注!


網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:python如何判斷數(shù)據(jù)類(lèi)型是不是字典
文章分享:http://www.dlmjj.cn/article/dhdgpep.html