के बराबर पाइथन क्या कोई पाइथन क्लास या मॉड्यूल है जो बिटसेट के समान संरचना को लागू करता है?जावा के बिटसेट
22
A
उत्तर
13
मानक पुस्तकालय में कुछ भी नहीं है। प्रयास करें:
6
अजगर में इस implementation पर एक नज़र डालें 3.
कार्यान्वयन मूल रूप से का उपयोग करता है में निर्मित int
प्रकार है, जो अजगर 3 (जहां long
में मनमाने ढंग से सटीक पूर्णांक प्रकार है पाइथन 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
आप एक मॉड्यूल मैं bitstring बुलाया लिखा (पूर्ण प्रलेखन here) पर एक नज़र लेने के लिए हालांकि साधारण मामलों यथासंभव शीघ्र होने की जरूरत है कि के लिए मैं अभी भी bitarray की सलाह देते हैं पसंद कर सकते हैं।
कुछ इसी तरह के सवाल:
What is the best way to do Bit Field manipulation in Python?
संबंधित मुद्दे
- 1. जावा बिटसेट उदाहरण
- 2. जावा बिटसेट को डीबी
- 3. परिवर्तनीय आकार बिटसेट
- 4. जावा 'बिटसेट' में `shiftLeft` और` shiftRight` फ़ंक्शन क्यों नहीं हैं?
- 5. थोड़ा वेक्टर (बिटसेट) (जावा में) को कैसे कार्यान्वित करें?
- 6. प्रदर्शन के समान सरणी के साथ जावा बिटसेट के लिए वैकल्पिक?
- 7. बिट के साथ बिटसेट को कैसे कार्यान्वित करें?
- 8. प्रारंभ में बिटसेट आकार को परिभाषित करें?
- 9. स्कैला म्यूटेबल बिटसेट, उत्परिवर्ती संचालन कहां हैं?
- 10. यादृच्छिक रूप से जावा
- 11. जावा
- 12. बूलियन [] बनाम बिटसेट: कौन सा अधिक कुशल है?
- 13. एसटीएल बिटसेट :: गिनती() विधि का प्रदर्शन क्या है?
- 14. अधिकतम प्रदर्शन के लिए मुझे किस बिटसेट कार्यान्वयन का उपयोग करना चाहिए?
- 15. बिटसेट को एक स्पष्ट कलाकार को सेट [Int] के उदाहरण के रूप में क्यों माना जाता है?
- 16. जावा: कुशलतापूर्वक बुलियन स्टोर [32]?
- 17. जावा के बिना जावा
- 18. जावा ओएस के तहत जावा
- 19. जावा के बराबर जावा .Net's NotSupportedException
- 20. जावा के साथ जावा नियमित अभिव्यक्ति
- 21. जावा वीएस समाधान फ़ाइल के बराबर जावा
- 22. जावा और जावा के बीच अंतर
- 23. जावा/जावा ईई साक्षात्कार के प्रबंधन दौर
- 24. जावा वेब साइट HTML के साथ जावा
- 25. जावा में बहुत कॉम्पैक्ट बिटर्रे
- 26. Clojure/जावा: ध्वनि के स्पेक्ट्रम विश्लेषण के लिए जावा लाइब्रेरीज?
- 27. स्पष्ट() जावा के LinkedList
- 28. हैशटेबल के जावा सरणी
- 29. जावा के लिए ऑटोमैपर
- 30. क्या जावास्क्रिप्ट जावा के
गैर जावा प्रोग्रामर के लिए, एक BitSet क्या है? आप क्या खत्म करने की कोशिश कर रहे हैं? भले ही कोई समकक्ष मौजूद न हो, फिर भी आपकी समस्या को हल करने का एक अच्छा, पाइथोनिक तरीका हो सकता है। –
असल में एक बिटसेट बिट बिट्स ऑपरेशंस के साथ बिट्स की एक स्ट्रिंग है जिसे उन पर लागू किया जा सकता है। वर्तमान बिटसेट के अंत में बिट्स या बिट्स को जोड़कर सेट को भी बढ़ाया जा सकता है –