简体   繁体   中英

Logic issue in Python code

I am new to Python,I am writing a simple program

Input

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

expected output is as below

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

Below is the code I have used ...

#!/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)

Actual output is:

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

Missing one more value.

You could use a defaultdict , that could make the program much easier:

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'}

I haven't debugged your algorithm but there are several things that stand out:

  • Python doesn't need ; at the end of lines.

  • str.join is actually called as seperator.join(list_of_words_to_be_joined) . You used the seperator as input for the function.

data.join doesn't do what you think. What you want is:

result_dic[v] += ',' + k

your join did not join the new item

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)

returns

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

you could also use 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

returns

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM