2017-11-24 20 views
0

私はPythonには新しく、辞書と文字列を比較できるコードを書こうとしています。私の入力辞書や文字列は次のようになります。辞書と文字列を比較して辞書を出力するには?

final_output = {'Python' : 'high-level language', 
    'and' : 'O', 
    'Php' : 'ssl', 
    'anbd' : 'O', 
    'Java' : 'low-level language', 
    'are' : 'O', 
    'programming' : 'O', 
    'languages.' : 'O', 
    'PYTHON' : 'high-level language', 
    'and' : 'O', 
    'PHP' : 'ssl', 
    'and' : 'O', 
    'JAVA' : 'low-level language', 
    'are' : 'O', 
    'good' : 'O', 
    'skills' : 'O', 
    'to' : 'O', 
    'have.' : 'O', 
    'python' : 'high-level language', 
    'and' : 'O', 
    'php' : 'ssl', 
    'and' : 'O', 
    'java' : 'low-level language', 
    'are' : 'O', 
    'programming' : 'O', 
    'languages' : 'O'} 

l = {'Python' : 'high-level language', 'PHP' : 'ssl', 'Java' : 'low-level language'} 

st = 'Python and Php and Java are programming languages. PYTHON and PHP and JAVA are good skills to have. python and php and java are programming languages' 

私は辞書のキーと文字列の個々の単語の間に大文字小文字を区別しない比較をしたい、次の出力(スプリット文字列に関数を適用)

または私は短所でこれを達成しようとした

final_output = [('Python' , 'low-level language'), 
('and' , 'O'), 
('Php' , 'ssl'), 
('and' , 'O'), 
('Java' , 'low-level language'), 
('are' , 'O'), 
('programming' , 'O'), 
('languages.' , 'O'), 
('PYTHON' , 'high-level language'), 
('and' , 'O'), 
('PHP' , 'ssl'), 
('and' , 'O'), 
('JAVA' , 'low-level language'), 
('are' , 'O'), 
('good' , 'O'), 
('skills' , 'O'), 
('to' , 'O'), 
('have.' , 'O'), 
('python' , 'high-level language'), 
('and' , 'O'), 
('php' , 'ssl'), 
('and' , 'O'), 
('java' , 'low-level language'), 
('are' , 'O'), 
('programming' , 'O'), 
('languages' , 'O')] 

(私の場合にも重要である)文の整合性が維持されるタプルのリストのような二つのリスト(というよりも、キーと文字列の辞書との比較)と比較することで、私の入力をideringし、次のコード行を書いた:

[i for i in (st.split(' ')) if i.lower() in [i.lower() for i in l]] 

をして得た:私が書いた上記の入力について

l = ['Python', 'PHP', 'Java'] 

st = 'Python and Php and Java are programming languages. PYTHON and PHP and JAVA are good skills to have. python and php and java are programming languages' 

['Python', 'Php', 'Java', 'PYTHON', 'PHP', 'JAVA', 'python', 'php', 'java'] 

これを適用して辞書とリストを比較し、必要に応じて出力を取得したいが、それを行う方法はわからない。

+0

'私は....'あなたが何かをしましたしたいですか?あなたのコードはどこですか? –

+0

申し訳ありません...私が試したことで質問を更新しました – sharathchandramandadi

答えて

1

それはとても簡単です:

l = {'Python' : 'high-level language', 'PHP' : 'ssl', 'Java' : 'low-level language'} 
st = 'Python and Php and Java are programming languages. PYTHON and PHP and JAVA are good skills to have. python and php and java are programming languages' 
l2 = {k.upper():v for k, v in l.items()} 
output_dict = {i:l2.get(i.upper(), 'O') for i in st.split(' ')} 

出力は次のようになります。

{'JAVA': 'low-level language', 
'Java': 'low-level language', 
'PHP': 'ssl', 
'PYTHON': 'high-level language', 
'Php': 'ssl', 
'Python': 'high-level language', 
'and': 'O', 
'are': 'O', 
'good': 'O', 
'have.': 'O', 
'java': 'low-level language', 
'languages': 'O', 
'languages.': 'O', 
'php': 'ssl', 
'programming': 'O', 
'python': 'high-level language', 
'skills': 'O', 
'to': 'O'} 

あなたが入力と同じ順序を取得したい場合は!注文キーで簡単にやります!:

your_order_key = 'Python and Php and Java are programming languages. PYTHON and PHP and JAVA are good skills to have. python and php and java are programming languages'.split(' ') 

注文を受けるにはdict! collection libを使用できます。

from collections import OrderedDict 

order_dict = OrderedDict() 
for k in your_order_key: 
    order_dict[k]=output.get(k) 

それをすべて入れ:

from collections import OrderedDict 
order_dict = OrderedDict() 

