📌  相关文章
📜  Python|检查给定对象是否是列表

📅  最后修改于: 2022-05-13 01:54:20.809000             🧑  作者: Mango

Python|检查给定对象是否是列表

给定一个对象,任务是检查该对象是否是列表。

方法 #1:使用isinstance

# Python code to demonstrate
# check whether the object 
# is a list or not
  
# initialisation list
ini_list1 = [1, 2, 3, 4, 5]
ini_list2 = '12345'
  
# code to check whether
# object is a list or not
if isinstance(ini_list1, list):
  print("your object is a list !")
else:
    print("your object is not a list")
    
if isinstance(ini_list2, list):
    print("your object is a list")
else:
    print("your object is not a list")
输出:
your object is a list !
your object is not a list


方法 #2:使用type(x)

# Python code to demonstrate
# check whether object 
# is a list or not
  
# initialisation list
ini_list1 = [1, 2, 3, 4, 5]
ini_list2 = (12, 22, 33)
  
# code to check whether
# object is a list or not
if type(ini_list1) is list:
    print("your object is a list")
else:
    print("your object is not a list")
  
if type(ini_list2) is list:
    print("your object is a list")
else:
    print("your object is not a list")
输出:
your object is a list
your object is not a list