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

更新於: 30-Jul-2019

95 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.