85 down vote favorite

31

What explains the difference in behavior of boolean and bitwise operations on lists vs numpy.arrays?

I'm getting confused about the appropriate use of the '&' vs 'and' in python, illustrated in the following simple examples.

    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!

This answer, and this answer both helped me understand that 'and' is a boolean operation but '&' is a bitwise operation.

I was reading some information to better understand the concept of bitwise operations, but I am struggling to use that information to make sense of my above 4 examples.

Note, in my particular situation, my desired output is a newlist where:

    len(newlist) == len(mylist1)
newlist[i] == (mylist1[i] and mylist2[i]) #for every element of newlist

Example 4, above, led me to my desired output, so that is fine.

But I am left feeling confused about when/how/why I should use 'and' vs '&'. Why do lists and numpy arrays behave differently with these operators?

Can anyone help me understand the difference between boolean and bitwise operations to explain why they handle lists and numpy.arrays differently?

I just want to make sure I continue to use these operations correctly going forward. Thanks a lot for the help!

Numpy version 1.7.1

python 2.7

References all inline with text.

EDITS

1) Thanks @delnan for pointing out that in my original examples I had am ambiguity that was masking my deeper confusion. I have updated my examples to clarify my question.

asked Mar 25 '14 at 21:18
rysqui

9661919
  • 4
    Example 1 only appears to give the correct output. It actually just returns the second list unaltered. Try some other lists, in particular anything where the second list contains a True in a position that's False in the first list: Boolean logic dictates a False output at that position, but you'll get a True. – user395760 Mar 25 '14 at 21:22
  •  
    @delnan Thanks for noticing the ambiguity in my examples. I have updated my examples to highlight my confusion and focus on the aspect of this behavior that I do not understand. I'm clearly missing something important, because I did not expect the output of Example 1. – rysqui Mar 25 '14 at 21:37
  • 2
    In Numpy there's np.bitwise_and() and np.logical_and() and friends to avoid confusion. – Dietrich Mar 25 '14 at 21:54
  •  
    In example 1, mylist1 and mylist2 does not output the same result as mylist2 and mylist1, since what is being returned is the second list as pointed out by delnan. – user2015487 Feb 16 '16 at 17:58
  • 1
    Possible duplicate of Python: Boolean operators vs Bitwise operators – Oliver Ni Nov 6 '16 at 16:09

7 Answers

up vote 72 down vote accepted

and tests whether both expressions are logically True while & (when used with True/False values) tests if both are True.

In Python, empty built-in objects are typically treated as logically False while non-empty built-ins are logically True. This facilitates the common use case where you want to do something if a list is empty and something else if the list is not. Note that this means that the list [False] is logically True:

>>> if [False]:
... print 'True'
...
True

So in Example 1, the first list is non-empty and therefore logically True, so the truth value of the and is the same as that of the second list. (In our case, the second list is non-empty and therefore logically True, but identifying that would require an unnecessary step of calculation.)

For example 2, lists cannot meaningfully be combined in a bitwise fashion because they can contain arbitrary unlike elements. Things that can be combined bitwise include: Trues and Falses, integers.

NumPy objects, by contrast, support vectorized calculations. That is, they let you perform the same operations on multiple pieces of data.

Example 3 fails because NumPy arrays (of length > 1) have no truth value as this prevents vector-based logic confusion.

Example 4 is simply a vectorized bit and operation.

Bottom Line

  • If you are not dealing with arrays and are not performing math manipulations of integers, you probably want and.

  • If you have vectors of truth values that you wish to combine, use numpy with &.

