简体   繁体   English

如何更改/交换值 position 是 python 字典?

[英]How can I change/swap value position is python dictionary?

I'm trying to make a cipher based on Ceaser Cipher, which instead of letters, I used numbers.我正在尝试基于 Ceaser Cipher 制作密码,我使用数字代替字母。 for example, 1 = 'a', 2 = 'b' and so on.例如,1 = 'a',2 = 'b' 等等。

This is the full code:这是完整的代码:

import string

dict = {}
message = input("Enter a message\n")
key = input("Enter a key\n")
encrypted = ""

for i, char in enumerate(string.ascii_lowercase):
    #key is from 0 to 25
    dict[i] = char
print(dict)

for val in message:
    if val in dict:
        encrypted += dict[val]
    for key, value in dict.items():
        if val == value:
            encrypted += str(key + 1)
            encrypted += " "
print(encrypted)

in print(dict) , it prints the value of the keyprint(dict)中,它打印键的值

For example:例如:

{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}

What I want to do is to change the value based on the key input我想做的是根据键输入更改值

For example:例如:

key = 'D3'

My desired output:我想要的 output:

{0: 'd', 1: 'e', 2: 'f', 3: 'a', 4: 'b', 5: 'c', 6: 'g', 7: 'h'}

Is there any solution for this?有什么解决办法吗?

This solution works only for the first time that the value_of_interest is found within the dict.此解决方案仅适用于第一次在字典中找到value_of_interest的情况。 In your case example - that s not the case, but I am not sure whether a more generalized approach of your cipher (I don t really know what exactly this is) ought to have this considered beforehand.在你的案例中 - 情况并非如此,但我不确定你的密码的更通用方法(我真的不知道这是什么)是否应该事先考虑这个问题。

my_dict={0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
print(f'Init dict: {my_dict}')

key_list=list(my_dict.keys())

#let's say ... (please make a more generalized parsing method for swaper)
swaper='D3'
print(f'Input swaper: {swaper}')
value_of_interest=swaper[0].lower()
shift_=int(swaper[1])

new_dict={}
for counter,key in enumerate(key_list):
    if my_dict[key]==value_of_interest :
        if len(key_list)>=counter+shift_: #Added a method for checking whether there are enough keys within the dict - after the D value is found.
            new_dict = {i:my_dict[key] for i,key in enumerate(key_list[counter:counter+shift_])}

            for i,key in enumerate(key_list[0:counter]):
                new_dict[shift_+i]=my_dict[key]
            for key in key_list[counter+shift_:]:
                new_dict[key]=my_dict[key]
        else:
            print(f'There are not more than {shift_} keys following the {value_of_interest} value')
            
print(f'Output dict: {new_dict}')

Output: Output:

Init dict: {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
Input swaper: D3
Output dict: {0: 'd', 1: 'e', 2: 'f', 3: 'a', 4: 'b', 5: 'c', 6: 'g', 7: 'h'}

In another case for example, Given the value of "B5" in the swaper variable, it gives:例如,在另一种情况下,给定交换器变量中“B5”的值,它给出:

Init dict: {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
Input swaper: B5
Output dict: {0: 'b', 1: 'c', 2: 'd', 3: 'e', 4: 'f', 5: 'a', 6: 'g', 7: 'h'}

I assume 'd3' means starting from d you are switching 3 values to the left side.我假设“d3”意味着从 d 开始,您将 3 个值切换到左侧。 If that's the case, 'd2' will return the following output: {0: 'd', 1: 'e', 2: 'c', 3: 'a', 4: 'b', 5: 'f', 6: 'g', 7: 'h'}如果是这种情况,“d2”将返回以下 output: {0: 'd', 1: 'e', 2: 'c', 3: 'a', 4: 'b', 5: 'f', 6: 'g', 7: 'h'}

See if this code will work, I wrote a loop and it allows you to customize your key every time.看看这段代码是否有效,我写了一个循环,它允许您每次都自定义您的密钥。 d is the initial dictionary and k is the 'D3' key. d是初始字典, k是“D3”键。

def swipe(d,k):
    start = k[0].lower()
    num = int(k[1])
    count = 0
    for i in d:
        if d[i] == start:
            break
        else:
            count+=1
    for i in range(count,count+num):
        d[i-count], d[i] = d[i],d[i-count]
    return d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM