Python
 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

  1. Pythonのissubset()関数

    この記事では、Python標準ライブラリで利用可能なissubset()関数の実装と使用法について学習します。 issubset()メソッドは、セットのすべての要素が別のセットに存在する場合(引数として渡される場合)はブール値のTrueを返し、それ以外の場合はブール値のFalseを返します。 下の図では、BはAのサブセットです。AとBが同一のセットである場合、AはBの適切なサブセットであることを意味します。これは、両方のセットに同じ要素が含まれていることを意味します。 構文 <set 1>.issubset(<set 2>) 戻り値 boolean True/

  2. Intersection()関数Python

    この記事では、任意のセットで実行できるintersection()関数について学習します。数学によると、共通部分とは、2つのセットから共通の要素を見つけることを意味します。 構文 <set name>.intersection(<set a1> <set a2> ……..) 戻り値 引数として渡されるセット内の共通要素。 例 set_1 = {'t','u','t','o','r','i','a','l&