Czy istnieje klasa lub moduł Pythona, który implementuje strukturę podobną do BitSet?Python odpowiednik Java's BitSet
22
A
Odpowiedz
13
W standardowej bibliotece nie ma nic. Spróbuj:
6
Zapraszamy do obejrzenia tej implementation w Pythonie 3.
Realizacja zasadzie wykorzystuje wbudowany int
typu, która jest arbitralna precyzja typ całkowitą w Pythonie 3 (gdzie long
jest odpowiednikiem Pythona 2).
#! /usr/bin/env python3
"""
bitset.py
Written by Geremy Condra
Licensed under GPLv3
Released 3 May 2009
This module provides a simple bitset implementation
for Python.
"""
from collections import Sequence
import math
class Bitset(Sequence):
"""A very simple bitset implementation for Python.
Note that, like with normal numbers, the leftmost
index is the MSB, and like normal sequences, that
is 0.
Usage:
>>> b = Bitset(5)
>>> b
Bitset(101)
>>> b[:]
[True, False, True]
>>> b[0] = False
>>> b
Bitset(001)
>>> b << 1
Bitset(010)
>>> b >> 1
Bitset(000)
>>> b & 1
Bitset(001)
>>> b | 2
Bitset(011)
>>> b^6
Bitset(111)
>>> ~b
Bitset(110)
"""
value = 0
length = 0
@classmethod
def from_sequence(cls, seq):
"""Iterates over the sequence to produce a new Bitset.
As in integers, the 0 position represents the LSB.
"""
n = 0
for index, value in enumerate(reversed(seq)):
n += 2**index * bool(int(value))
b = Bitset(n)
return b
def __init__(self, value=0, length=0):
"""Creates a Bitset with the given integer value."""
self.value = value
try: self.length = length or math.floor(math.log(value, 2)) + 1
except Exception: self.length = 0
def __and__(self, other):
b = Bitset(self.value & int(other))
b.length = max((self.length, b.length))
return b
def __or__(self, other):
b = Bitset(self.value | int(other))
b.length = max((self.length, b.length))
return b
def __invert__(self):
b = Bitset(~self.value)
b.length = max((self.length, b.length))
return b
def __xor__(self, value):
b = Bitset(self.value^int(value))
b.length = max((self.length, b.length))
return b
def __lshift__(self, value):
b = Bitset(self.value << int(value))
b.length = max((self.length, b.length))
return b
def __rshift__(self, value):
b = Bitset(self.value >> int(value))
b.length = max((self.length, b.length))
return b
def __eq__(self, other):
try:
return self.value == other.value
except Exception:
return self.value == other
def __int__(self):
return self.value
def __str__(self):
s = ""
for i in self[:]:
s += "1" if i else "0"
return s
def __repr__(self):
return "Bitset(%s)" % str(self)
def __getitem__(self, s):
"""Gets the specified position.
Like normal integers, 0 represents the MSB.
"""
try:
start, stop, step = s.indices(len(self))
results = []
for position in range(start, stop, step):
pos = len(self) - position - 1
results.append(bool(self.value & (1 << pos)))
return results
except:
pos = len(self) - s - 1
return bool(self.value & (1 << pos))
def __setitem__(self, s, value):
"""Sets the specified position/s to value.
Like normal integers, 0 represents the MSB.
"""
try:
start, stop, step = s.indices(len(self))
for position in range(start, stop, step):
pos = len(self) - position - 1
if value: self.value |= (1 << pos)
else: self.value &= ~(1 << pos)
maximum_position = max((start + 1, stop, len(self)))
self.length = maximum_position
except:
pos = len(self) - s - 1
if value: self.value |= (1 << pos)
else: self.value &= ~(1 << pos)
if len(self) < pos: self.length = pos
return self
def __iter__(self):
"""Iterates over the values in the bitset."""
for i in self[:]:
yield i
def __len__(self):
"""Returns the length of the bitset."""
return self.length
4
Może chcesz przyjrzeć moduł napisałem o nazwie bitstring (pełna dokumentacja here), choć do prostych spraw, które muszą być jak najszybciej chciałbym jeszcze polecić bitarray.
Niektóre podobne pytania:
What is the best way to do Bit Field manipulation in Python?
Powiązane problemy
- 1. Jak przekonwertować ciąg na bitset?
- 2. python - odpowiednik strtotime?
- 3. Python odpowiednik perl -pe?
- 4. Python odpowiednik Jstack?
- 5. python getoutput() odpowiednik podprocesu
- 6. python: Windows odpowiednik SIGALRM
- 7. Python odpowiednik atoi/atof
- 8. Bitset o zmiennym rozmiarze
- 9. bitowe pole vs Bitset
- 10. Konwersja BitSet int
- 11. pomocą bitset pojemnik C++
- 12. Python: Jaki jest odpowiednik PHP "=="
- 13. Python odpowiednik Java to compareTo()
- 14. Odpowiednik JavaBean w języku Python
- 15. Python dir() odpowiednik w JavaScript?
- 16. coffeescript odpowiednik python „pass” oświadczenie
- 17. Czy Python ma odpowiednik "przełącznika"?
- 18. Python evdev odpowiednik dla OSX
- 19. Losowo zainicjować BitSet w JAVA
- 20. Zapisywanie java BitSet na DB
- 21. jak znaleźć i zwrócić obiekty w javas hashset
- 22. Co to jest odpowiednik "gem" w python?
- 23. Czy istnieje odpowiednik Python dla HighLine?
- 24. Czy istnieje odpowiednik InnerText w BeautifulSoup/python?
- 25. Czy istnieje odpowiednik javascript metody __getattr__ python?
- 26. Python OpenCV CV2 odpowiednik dla CV_FILLED
- 27. Python: signal.pause() odpowiednik w systemie Windows
- 28. Python odpowiednik stałej magicznej PHP __DIR__?
- 29. Czy python ma odpowiednik "btoa" Javascripta?
- 30. Python: Java rzuca odpowiednik w Pythonie
Dla programistów spoza Java, co jest BitSet? Co próbujesz osiągnąć? Nawet jeśli nie ma odpowiednika, może istnieć dobry, pytonowy sposób rozwiązania problemu. –
Zasadniczo BitSet to ciąg bitów z operacjami bitowymi, które można do nich zastosować. Zestaw można również rozszerzyć, dołączając bity lub zestawy bitów na koniec bieżącego zestawu bitowego –