关于panda中dataframe的与&运算*(stackoverflow高票答案)的更多相关文章

  1. Java中的Bigdecimal类型运算

    Java中的Bigdecimal类型运算 双精度浮点型变量double可以处理16位有效数.在实际应用中,需要对更大或者更小的数进行运算和处理.Java在java.math包中提 供的API类BigD ...

  2. 【转】Cocoa中的位与位运算

    转自:http://www.tuicool.com/articles/niEVjy 介绍 位操作是程序设计中对位模式或二进制数的一元和二元操作. 在许多古老的微处理器上, 位运算比加减运算略快, 通常 ...

  3. python中 and 和 or 运算的核心思想 ——— 短路逻辑

    python中 and 和 or 运算的核心思想 --- 短路逻辑 1. 包含一个逻辑运算符 首先从基本的概念着手,python中哪些对象会被当成 False 呢?而哪些又是 True 呢? 在Pyt ...

  4. Python语言中的按位运算

    (转)位操作是程序设计中对位模式或二进制数的一元和二元操作. 在许多古老的微处理器上, 位运算比加减运算略快, 通常位运算比乘除法运算要快很多. 在现代架构中, 情况并非如此:位运算的运算速度通常与加 ...

  5. pandas DataFrame(4)-向量化运算

    pandas DataFrame进行向量化运算时,是根据行和列的索引值进行计算的,而不是行和列的位置: 1. 行和列索引一致: import pandas as pd df1 = pd.DataFra ...

  6. java中多个数字运算后值不对(失真)处理方法

    最近遇到一个bug ,在java里面计算两个数字相减,633011.20-31296.30 得到的结果居然是601714.8999999999,丢失精度了,原来这是Java浮点运算的一个bug. 解决 ...

  7. js中多个数字运算后值不对(失真)处理方法

    最近遇到一个bug ,在js里面计算两个数字相减,633011.20-31296.30 得到的结果居然是601714.89,领导不乐意了说怎么少了0.01,我一听,噶卵达,来达鬼,不可能啊,我Goog ...

  8. python中实现三目运算

    python中没有其他语言中的三元表达式,不过有类似的实现方法 如: a = 1 b =2 k = 3 if a>b else 4 上面的代码就是python中实现三目运算的一个小demo, 如 ...

  9. Pandas中DataFrame修改列名

    Pandas中DataFrame修改列名:使用 rename df = pd.read_csv('I:/Papers/consumer/codeandpaper/TmallData/result01- ...

随机推荐

  1. J2EE基础总结(5)——EJB

    什么是EJB     JB事实上就是企业Java Beans. EJB是J2EE平台的重要组成部分. J2EE平台基于组件的企业级应用架构,提供多 层次.分布式和高事务的功能特点.     EJB提供 ...

  2. iOS常用的正则表达式总结

    /* 正则表达式说明: . 匹配除换行符以外的任意字符 \\w 匹配字母或数字或下划线或汉字 \\s 匹配任意的空白符 \\d 匹配数字 \\b 匹配单词的开始或结束 ^ 匹配字符串的开始 $ 匹配字 ...

  3. Vim 经常使用快捷键及键盘图

    Vim经常使用的快捷键 h - 光标左移一个字符   j - 光标下移一个字符 k - 光标上移一个字符   l - 光标右移一个字符  下移15行 - 15j Ctrl + f - 屏幕向下移动一页 ...

  4. Linux下查看history里的某种命令

    Linux下,直接键入history命令,会将当前账户此前所有的命令都显示出来,未免太多了些.如果我只想查找某种命令,怎么办? 比如说,我只想查找我之前运行过的 "git" 命令 ...

  5. BestCoder Round #61 (div.2) B.Game 细节题

    Game   问题描述 XY在玩一个游戏:有N根柱子排成一排,编号为1到N,每个柱子上面有一块宝石,现在XY站在第S根柱子上,出口在第T跟柱子上,XY需要拿到所有宝石后从出口离开.每次XY可以走到相邻 ...

  6. python实现自动重启本程序的方法 技术的漩涡

    python实现自动重启本程序的方法 http://www.jb51.net/article/69174.htm import requests, time url_l = []with open(' ...

  7. Fiddler抓取https请求,解决“证书错误”警告

    要抓取走HTTPS内容,Fiddler必须解密HTTPS流量. 但是,浏览器将会检查数字证书,并发现会话遭到窃听.为了骗过浏览 器,Fiddler通过使用另一个数字证书重新加密HTTPS流量. Fid ...

  8. POJ 1663:Number Steps

    Number Steps Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13758   Accepted: 7430 Des ...

  9. open_basedir restriction in effect,解决php引入文件权限问题 lnmp

    1.配置了虚拟域名 vim /usr/local/nginx/conf/vhost/siemens.conf server { listen 80; #listen [::]:80 default_s ...

  10. CF19 E Fairy——树上差分

    题目:http://codeforces.com/contest/19/problem/E 先把图连成一棵树,然后对于每条非树边,判断它是在奇环中还是偶环中: 把环上的点打上相应的差分标记,并记录有多 ...