解析python中的位列表

时间:2016-08-29 08:25:53

标签: python python-2.7

我有一个64位的列表。

['11111111', '11111111', '10001111', '11111110', '11011011', '11111111', '01011011', '10000111']

这里每个位代表一个特征。如果位为1,则支持该特定功能,否则不支持。

如果它是1,我有什么方法可以检查该位并返回其相关的功能。

Input : 1111111

Output: If all bits are one then I need to print all eight features masked in it.

6 个答案:

答案 0 :(得分:0)

您可以将每个要素的变量中的字符串解包为:

>>> my_bit = '11111110'
>>> feat_1, feat_2, feat_3, feat_4, feat_5, feat_6, feat_7, feat_8 = my_bit
>>> print feat_1, feat_2, feat_3, feat_4, feat_5, feat_6, feat_7, feat_8
1 1 1 1 1 1 1 0

现在,您可以将每个功能检查为:

if feat_1:
     # Do something
else:
     # Do something else

答案 1 :(得分:0)

我猜你可以这样做:

bits = ['11111111', '11111111', '10001111', '11111110', '11011011', '11111111', '01011011', '10000111']

# to produce features mapping dynamically:
# features = {i:"feature " + str(i) for i in range(0,64)}
features = {0: 'feature 0', 1: 'feature 1', 2: 'feature 2', 3: 'feature 3', 4: 'feature 4', 5: 'feature 5', 6: 'feature 6', 7: 'feature 7', 8: 'feature 8', 9: 'feature 9', 10: 'feature 10', 11: 'feature 11', 12: 'feature 12', 13: 'feature 13', 14: 'feature 14', 15: 'feature 15', 16: 'feature 16', 17: 'feature 17', 18: 'feature 18', 19: 'feature 19', 20: 'feature 20', 21: 'feature 21', 22: 'feature 22', 23: 'feature 23', 24: 'feature 24', 25: 'feature 25', 26: 'feature 26', 27: 'feature 27', 28: 'feature 28', 29: 'feature 29', 30: 'feature 30', 31: 'feature 31', 32: 'feature 32', 33: 'feature 33', 34: 'feature 34', 35: 'feature 35', 36: 'feature 36', 37: 'feature 37', 38: 'feature 38', 39: 'feature 39', 40: 'feature 40', 41: 'feature 41', 42: 'feature 42', 43: 'feature 43', 44: 'feature 44', 45: 'feature 45', 46: 'feature 46', 47: 'feature 47', 48: 'feature 48', 49: 'feature 49', 50: 'feature 50', 51: 'feature 51', 52: 'feature 52', 53: 'feature 53', 54: 'feature 54', 55: 'feature 55', 56: 'feature 56', 57: 'feature 57', 58: 'feature 58', 59: 'feature 59', 60: 'feature 60', 61: 'feature 61', 62: 'feature 62', 63: 'feature 63'}

for ind, bit in enumerate(reversed("".join(bits))):
    if bit == '1':
        print "Feature " + features[ind] + " turned on"

使用地点:

  • reversed - 因为从LSB到MSB读取位,所以我们需要从右向左迭代。
  • enumerate - 因为我们想要获取相关索引来检查映射中的功能名称/属性是什么。
  • "".join(bits) - 因为我们希望能够将列表中的所有项目加入到我们可以通过的一个可迭代对象中。

答案 2 :(得分:0)

解决方案取决于您枚举功能的方式。如果你枚举从0到63的功能,它将是这样的:

import math

testdata = ['11111111', '11111111', '10001111', '11111110', '11011011', '11111111', '01011011', '10000111']

def test_feature(data, i):
        block = math.floor(i/8)
        pos = i % 8

        if data[block][pos] == '1':
                return True

        return False

print(testdata)
print(test_feature(testdata, 0))
print(test_feature(testdata, 3))
print(test_feature(testdata, 8))
print(test_feature(testdata, 17))
print(test_feature(testdata, 33))
print(test_feature(testdata, 63))

但是,您可以轻松调整此方法以匹配其他枚举。

答案 3 :(得分:0)

您可以先制作功能列表。

feat_list = ['f1','f2','f3','f4','f6','f7','f8']

bit_list = ['11111111', '11111111', '10001111', '11111110', '11011011', '11111111', '01011011', '10000111']

然后做一个功能。

def extract_feature(bit_string):
    result = []
    for ind, bit in enumerate(bit_string):
        if bit == '1':
            result.append(feat_list[ind])
    return result

然后使用循环来获取每个位串的功能。

for bit_string in bit_list:
    print extract_feature(bit_string)

请注意,它不是很有效,但它是冗长的,以便新手可以理解。

答案 4 :(得分:0)

我不知道我是否完全理解:)

def extract_features(x, f):
    return [f[i] for i, elem in enumerate(x) if x[i] == '1']


features = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
x = '11111111'    
y = '10101010'

print (extract_features(x, features))
print (extract_features(y, features))

"""
<Output>
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
['a', 'c', 'e', 'g']
"""

答案 5 :(得分:0)

def f0(): print "executing feature0"
def f1(): print "executing feature1"
def f2(): print "executing feature2"
def f3(): print "executing feature3"
def f4(): print "executing feature4"
def f5(): print "executing feature5"
def f6(): print "executing feature6"
def f7(): print "executing feature7"


def run_features(mask):
    for index, bit in (enumerate(mask[::-1])):
        if bit == '1':
            features[index]()

bits = [
    '11111111', '11111111', '10001111', '11111110',
    '11011011', '11111111', '01011011', '10000111'
]

features = [
    f0, f1, f2, f3, f4, f5, f6, f7
]

for mask in bits:
    run_features(mask)
    print '-' * 80