蟒蛇:检查清单是多维的,或一维 [英] python: check if list is multidimensional or one dimensional

查看:147
本文介绍了蟒蛇:检查清单是多维的,或一维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在编程Python和我创建了来自用户的输入列表的方法,不知道他究竟是多维的还是一维的。我该如何检查?
示例:

I am currently programing in python and I created a method that inputs list from the user, without knowing whether he is multidimensional or one dimensional. how do I check? sample:

def __init__(self,target):    
    for i in range(len(target[0])):
        w[i]=np.random.rand(len(example[0])+1)

目标列表中。问题在于目标[0]可能中间体

target is the list. the problem is that target[0] might be int.

推荐答案

我觉得你只是想 isinstance

实例:

>>> a = [1, 2, 3, 4]
>>> isinstance(a, list)
True
>>> isinstance(a[0], list)
False
>>> isinstance(a[0], int)
True
>>> b = [[1,2,3], [4, 5, 6], [7, 8, 9]]
>>> isinstance(b[0], list)
True

这篇关于蟒蛇:检查清单是多维的,或一维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