【问题标题】:Change character based off of its position? Python 2.7根据角色的位置改变角色?蟒蛇 2.7
【发布时间】:2016-09-08 01:47:56
【问题描述】:

我有一个长度未知的字符串,包含字符 a-z A-Z 0-9。我需要使用字典从左到右更改每个字符的位置。

例子:

string = "aaaaaaaa"
def shift_char(text):
    for i in len(text):
        # Do Something for each character
    return output
print shift_char(string)
'adktivep'

【问题讨论】:

  • 嘿@Austin,你怎么描述,哪个位置的哪个字符应该转换成哪个字符?
  • @TonyMontana 将在我的程序中设置。这个想法是为每个职位都有一本字典。所以如果'f'是'3'个字符,在'dict3'中寻找'f'。我将使用它来帮助编写我为代码编写的加密概念。是的,我读过为什么we shouldn't roll our own encryption。但这只是目前的个人项目。
  • @TonyMontana 当然,这只是我对它如何工作的想法。如果您有更好的想法,请随时说出来。
  • 词典是如何制作的?它们是随机的吗?还是他们被转移了? (就像一个凯撒密码)。
  • @bobsterman 它们是静态的,但在我编写代码后将使用脚本随机生成。

标签: python python-2.7


【解决方案1】:

好的,希望这是可以理解的,否则请随时提出问题。

import random
import string

# Letter pool, these are all the possible characters
# you can have in your string
letter_pool = list(string.ascii_letters + string.digits)

input_string = "HelloWorld"
string_length = len(input_string)

# Generate one random dictionary for each character in string
dictionaries = []

for i in range(string_length):
    # Copy letter_pool to avoid overwriting letter_pool
    keys = list(letter_pool)
    values = list(letter_pool)
    # Randomise values, (keep keys the same)
    random.shuffle(values)

    # This line converts two lists into a dictionary
    scrambled_dict = dict(zip(keys, values))

    # Now each letter (key) maps to a random letter (value)
    dictionaries.append(scrambled_dict)

# Initiate a fresh string to start adding characters to
out_string = ""

# Loop though the loop string, record the current place (index) and character each loop
for index, char in enumerate(input_string):
    # Get the dictionary for this place in the string
    dictionary = dictionaries[i]
    # Get the randomised character from the dictionary
    new_char = dictionary[char]
    # Place the randomised character at the end of the string
    out_string += new_char

# Print out the random string
print(out_string)

编辑:如果您只想生成一次随机字典,并且每次都加载它们,您可以序列化 dictionaries 数组。我最喜欢的是json

