Python|翻訳ウィジェット


1、前章の内容を振り返る
1.1、random+stringを使用してパスワードを自動的に生成する
#!/usr/bin/env python
# Author:ywyankerp
import random
import string
My_List = []
My_Name = input("         :")
My_Password = input("            :")
for i in range(int(My_Password)):
    My_Ran = random.choice(string.ascii_uppercase + string.digits)
    My_List.append(My_Ran)
Resultss = "".join(My_List)
User =  f"      :{My_Name}       :{Resultss}"
print(User)
>>>         :  
>>>            :9
      :         :LZT1W3MMR

2、本章の知識点
  • dict
  • set
  • tuple

  • 2、辞書の使用
    2.1、辞書構造は1つの対応関係を探し当てて{“鍵”:“値”}{“1”:“2”}dict集合
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    print(My_Name_dict)
    >>>{'  ': 'yankai', '  ': 'zhangsan', '  ': 'wangwu'}
    

    2.2、値を取る
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Name = My_Name_dict["  "]
    print(My_Name)
    >>>yankai
    

    たとえば、存在しない値を取得します.
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Name = My_Name_dict["uwaydoawo"]
    print(My_Name)
    >>>Traceback (most recent call last):
      File "e:\Python_yanzan\Python-yankai\My_dict.py", line 8, in <module>
        My_Name = My_Name_dict["uwaydoawo"]
    KeyError: 'uwaydoawo'
    

    クエリーにない値は直接エラーを報告します
    2.3.get方式で値を取る
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Name = My_Name_dict.get("  ")
    print(My_Name)
    >>>yankai #     
    

    では、.get方式を使用して、ない値を取る効果は次のとおりです.
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Name = My_Name_dict.get("pwadjdpjwap;dwa")
    print(My_Name)
    >>>None #       !         
    

    たとえば、.get方式で値を取ります.この値がなければ、次のように出力します.
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Name = My_Name_dict.get("pwadjdpjwap;dwa","             ")
    print(My_Name)
    >>>              #      
    

    My_Name = My_Name_dict.get(「pwadjdpjwap;dwa」、「検索した内容に合わない」
    解釈:“pwadjdpjwap;dwa”に内容がなければ、“出力の後ろのこの話!”
    2.4、辞書にキー値を追加する
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Name = My_Name_dict
    print(My_Name)
    >>>{'  ': 'yankai', '  ': 'zhangsan', '  ': 'wangwu'}
    #       yankai zhangsan wangwu 
    

    キー値を直接追加するには、次のようにします.
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_ADD = My_Name_dict["  "] = "27"
    #     ["   "] = " "
    print(My_Name_dict)
    >>>{'  ': 'yankai', '  ': 'zhangsan', '  ': 'wangwu', '  ': '27'} #     
    

    2.5、辞書にリストリストを直接追加できる
    #!/usr/bin/env python
    # Author:ywyankerp
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_ADD = My_Name_dict["  "] = [1, 2, 3, 4, 5, 6,]
    print(My_Name_dict)
    >>>{'  ': 'yankai', '  ': 'zhangsan', '  ': 'wangwu', '  ': [1, 2, 3, 4, 5, 6]} #     
    

    リストを定義してから辞書にネストすることもできます.
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ] #        
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add = My_Name_dict["    "] = My_List #          
    print(My_Name_dict)
    >>>{'  ': 'yankai', '  ': 'zhangsan', '  ': 'wangwu', '    ': ['  ', '  ', '  ', '  ', '  ']} #     
    

    辞書をネストすることもできます.
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ]
    Add_book_dict = {
        "book" : "python3",
        "price" : 100
    }
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add =  My_Name_dict["   "] = Add_book_dict
    print(My_Name_dict)
    >>> {'  ': 'yankai', '  ': 'zhangsan', '  ': 'wangwu', '   ': {'book': 'python3', 'price': 100}} #     
    

    辞書のキーを削除
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ]
    Add_book_dict = {
        "book" : "python3",
        "price" : 100
    }
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add =  My_Name_dict["   "] = Add_book_dict
    del My_Name_dict["  "] #   :      
    print(My_Name_dict)
    >>> {'  ': 'yankai', '  ': 'zhangsan', '   ': {'book': 'python3', 'price': 100}} #        
    

    del辞書名[キー]を削除すると削除できます
    ディクショナリ内のキーの値の変更
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ]
    Add_book_dict = {
        "book" : "python3",
        "price" : 100
    }
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add =  My_Name_dict["   "] = Add_book_dict
    My_Name_dict["  "] = "yankerp" #     
    print(My_Name_dict)
    >>> {'  ': 'yankerp', '  ': 'zhangsan', '  ': 'wangwu', '   ': {'book': 'python3', 'price': 100}} #     
    

    修正しゅうせい:ディクショナリ名[キー](Key)=[値](Value)
    3、keysメソッドを使って辞書の中のだからkeyを取得する
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ]
    Add_book_dict = {
        "book" : "python3",
        "price" : 100
    }
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add =  My_Name_dict["   "] = Add_book_dict
    My_keys = My_Name_dict.keys() #   keys  
    print(My_keys)
    >>> dict_keys(['  ', '  ', '  ', '   ']) #     
    

    forループ出力の使用
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ]
    Add_book_dict = {
        "book" : "python3",
        "price" : 100
    }
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add =  My_Name_dict["   "] = Add_book_dict
    for i in My_Name_dict.keys():
        print(i)
    >>> #        
    >  
      
      
      
    

    itemsメソッドの使用
    1つのメソッド#キー値ペアは、メタグループの1つのグループとして理解される.
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ]
    Add_book_dict = {
        "book" : "python3",
        "price" : 100
    }
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add =  My_Name_dict["   "] = Add_book_dict
    My_itm = My_Name_dict.items()
    print(My_itm)
    >>> dict_items([('  ', 'yankai'), ('  ', 'zhangsan'), ('  ', 'wangwu'), ('   ', {'book': 'python3', 'price': 100})]) #                  
    

    forループ出力itemsの使用
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ]
    Add_book_dict = {
        "book" : "python3",
        "price" : 100
    }
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add =  My_Name_dict["   "] = Add_book_dict
    for i in My_Name_dict.items():
        print(i)
    ('  ', 'yankai')
    ('  ', 'zhangsan')
    ('  ', 'wangwu')
    ('   ', {'book': 'python3', 'price': 100})
    

    以上の出力は、キー&&値をそれぞれ設定して変数出力を以下のように出力することができます.
    #!/usr/bin/env python
    # Author:ywyankerp
    My_List = ["  ", "  ", "  ", "  ", "  ", ]
    Add_book_dict = {
        "book" : "python3",
        "price" : 100
    }
    My_Name_dict = {
        "  " : "yankai",
        "  " : "zhangsan",
        "  " : "wangwu"
    }
    My_Add =  My_Name_dict["   "] = Add_book_dict
    for a, b in My_Name_dict.items():
        My_fules = f"{a}=={b}"
        print(My_fules)
    #     :
      ==yankai
      ==zhangsan
      ==wangwu
       =={'book': 'python3', 'price': 100}
    

    3、tupleユニット
    name = 1,2
    print(name)
    >>(1,2)
    

    かっこをつける
    name = (1,2)
    print(name)
    >>(1,2)
    

    3.1、例えばIPアドレス及びポート
    Ip_Port = ("192.168.1.100","80")
    print(Ip_Port)
    >>('192.168.1.100', '80')
    

    3.2、Ip_portタプルをネストする辞書
    My_IP = {} #         
    Ip_Port = ("192.168.1.100","80") #       
    My_IP["My_ip_port"] = Ip_Port #           
    print(My_IP) #     
    >>>{'My_ip_port': ('192.168.1.100', '80')} #     
    

    重複しない要素
    setコレクション
    nuber = {1,2,2,3,3,4,5,6,7,7,7,7,78,8,9,}
    set
    nuber
    >>{1, 2, 3, 4, 5, 6, 7, 8, 9, 78} 
    
    for i in nuber:
        print(i)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    78
    
    name = {1,2,3,4,4,4,4,4,5,5,5,5,6,7,8,9,0}
    result = set(name)
    print(result)
    >>>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    type(result)
    set
    

    4、翻訳ウィジェットの作成
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    print("    yankerp     ,           .".center(50, "-"))
    import random
    import string
    import sys
    Your_name = input("         :")
    Your_Password = input("           :")
    My_list = []
    for i in range(int(Your_Password)):
        My_Password = random.choice(string.ascii_lowercase + string.digits)
        My_list.append(My_Password)
        My_List_Com = "".join(My_list)
    print(f"               :{Your_name}      :{My_List_Com}")
    print("            ,              ".center(50, "-"))
    Your_Sign = input("         :")
    Your_Sign_pass = input ("        :")
    if Your_Sign == Your_name and Your_Sign_pass == My_List_Com:
        print("         ")
    else:
        print("             ,        !!!")
        sys.exit()
    print("           ,      ~~~".center(50, "-"))
    
    My_dict = {
        "  " : "China",
        "  " : "YanKai",
        "  " : "Monday",
        "  " : "Python",
        "  " : "sport",
        " " : "steel",
        "  " : "steer",
    }
    Your_query = input("         :")
    if (My_dict.get(Your_query, '')):
        print(f"        :{Your_query}      :{My_dict[Your_query]}")
    else: 
        print(f"           '{Your_query}'    !!!")
        Your_Add = input("          (y/n?)")
        if Your_Add == "y":
            Key = input("         Key:")
            value = input("         value:")
            Add_dict = My_dict[Key] = value
            print(f"       -    :{Key}     :{value}")
            for a, b in My_dict.items():
                print(f"{a} == {b}")
        else:
            print("        ,      !!!")
    
    

    実行結果は次のとおりです.
    ----------    yankerp     ,           .-----------
             :  
               :9
                   :        :n9eiu9xe4
    -----------------------
             :  
            :n9eiu9xe4
             
    --------------~~~---------------
             ::Python
    

    ユーザー名のパスワードを次のように入力します.
    ----------    yankerp     ,           .-----------
             :  
               :9
                   :        :mm9yg5czo
    -----------------------
             :  
            :a
                 ,        !!
    

    翻訳キーを追加するには、次のようにします.
    ----------    yankerp     ,           .-----------
             :  
               :9
                   :        :er5zfn0iw
    -----------------------
             :  
            :er5zfn0iw
             
    --------------~~~---------------
             :a
               'a'    !!!
              (y/n?)y
             Key:  
             value:zhangsan
           -    :       :zhangsan
       == China
       == YanKai
       == Monday
       == Python
       == sport
      == steel
       == steer
       == zhangsa