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 次浏览

开启你的 职业

通过完成课程获得认证

开始
广告