【讨论】:

  • 好的,我查看了您的代码,并对其进行了测试。我得到的输出是完全随机的。不知道是我做错了,还是你错过了理解。但无论哪种方式,这都不是我想要的。我需要能够以可预测的方式更改字符,以便我还可以反转最终代码不同部分的更改。也许使用种子来生成字典?
  • 好的,经过进一步的测试,我得出结论,这不是我想要的。我用input_string = "ttttttttttt" 替换了input_string = "HelloWorld",在控制台中我得到了结果“vvvvvvvvvvv”。你看,我试图打乱重复的字符,使它们不再匹配。但是以这样一种方式,我可以在以后扭转结果。可以说input_string = "test"。在“测试”中,t=1 e=2 s=3 t=4 以此类推。我需要代码说,“好的,'t' 在插槽'1',所以应该说'Q'。好吧,'t' 也在插槽'4',所以应该是'f' 。”你明白我的意思吗?
  • 哦,哎呀,我明白了。超过 36 个字符的字符串呢?
  • 不确定.. 有问题的输入字符串很容易有数万个字符长。我有一个想法是你启发的。所以,参考我的最后一条评论,“测试”是如何通过长度“1,2,3,4”检查的,我正在尝试限制长度,然后重复该限制.我正在生成一个长度与输入字符串匹配的字符串,但将按顺序为特定字符(例如:0-7。因此“HelloWorld”将根据其长度为“01234567012”),然后使用查找和替换字符num_string[#]input_string[#] 这样我就可以跟踪当前的替换字符
【解决方案2】:

this post 中的Mark Tolonen 的帮助下,我想出了一个解决方案。以下示例仅使用 4 个字典,而我打算做更多:

# Input String
string = "ttttttttttttttttttttttttttttttt"

# Defining dictionarys for 0-4
dicnums = [{"0":"n","1":"Q","2":"k","3":"W","4":"F","5":"g","6":"9","7":"e","8":"v","9":"3","a":"r","b":"T","c":"c","d":"o","e":"b","f":"y","g":"2","h":"A","i":"i","j":"p","k":"1","l":"P","m":"w","n":"x","o":"s","p":"Y","q":"h","r":"G","s":"7","t":"S","u":"6","v":"K","w":"Z","x":"M","y":"C","z":"J","A":"u","B":"f","C":"j","D":"E","E":"a","F":"H","G":"O","H":"N","I":"l","J":"U","K":"I","L":"V","M":"m","N":"5","O":"R","P":"4","Q":"z","R":"L","S":"0","T":"q","U":"D","V":"8","W":"B","X":"X","Y":"d","Z":"t"}, {"0":"q","1":"6","2":"W","3":"4","4":"J","5":"u","6":"n","7":"T","8":"I","9":"O","a":"V","b":"3","c":"Z","d":"s","e":"R","f":"E","g":"G","h":"P","i":"5","j":"l","k":"e","l":"m","m":"F","n":"t","o":"8","p":"K","q":"L","r":"Y","s":"M","t":"D","u":"j","v":"z","w":"H","x":"g","y":"9","z":"f","A":"0","B":"p","C":"o","D":"d","E":"X","F":"S","G":"k","H":"1","I":"Q","J":"C","K":"U","L":"i","M":"r","N":"w","O":"y","P":"B","Q":"2","R":"x","S":"A","T":"c","U":"7","V":"h","W":"v","X":"N","Y":"b","Z":"a"}, {"0":"x","1":"W","2":"q","3":"B","4":"j","5":"I","6":"E","7":"g","8":"U","9":"e","a":"8","b":"3","c":"5","d":"k","e":"9","f":"N","g":"7","h":"Q","i":"t","j":"r","k":"L","l":"Z","m":"b","n":"n","o":"Y","p":"H","q":"R","r":"6","s":"P","t":"1","u":"S","v":"M","w":"p","x":"l","y":"F","z":"2","A":"c","B":"T","C":"G","D":"h","E":"X","F":"v","G":"s","H":"O","I":"D","J":"4","K":"a","L":"A","M":"m","N":"d","O":"C","P":"f","Q":"V","R":"i","S":"o","T":"u","U":"w","V":"0","W":"K","X":"z","Y":"y","Z":"J"}, {"0":"x","1":"W","2":"q","3":"B","4":"j","5":"I","6":"E","7":"g","8":"U","9":"e","a":"8","b":"3","c":"5","d":"k","e":"9","f":"N","g":"7","h":"Q","i":"t","j":"r","k":"L","l":"H","m":"b","n":"n","o":"Y","p":"Z","q":"R","r":"6","s":"P","t":"1","u":"S","v":"M","w":"p","x":"l","y":"F","z":"2","A":"c","B":"T","C":"G","D":"h","E":"X","F":"v","G":"s","H":"O","I":"D","J":"4","K":"a","L":"A","M":"m","N":"d","O":"C","P":"f","Q":"V","R":"i","S":"o","T":"u","U":"w","V":"0","W":"K","X":"z","Y":"y","Z":"J"}]

# Changing my string characters
string_fin = ''.join(dicnums[i%4][c] for i,c in enumerate(string))

# Print result
print string_fin 

如果我使用所有 't',结果是 "SD11SD11SD11SD11SD11SD11SD11SD1"。如果我使字典范围更广,并且不重复使用相同的字符(我不会),那么输出会好得多。一旦与我的其他加密概念相结合,我的程序可能实际上可以像我想要的那样工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多