![](/img/trans.png)
[英]Most pythonic/stylish/efficient way to create a dataframe from 2-dimensional list of string with varied length
[英]Most pythonic way to check if a 1-dimensional list is an element of a 2-dimensional list?
使用Python 2.7.6,我有一个rgb颜色的列表,每种颜色都有一个列表,即:
color_list = [ [0, 0, 0], [255, 0, 0]....[255, 255, 255] ]
调用此:
color = [0, 0, 0]
if color in color_list:
# do stuff
要求:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我担心会执行错误提示,即。 color.any()
或color.all()
会在我的颜色列表中的任何地方查找那些整数。 我可以想出达到我实际目标的方法,但是我的直觉是Python很好地意识到了这种需求的到来,并且有一种Python的方式可以实现它。 会帮忙吗?
我失败了 上面代码中的color
是numpy.ndarray
您看到的错误消息来自numpy
。
这意味着color
是numpy数组,或者color_list
是,或者color_list
是numpy数组的列表。 如果全部都是列表,那么您的代码就可以了。
color_list = [ [0, 0, 0], [255, 0, 0], [255, 255, 255] ]
color = [0, 0, 0]
color in color_list
=> True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.