fork download
  1. '''
  2. created for stack overflow question
  3. https://stackoverflow.com/questions/65965140/how-to-convert-a-list-word-to-a-dictionary-key-and-to-a-string-at-end
  4.  
  5. usage:
  6. invoke from command line with no arguments to use parameters present in question.
  7. otherwise invoke with 2 arguments
  8. e.g. maptuples <tuplelist.txt> <mapdict.txt>
  9. '''
  10.  
  11. def mapem(iterable, mapping):
  12. dd = {v: k for k, v in mapping.items()}
  13. for t in iterable:
  14. mapped = dd.get(t)
  15. if mapped:
  16. yield mapped
  17.  
  18.  
  19. if __name__ == '__main__':
  20. from sys import argv
  21. if len(argv) < 3:
  22. d = {'J': (133, 101, 150), 'n': (130, 99, 225), '1': (163, 201, 196), 'r': (212, 212, 235), 'S': (248, 128, 245), '8': (65, 244, 22), 'f': (22, 213, 90), 'U': (173, 66, 188)}
  23. tl = [(133, 101, 150), (130, 99, 225), (163, 201, 196), (212, 212, 235), (248, 128, 245), (190, 239, 125), (123, 202, 119), (107, 58, 33), (161, 135, 209), (108, 105, 192)]
  24. print(''.join(mapem(tl, d)))
  25. else:
  26. from ast import literal_eval
  27. with open(argv[1]) as ftup:
  28. tuples = literal_eval(ftup.read())
  29. with open(argv[2]) as fdict:
  30. mapping = literal_eval(fdict.read())
  31.  
  32. with open('tupresult.txt', 'w') as fout:
  33. for letter in mapem(tuples, mapping):
  34. fout.write(letter)
  35.  
  36. with open('tupresult.txt') as f:
  37. print(f.read())
  38.  
Success #stdin #stdout 0.03s 9224KB
stdin
Standard input is empty
stdout
Jn1rS