如何根据 Python 中的一长串信息重新创建嵌套列表?
How do I recreate a nested list from a long string of information in Python?
我有一种算法可以使用程序生成的加密密钥将字符串加密为一长串数字。它的工作原理是一次加密一个词并将其放入嵌套列表中。
例如:
"4 3 1 4 5 5 2 4 6 2 3"
这将是两个单词,因为第一个数字是四,这意味着接下来的四个数字代表一个单词的四个字母。然后在这5个数字后面有一个5,意思是接下来的5个数字代表一个单词的5个字母。
不知道怎么转换:
"4 3 1 4 5 5 2 4 6 2 3"
进入嵌套列表:
[[3,1,4,5], [2,4,6,2,3] ]
我尝试了很多概念,但似乎无法弄清楚任何事情。有什么想法吗?
这里是加密代码,如果你需要的话:
import string
import random
def generateKey():
return(''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(2)))
def encrypt(message, encryptionKey = list(generateKey())):
print(''.join(encryptionKey))
message = message.split(' ')
array = []
z = 0
for word in message:
array.append([])
for word in message:
array[z].append(len(word)*ord(encryptionKey[0]))
for letter in word:
array[z].append(ord(letter)*ord(encryptionKey[1]))
z += 1
z = 0
for row in array:
for _set in row:
print(str(_set) + ' ', end='')
print('\n')
return(array) #Even though it returns a list, this data will be transferred from one person to another via one long string of text
假设您的加密函数格式正确,这应该会产生您请求的输出。基本上,它通过创建一个共享迭代器来工作,该迭代器将在子例程调用之间保留其位置。
def sentence(text):
iter_text = iter(text.split())
# split text on spaces and create a single iterator from it
def word(i_text, num_chars):
"""Helper function to return a list of the num_chars length
pulled out of the iterator i_text"""
return [next(i_text) for _ in range(num_chars)]
# [int(next(i_text)) ... ] for your literal output, but since you're
# encrypting as a string it seems more consistent to DECRYPT to a string
return [word(iter_text, int(ch)) for ch in iter_text]
示例:
>>> text = '4 3 1 4 5 5 2 4 6 2 3'
>>> result = sentence(text)
>>> print(result)
[['3', '1', '4', '5'], ['2', '4', '6', '2', '3']]
我有一种算法可以使用程序生成的加密密钥将字符串加密为一长串数字。它的工作原理是一次加密一个词并将其放入嵌套列表中。
例如:
"4 3 1 4 5 5 2 4 6 2 3"
这将是两个单词,因为第一个数字是四,这意味着接下来的四个数字代表一个单词的四个字母。然后在这5个数字后面有一个5,意思是接下来的5个数字代表一个单词的5个字母。
不知道怎么转换:
"4 3 1 4 5 5 2 4 6 2 3"
进入嵌套列表:
[[3,1,4,5], [2,4,6,2,3] ]
我尝试了很多概念,但似乎无法弄清楚任何事情。有什么想法吗?
这里是加密代码,如果你需要的话:
import string
import random
def generateKey():
return(''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(2)))
def encrypt(message, encryptionKey = list(generateKey())):
print(''.join(encryptionKey))
message = message.split(' ')
array = []
z = 0
for word in message:
array.append([])
for word in message:
array[z].append(len(word)*ord(encryptionKey[0]))
for letter in word:
array[z].append(ord(letter)*ord(encryptionKey[1]))
z += 1
z = 0
for row in array:
for _set in row:
print(str(_set) + ' ', end='')
print('\n')
return(array) #Even though it returns a list, this data will be transferred from one person to another via one long string of text
假设您的加密函数格式正确,这应该会产生您请求的输出。基本上,它通过创建一个共享迭代器来工作,该迭代器将在子例程调用之间保留其位置。
def sentence(text):
iter_text = iter(text.split())
# split text on spaces and create a single iterator from it
def word(i_text, num_chars):
"""Helper function to return a list of the num_chars length
pulled out of the iterator i_text"""
return [next(i_text) for _ in range(num_chars)]
# [int(next(i_text)) ... ] for your literal output, but since you're
# encrypting as a string it seems more consistent to DECRYPT to a string
return [word(iter_text, int(ch)) for ch in iter_text]
示例:
>>> text = '4 3 1 4 5 5 2 4 6 2 3'
>>> result = sentence(text)
>>> print(result)
[['3', '1', '4', '5'], ['2', '4', '6', '2', '3']]