0

I have a list of several lists letters. Each letter correspond to a number.

h = 1
w = 2
wh = 3

list = [["w, w, h, w, w, w, h"], ["w, h, w, w, h, wh, h"]]

I need to the list of letters to print:

print(list[0])
>> w, w, h, w, w, w, h

Then I need to strip the quotes away so I can get

print(stripped_list)
>>2, 2, 1, 2, 2, 2, 1

I'm getting an error when I try the string method (strip, replace, etc). Is there another method or a better way of displaying my info to get both the original letters as well as a list of the corresponding numbers. Thank you.

Feick
  • 5
  • 1
  • Hello, and welcome to StackOverflow. Questions seeking debugging help must provide a [mcve]. Please check out [help] for more information on crafting on-topic questions. "I am getting an error" is not an adequate problem specification. What have you tried, and what error are you getting exactly? – juanpa.arrivillaga Oct 21 '18 at 17:57
  • maybe you're applying `replace` on the list instead of list items? – Jean-François Fabre Oct 21 '18 at 17:57
  • No, you can't just "strip away the quotes" because they're part of the representation of any string. What you can do: `", ".join(str(globals()[name.strip()]) for name in "w, h, wh".split(","))` – ForceBru Oct 21 '18 at 18:00

1 Answers1

0

Maybe something like this, use a dictionary for your conversions, and unpack your results

d = {'h': 1, 'w': 2, 'wh': 3}
lst = [["w, w, h, w, w, w, h"], ["w, h, w, w, h, wh, h"]]

print(*lst[0])
print(*[d[i] for i in lst[0][0].split(', ')])
# w, w, h, w, w, w, h
# 2 2 1 2 2 2 1
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20