2016-08-29 72 views
1

我有一個64位的列表。解析python中的位列表

['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.

+2

你能舉一個例子輸入你想要得到它的結果呢?它會幫助我們幫助你。 – Mureinik

+0

你想要返回什麼,該項目的索引? – GoatsWearHats

+0

@KaasiasKomplex項目本身,我應該返回 – taz

回答

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) - 因爲我們希望能夠將列表中的所有項目加入到我們可以通過的一個可迭代對象中。
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 
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)) 

但是,您可以輕鬆地調整這種方法來匹配其他枚舉。

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) 

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

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'] 
""" 
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