English 中文(简体)
如何匹配从输入到列表的元素?
原标题:How to match elements from input to list?
I m new to python, and I just wanted to ask how you would match one element from a user input to a list. I m currently working on a caesar cipher program, and I basically typed up a list of the alphabets: alphabet = [ a , b , c , d ,...] Then asked the user for a word to encrypt, text = input( Enter your message ) and the shift amount, I figured I could set an empty string variable and then loop through (for loop) each letter to shift it. But this is where I got stuck, how will I shift each letter in the input by the shift amount using the list? I tried to loop through the input using the indexes of the list alphabet, but since had no idea what I was doing it didn t work.
问题回答
Here s a generic implementation of the caesar-cipher, that should work for any shift amount, and any charset. Feel free to get rid of the type hinting if you feel it hinders readability. I personally would prefer to have it though. import string from typing import Hashable, Iterable, Sequence, TypeVar T = Hashable def caesar_ciper(message: Iterable[T], k: int, charset: Sequence[T] = string.ascii_lowercase) -> Iterable[T]: n = len(charset) char_idx = dict(zip(charset, range(n))) shift = lambda x: charset[(char_idx[x] + k) % n] return map(shift, message) message = abcxyz # read from user k = 3 # read from user encoded = .join(caesar_ciper(message, k)) assert encoded == defabc
So if you are just trying to shift by a constant amount, then this is fairly straightforward, you could have done with this a string rather than a list as well. The main thing to think about is the wrapping, which we can do with modulus operator. Lets say you want to shift 3 places: So if the index + shift is less than 26, the modulo operation doesn t change anything 2 + 3 % 26 = 5 If index + shift is 26 or greater, the modulo operation wraps it around. 24 + 3 % 26 = 1 example = "zebra" alphabet = [ a , b , c , d , e , f , g , h , i , j , k , l , m , n , o , p , q , r , s , t , u , v , w , x , y , z ] encrypted = for char in example: if char in alphabet: i = alphabet.index(char) j = (i + 3) % 26 encrypted += alphabet[j] else: encrypted += char




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签