我有以下功能:
def dict_to_binary(the_dict):
json_dict = json.dumps(the_dict)
binary = ' '.join(format(ord(letter), 'b') for letter in json_dict)
return binary
def binary_to_dict(the_binary):
jsn = ''.join(chr(int(x, 2)) for x in the_binary.split())
d = json.loads(jsn)
return d
这些函数接收dict对象,将其转换为json,然后使用ord
获取json
中每个项的unicode,然后将其转换为二进制。你知道吗
我如何编辑format
字符串来实现4位二进制表示,而不是它当前表示的8位二进制表示?完整代码如下:
inverted_index = {word:(document_frequency[word], d[word]) for word in d}
thebinary = dict_to_binary(inverted_index)
print(thebinary)
print()
print()
print()
convertedbinary = binary_to_dict(thebinary)
print(convertedbinary)
收益率
1111011 100010 1101100 1101111 1110010 1100101 1101101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110001 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101001 1110000 1110011 1110101 1101101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110001 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100100 1101111 1101100 1101111 1110010 100010 111010 100000 1011011 110010 101100 100000 1011011 1011011 110001 101100 100000 110001 1011101 101100 100000 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110011 1101001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110001 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100001 1101101 1100101 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110001 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100011 1101111 1101110 1110011 1100101 1100011 1110100 1100101 1110100 1110101 1110010 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100001 1100100 1101001 1110000 1101001 1110011 1100011 1101001 1101110 1100111 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1101100 1101001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110011 1100101 1100100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1101001 1110101 1110011 1101101 1101111 1100100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110100 1100101 1101101 1110000 1101111 1110010 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101001 1101110 1100011 1101001 1100100 1101001 1100100 1110101 1101110 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110101 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110011 1011101 101100 100000 1011011 110010 101100 100000 110011 1011101 101100 100000 1011011 110010 101100 100000 110011 1011101 1011101 1011101 101100 100000 100010 1101100 1100001 1100010 1101111 1110010 1100101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100100 1101111 1101100 1101111 1110010 1100101 100010 111010 100000 1011011 110010 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 101100 100000 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101101 1100001 1100111 1101110 1100001 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100001 1101100 1101001 1110001 1110101 1100001 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1101110 1101001 1101101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100001 1100100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101101 1101001 1101110 1101001 1101101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110110 1100101 1101110 1101001 1100001 1101101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110001 1110101 1101001 1110011 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101110 1101111 1110011 1110100 1110010 1110101 1100100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1111000 1100101 1110010 1100011 1101001 1110100 1100001 1110100 1101001 1101111 1101110 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110101 1101100 1101100 1100001 1101101 1100011 1101111 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101100 1100001 1100010 1101111 1110010 1101001 1110011 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101110 1101001 1110011 1101001 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100001 1101100 1101001 1110001 1110101 1101001 1110000 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1111000 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1100001 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100011 1101111 1101101 1101101 1101111 1100100 1101111 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100011 1101111 1101110 1110011 1100101 1110001 1110101 1100001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110010 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100100 1110101 1101001 1110011 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100001 1110101 1110100 1100101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101001 1110010 1110101 1110010 1100101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110010 1100101 1110000 1110010 1100101 1101000 1100101 1101110 1100100 1100101 1110010 1101001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110110 1101111 1101100 1110101 1110000 1110100 1100001 1110100 1100101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110110 1100101 1101100 1101001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1110011 1110011 1100101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100011 1101001 1101100 1101100 1110101 1101101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1110101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100110 1110101 1100111 1101001 1100001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101110 1110101 1101100 1101100 1100001 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110000 1100001 1110010 1101001 1100001 1110100 1110101 1110010 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110011 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1111000 1100011 1100101 1110000 1110100 1100101 1110101 1110010 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110011 1101001 1101110 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101111 1100011 1100011 1100001 1100101 1100011 1100001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100011 1110101 1110000 1101001 1100100 1100001 1110100 1100001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101110 1101111 1101110 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110000 1110010 1101111 1101001 1100100 1100101 1101110 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110011 1110101 1101110 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100011 1110101 1101100 1110000 1100001 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1110001 1110101 1101001 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101111 1100110 1100110 1101001 1100011 1101001 1100001 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100100 1100101 1110011 1100101 1110010 1110101 1101110 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101101 1101111 1101100 1101100 1101001 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100001 1101110 1101001 1101101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101001 1100100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1100101 1110011 1110100 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 101100 100000 100010 1101100 1100001 1100010 1101111 1110010 1110101 1101101 100010 111010 100000 1011011 110001 101100 100000 1011011 1011011 110100 101100 100000 110001 1011101 1011101 1011101 1111101
{'lorem': [1, [[1, 1]]], 'ipsum': [1, [[1, 1]]], 'dolor': [2, [[1, 1], [3, 1]]], 'sit': [1, [[1, 1]]], 'amet': [1, [[1, 1]]], 'consectetur': [1, [[2, 1]]], 'adipiscing': [1, [[2, 1]]], 'elit': [1, [[2, 1]]], 'sed': [1, [[2, 1]]], 'eiusmod': [1, [[2, 1]]], 'tempor': [1, [[2, 1]]], 'incididunt': [1, [[2, 1]]], 'ut': [1, [[2, 3], [2, 3], [2, 3]]], 'labore': [1, [[2, 1]]], 'et': [1, [[2, 1]]], 'dolore': [2, [[2, 1], [3, 1]]], 'magna': [1, [[2, 1]]], 'aliqua': [1, [[2, 1]]], 'enim': [1, [[2, 1]]], 'ad': [1, [[2, 1]]], 'minim': [1, [[2, 1]]], 'veniam': [1, [[2, 1]]], 'quis': [1, [[2, 1]]], 'nostrud': [1, [[2, 1]]], 'exercitation': [1, [[2, 1]]], 'ullamco': [1, [[2, 1]]], 'laboris': [1, [[2, 1]]], 'nisi': [1, [[2, 1]]], 'aliquip': [1, [[2, 1]]], 'ex': [1, [[2, 1]]], 'ea': [1, [[2, 1]]], 'commodo': [1, [[2, 1]]], 'consequat': [1, [[2, 1]]], 'duis': [1, [[3, 1]]], 'aute': [1, [[3, 1]]], 'irure': [1, [[3, 1]]], 'reprehenderit': [1, [[3, 1]]], 'voluptate': [1, [[3, 1]]], 'velit': [1, [[3, 1]]], 'esse': [1, [[3, 1]]], 'cillum': [1, [[3, 1]]], 'eu': [1, [[3, 1]]], 'fugiat': [1, [[3, 1]]], 'nulla': [1, [[3, 1]]], 'pariatur': [1, [[3, 1]]], 'excepteur': [1, [[4, 1]]], 'sint': [1, [[4, 1]]], 'occaecat': [1, [[4, 1]]], 'cupidatat': [1, [[4, 1]]], 'non': [1, [[4, 1]]], 'proident': [1, [[4, 1]]], 'sunt': [1, [[4, 1]]], 'culpa': [1, [[4, 1]]], 'qui': [1, [[4, 1]]], 'officia': [1, [[4, 1]]], 'deserunt': [1, [[4, 1]]], 'mollit': [1, [[4, 1]]], 'anim': [1, [[4, 1]]], 'id': [1, [[4, 1]]], 'est': [1, [[4, 1]]], 'laborum': [1, [[4, 1]]]}
您可以使用[Python 3.Docs]: base64 — Base16, Base32, Base64, Base85 Data Encodings(b16encode和b16decode将字符串转换为适合4位的chars)并结合一些字符串<;=>;int转换:
我没有把它们封装在函数中,这很简单。你知道吗
相关问题 更多 >
编程相关推荐