python - How to output values from a dictionary without printing it out as a list? -


i assigned group ana grams lexiographicaly.

below 1 of test cases:

input: eat tea tan ate nat bat

output:

ate eat tea bat nat tan

however, keep getting following output the anagrams encapsulated in list , order @ each line gets printed varies every time:

['ate', 'eat', 'tea'] ['nat', 'tan'] ['bat'] or

['nat', 'tan'] ['bat'] ['ate', 'eat', 'tea'] or

['ate', 'eat', 'tea'] ['bat'] ['nat', 'tan']

how fix outputs without being capped in list , possibly in right order?

this have done far:

import sys collections import * def computeanagrams(string):     d = defaultdict(list)     word in string:         key = ''.join(sorted(word))         d[key].append(word)     return d  def main():     string in sys.stdin:         stringlist = string.split()         if len(stringlist) == 0:             break         d = computeanagrams(stringlist)         key,anagrams in d.items():             if len(anagrams) >=1:                 print(sorted(anagrams))         print ('') main() 

note: machine runs programs reads input stdin/keyboard , prints output console(stdout).

i believe issue -

print(sorted(''.join(anagrams))) 

you using sorted after join list string, in case, sorted returns list of characters in sorted order (i guess current output getting).

if want elements in sorted order, sorted should used on anagrams list, not string after joining. example -

print(', '.join(sorted(anagrams))) 

i using ', ' join strings, use , separator, otherwise output strings without spaces in-between, if want, can use other separator want.

demo -

after above change -

input -

eat tea tan ate nat bat 

output -

bat ate, eat, tea nat, tan 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -