2017-06-13 4 views
3

ich Python bin hier, ich schreibe ein einfaches ProgrammLogic Problem in Python-Code

Eingang

{'messagingservice': 'build4', 
'oltpdatabase': 'build1', 
'paymentsdatabase': 'build2', 
'restserver': 'build5', 
'PESQL': 'build3', 
'configdatabase': 'build1'} 

erwartete Ausgabe ist als unten

{'build4': 'messagingservice', 
'build5': 'restserver', 
'build2': 'paymentsdatabase', 
'build3': 'PESQL', 
'build1': 'oltpdatabase,configdatabase '} 

Sie den Code unten ist, ich habe verwendet ...

#!/usr/bin/python 
import json 
import ast 
from sys import argv 
data = json.dumps(argv[1]); 
json_to_unicode = json.loads(data) 
unicode_to_dic = ast.literal_eval(json_to_unicode); 
print(unicode_to_dic); 
result_dic={}; 
data=''; 
for k,v in unicode_to_dic.iteritems(): 
    if v in result_dic: 
    data=data.join((result_dic[v],',')); 
    print (data) 
    result_dic[v]=data 

    else: 
    result_dic[v]=k; 

print(result_dic) 

Tatsächliche outpu t ist:

{'build4': 'messagingservice', 
'build5': 'restserver', 
'build2': 'paymentsdatabase', 
'build3': 'PESQL', 
'build1': 'oltpdatabase,'} 

Einen weiteren Wert fehlt.

+0

Können Sie eine kurze Erklärung dazu geben, was dieser Code tun soll? – idjaw

+0

Mögliches Duplikat von [Malformed String ValueError ast.literal \ _eval() mit String-Darstellung von Tuple] (https://stackoverflow.com/questions/14611352/malformed-string-valueerror-ast-literal-eval-with-string- Repräsentation-of-tup) –

Antwort

1

Ihre beitreten hat das neue Element

result_dic={} 
data='' 
for k,v in d.iteritems(): 
    if v in result_dic.keys(): 
     data=data.join((result_dic[v],', ',k)) 
     result_dic[v]=data 
    else: 
     result_dic[v]=k 

print(result_dic) 

kehrt nicht beitreten

{'build4': 'messagingservice', 'build5': 'restserver', 'build2': 'paymentsdatabase', 'build3': 'PESQL', 'build1': 'oltpdatabase, configdatabase'} 

Sie auch collections.Counter

from collections import Counter 
new =Counter() 
d={'messagingservice': 'build4', 'oltpdatabase': 'build1', 'paymentsdatabase': 'build2', 'restserver': 'build5', 'PESQL': 'build3','configdatabase': 'build1'} 
for k,v in d.items(): 
    if new[v]: 
     new[v]+=', '+k 
    else: 
     new[v]=k 
print new 

Renditen nutzen könnten

Counter({'build1': 'oltpdatabase, configdatabase', 
     'build2': 'paymentsdatabase', 
     'build3': 'PESQL', 
     'build4': 'messagingservice', 
     'build5': 'restserver'}) 
4

Sie könnten einen defaultdict verwenden, dass das Programm machen könnte viel einfacher:

unicode_to_dic = { 
'messagingservice': 'build4', 
'oltpdatabase': 'build1', 
'paymentsdatabase': 'build2', 
'restserver': 'build5', 
'PESQL': 'build3', 
'configdatabase': 'build1'} 

from collections import defaultdict 

res = defaultdict(list) 

# find all keys that have the same value 
for key, value in unicode_to_dic.items(): 
    res[value].append(key) 

# convert the list of keys to a string seperated by ',' 
for key, value in res.items(): 
    res[key] = ','.join(value) 

# Convert it to a normal dict - that's optional because defaultdict behaves just 
# like a normal dict (in most circumstances at least). 
dict(res) 

# {'build1': 'oltpdatabase,configdatabase', 
# 'build2': 'paymentsdatabase', 
# 'build3': 'PESQL', 
# 'build4': 'messagingservice', 
# 'build5': 'restserver'} 

Ich habe Ihren Algorithmus nicht debuggt, aber es gibt mehrere Dinge, die auffallen:

  • Python doesn brauche nicht ; am Ende der Zeilen.

  • str.join wird eigentlich als seperator.join(list_of_words_to_be_joined) bezeichnet. Sie haben den Separator als Eingabe für die Funktion verwendet.

+0

Vielen Dank, verbrachte ich den ganzen Tag, um das zu knacken .. :) –

+0

@VetrivelkumarPandi Kein Problem. Vergessen Sie nicht, [upvote] (https://stackoverflow.com/help/privileges/vote-up) alle hilfreichen Antworten zu lesen und [accept] (https://meta.stackexchange.com/questions/5234/how-does -Aufnahme-eine-Antwort-Arbeit) die hilfreichste. – MSeifert

1

data.join tut nicht, was Sie denken. Was Sie wollen, ist:

result_dic[v] += ',' + k