Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python의 Zip 함수를 사용하여 새 문자 집합으로 변경합니다.

<시간/>

26개의 문자 집합이 주어지면 여기에서는 새 문자 집합을 사용합니다. 그리고 알파벳 집합(a, b, c........z)과 같은 또 다른 문자 집합이 있는 경우 우리의 임무는 새 문자 집합과 해당 알파벳 집합 간의 관계를 만드는 것입니다.

예시

New character set: qwertyuiopasdfghjklzxcvbnm
Input: "wwmm"
Output: bbzy

알고리즘

Step 1: Given a new character set and input the string to make a relation.
Step 2: and the original character set also given below.
Step 3: Create a dictionary, we use here map technique, we map the English character set and new given character set, zip() does it for us. Both character sets are the map, here we match each character of given character set with each character of new charset sequentially.
Step 4: iterate through the original string and get characters of an original character set.
Step 5: join characters without space to get new string.

예시 코드

# Function to change string to a new character 
defnewString(cs,n): 
   ori = 'abcdefghijklmnopqrstuvwxyz'
   newchar = dict(zip(cs,ori)) 
   newstr = [newchar[chr] for chr in n]  
   print (''.join(newstr)) 
# Driver program 
if __name__ == "__main__": 
   newcharSet = 'qwertyuiopasdfghjklzxcvbnm'
   input = 'wwmn'
   newString(newcharSet,input) 

출력

bbzy