l = {'Python' : 'high-level language', 'PHP' : 'ssl', 'Java' : 'low-level language'} 
st = 'Python and Php and Java are programming languages. PYTHON and PHP and JAVA are good skills to have. python and php and java are programming languages' 
l2 = {k.upper():v for k, v in l.items()} 
output_dict = {i:l2.get(i.upper(), 'O') for i in st.split(' ')} 
for k in your_order_key: 
    order_dict[k]=output.get(k) 

をそして、あなたは、出力は次のようになります。

OrderedDict([('Python', 'high-level language'), 
      ('and', 'O'), 
      ('Php', 'ssl'), 
      ('Java', 'low-level language'), 
      ('are', 'O'), 
      ('programming', 'O'), 
      ('languages.', 'O'), 
      ('PYTHON', 'high-level language'), 
      ('PHP', 'ssl'), 
      ('JAVA', 'low-level language'), 
      ('good', 'O'), 
      ('skills', 'O'), 
      ('to', 'O'), 
      ('have.', 'O'), 
      ('python', 'high-level language'), 
      ('php', 'ssl'), 
      ('java', 'low-level language'), 
      ('languages', 'O')]) 
+0

あなたは大文字小文字の区別を忘れています。 – mhawke

+0

@mhawke、うん!更新しました! –

+0

文の完全性は維持されなければなりません(予想される出力に言及したように、私たちは辞書のキーの順序を維持できないことを知っています)。しかし、タプルのリストとして結果を出力する助けがあれば大丈夫です。 – sharathchandramandadi

1

はちょうどロジックといくつかを使用し、この単純なタスクのための2つのループを使用しないでください。多くのループではなく の条件で、1つのループで作業を実行できます ループ:

import json 

data_keys = {'Python': 'high-level language', 'PHP': 'ssl', 'Java': 'low-level language'} 

data = 'Python and Php and Java are programming languages. PYTHON and PHP and JAVA are good skills to have. python and php and java are programming languages' 

result = {} 
for words in data.split(): 

    if words.title() in data_keys: 
     result[words] = data_keys.get(words.title()) 
    elif words.lower() in data_keys: 
     result[words] = data_keys.get(words.lower()) 
    elif words.upper() in data_keys: 
     result[words] = data_keys.get(words.upper()) 



    else: 
     result[words] = 0 

print(json.dumps(result, indent=2)) 

出力:

{ 
    "php": "ssl", 
    "Java": "low-level language", 
    "Python": "high-level language", 
    "to": 0, 
    "programming": 0, 
    "have.": 0, 
    "java": "low-level language", 
    "PHP": "ssl", 
    "languages": 0, 
    "python": "high-level language", 
    "skills": 0, 
    "languages.": 0, 
    "Php": "ssl", 
    "JAVA": "low-level language", 
    "are": 0, 
    "PYTHON": "high-level language", 
    "and": 0, 
    "good": 0 
} 

あなたがコメントアウトとしてあなたは秩序を維持したい場合は、次の文章の完全性が維持されなければならない

その後:

import json 

data_keys = {'Python': 'high-level language', 'PHP': 'ssl', 'Java': 'low-level language'} 

data = 'Python and Php and Java are programming languages. PYTHON and PHP and JAVA are good skills to have. python and php and java are programming languages' 

result = [] 
for words in data.split(): 

    if words.title() in data_keys: 
     result.append((words,data_keys.get(words.title()))) 
    elif words.lower() in data_keys: 
     result.append((words,data_keys.get(words.lower()))) 
    elif words.upper() in data_keys: 
     result.append((words,data_keys.get(words.upper()))) 




    else: 
     result.append((words,0)) 



print(json.dumps(result,indent=2)) 

出力:

[ 
    [ 
    "Python", 
    "high-level language" 
    ], 
    [ 
    "and", 
    0 
    ], 
    [ 
    "Php", 
    "ssl" 
    ], 
    [ 
    "and", 
    0 
    ], 
    [ 
    "Java", 
    "low-level language" 
    ], 
    [ 
    "are", 
    0 
    ], 
    [ 
    "programming", 
    0 
    ], 
    [ 
    "languages.", 
    0 
    ], 
    [ 
    "PYTHON", 
    "high-level language" 
    ], 
    [ 
    "and", 
    0 
    ], 
    [ 
    "PHP", 
    "ssl" 
    ], 
    [ 
    "and", 
    0 
    ], 
    [ 
    "JAVA", 
    "low-level language" 
    ], 
    [ 
    "are", 
    0 
    ], 
    [ 
    "good", 
    0 
    ], 
    [ 
    "skills", 
    0 
    ], 
    [ 
    "to", 
    0 
    ], 
    [ 
    "have.", 
    0 
    ], 
    [ 
    "python", 
    "high-level language" 
    ], 
    [ 
    "and", 
    0 
    ], 
    [ 
    "php", 
    "ssl" 
    ], 
    [ 
    "and", 
    0 
    ], 
    [ 
    "java", 
    "low-level language" 
    ], 
    [ 
    "are", 
    0 
    ], 
    [ 
    "programming", 
    0 
    ], 
    [ 
    "languages", 
    0 
    ] 
] 
関連する問題