python中‘和’(布尔)与‘&’(按位)之间的区别。为什么与列表和numpy数组的行为不同?
什么解释了列表和numpy.Array上的布尔操作和按位操作的行为差异?
&
and
mylist1 = [True, True, True, False, True] mylist2 = [False, True, False, True, False] >>> len(mylist1) == len(mylist2) True # ---- Example 1 ---- >>>mylist1 and mylist2 [False, True, False, True, False] #I am confused: I would have expected [False, True, False, False, False] # ---- Example 2 ---- >>>mylist1 & mylist2 *** TypeError: unsupported operand type(s) for &: 'list' and 'list' #I am confused: Why not just like example 1? # ---- Example 3 ---- >>>import numpy as np >>> np.array(mylist1) and np.array(mylist2) *** ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() #I am confused: Why not just like Example 4? # ---- Example 4 ---- >>> np.array(mylist1) & np.array(mylist2) array([False, True, False, False, False], dtype=bool) #This is the output I was expecting!
len(newlist) == len(mylist1) newlist[i] == (mylist1[i] and mylist2[i]) #for every element of newlist
有谁能帮助我理解布尔操作和按位操作之间的区别,来解释为什么它们处理列表和numpy.数组的方式不同呢?
Numpy version 1.7.1python 2.7References all inline with text.
FFIVE
宝慕林4294392
杨__羊羊
相关分类