讓我們用二進制的a=109
或1101101
。如何遍歷這個數位,如:[64, 32, 8, 4, 1]
用迭代遍歷整數位的Python方法
回答
我的方法:
def bits(number):
bit = 1
while number >= bit:
if number & bit:
yield bit
bit <<= 1
我不認爲這是它內置函數。
我還想知道是否沒有更好的方法來處理你正在做的事情。有一個很好的機會,你不會真的想迭代這樣的位。他們可能是一個更好的方法。
出於好奇,我跑的方法的一些時間貼在這裏,我的結果:
Winston 2.35238099098
F.J. 6.21106815338
F.J. (2) 5.21456193924
Sven 2.90593099594
Duncan 2.33568000793
freegnu 4.67035484314
F.J.轉換爲字符串,我猜,會傷害他的表現。各種優化嘗試都有所幫助,但是Sven並沒有產生與其他人相反的結果,如果你真的需要這樣做,這可能是一個優勢。鄧肯的做法贏得speedwise(勉強)
再次340282366920938463463374607431768211457而不是109:
Winston 44.5073108673
F.J. 74.7332041264
Sven 47.6416211128
Duncan 2.58612513542
不錯,鄧肯!應該指出的是,這幾乎是鄧肯方法的最好的例子,所以它不會總是有這個巨大的優勢。
的Python 2.7:
def binary_decomposition(x):
p = 2 ** (int(x).bit_length() - 1)
while p:
if p & x:
yield p
p //= 2
例子:
>>> list(binary_decomposition(109))
[64, 32, 8, 4, 1]
>>> [2**i for i, v in enumerate(bin(109)[:1:-1]) if int(v)]
[1, 4, 8, 32, 64]
顯然,爲了在此相反,你既可以只使用這或反向結果:
>>> [2**i for i, v in enumerate(bin(109)[:1:-1]) if int(v)][::-1]
[64, 32, 8, 4, 1]
編輯:這裏有一個稍微長一點的版本,應該是更高效:
from itertools import takewhile, count
[p for p in takewhile(lambda x: x <= 109, (2**i for i in count())) if p & 109]
+1 oneliner – 2012-01-17 17:59:46
字符串轉換比最壞情況下的迭代器版本更快340282366920938463463374607431768211457和可能超過一定大小的任何東西。我將你的if(int)()測試改爲一個字符串test =='1'來加快速度。 @鄧肯仍然能夠輕鬆擊敗它。 – freegnu 2012-01-17 19:33:03
FJ答案的效率可以顯着提高。
from itertools import count,takewhile
[2**i for i in takewhile(lambda x:109>2**x,count()) if 109&2**i][::-1]
我喜歡一個襯墊:)
我做了一個快速timeit.Timer.timeit()有違反本和@Duncan。鄧肯依然贏得勝利,但至少在同一班級中不是單線。
from timeit import Timer
duncan="""\
def bits(n):
while n:
b=n&(~n+1)
yield b
n^=b
"""
Duncan=Timer('list(bits(109))[::-1]',duncan)
Duncan.timeit()
4.3226630687713623
freegnu=Timer('[2**i for i in takewhile(lambda x:109>2**x,count()) if 109&2**i][::-1]','from itertools import count,takewhile')
freegnu.timeit()
5.2898638248443604
實施例單行溶液:
[1 << bit for bit in xrange(bitfield.bit_length()) if bitfield & (1 << bit)]
或者:
[bit for bit in (1 << n for n in xrange(bitfield.bit_length())) if bitfield & bit]
注:
- 使用範圍在Python 3
- 想檢查bitfie ld> = 0
S.O.不會讓我把這個作爲評論,但這裏是Duncan's解決方案如何工作的逐行示例。希望這可以澄清發生了什麼。
讓我們使用十進制數109作爲一個例子:
# 109 is .............. 0110 1101
# ~109 is -110 which is 1001 0010 NOTE: It's -110 instead of -109 because of 1's compliment
# ~109+1 is -109....... 1001 0011
# 109 AND ~109 is...... 0000 0001 = 1 <---- 1st value yielded by the generator
# 109 XOR 1 is......... 0110 1100 = n = 108
# 108.................. 0110 1100
# ~108+1= -108......... 1001 0100
# 108 AND -108......... 0000 0100 = 4 <---- 2nd value yielded by the generator
# 108 XOR 4............ 0110 1000 = n = 104
# 104.................. 0110 1000
# ~104+1............... 1001 1000
# 104 AND -104......... 0000 1000 = 8 <---- 3rd value yielded by the generator
# 104 XOR 8............ 0110 0000 = n = 96
# 96................... 0110 0000
# ~96+1................ 1010 0000
# 96 AND -96........... 0010 0000 = 32 <---- 4th value yielded by the generator
# 96 XOR 32............ 0100 0000 = n = 64
# 64................... 0100 0000
# ~64+1................ 1100 0000
# 64 AND -64........... 0100 0000 = 64 <---- 5th value yielded by the generator
# 64 XOR 64............ 0000 0000 = n = 0; thus, the while loop terminates.
- 1. 遍歷3D數組的迭代方法
- 2. 對樹的遍歷迭代方法
- 3. 迭代Inorder遍歷
- 4. DFS遍歷迭代
- 5. 迭代遍歷所有行Smartsheet API Python
- 6. Python Django模板:迭代遍歷列表
- 7. 迭代器中迭代遍歷控件
- 8. 迭代遍歷類Angular4
- 9. libxmlrpc迭代遍歷struct
- 10. 迭代後序遍歷bst?
- 11. 迭代八叉樹遍歷
- 12. itertools.groupby:迭代遍歷組pairwise
- 13. C++迭代器遍歷
- 14. 迭代DFS如何遍歷?
- 15. PHP可迭代到數組或遍歷
- 16. 什麼是反向迭代遍歷Iterator的最快方法
- 17. 在Python3中迭代遍歷文件的行時使用「tell()」的替代方法?
- 18. 使用迭代器遍歷列表?
- 19. 使用迭代器遍歷結果集?
- 20. LinkedList時間遍歷的迭代器
- 21. 通過類的迭代遍歷對象
- 22. c中的迭代目錄遍歷
- 23. Python:二叉樹遍歷迭代器不使用條件
- 24. lisp循環遍歷整數位的方式
- 25. 遍歷python中的單值和迭代的組合
- 26. 通過迭代遍歷遍歷一個向量內的兩個整數的比較集合
- 27. 迭代遍歷不同的python文件作爲變量
- 28. 樹遍歷,遞歸比python中的迭代更快嗎?
- 29. 遍歷GridViewRow方法
- 30. 向量迭代器沒有循環遍歷整個向量
http://wiki.python.org/moin/BitwiseOperators應該足夠 – 2012-01-17 17:15:46
如果你正在尋找轉換,你可以使用數字'bin(109)'或'int('1101101',2)'。 – 2012-01-17 17:20:38
'map(lambda x:int(x),bin(NUMBER)[2:])'。但是這個解決方案可能太簡單而且不太合理。 ;) – Gandaro 2012-01-17 17:28:14