D
D
dabiankolins12018-08-14 01:42:17
Python
dabiankolins1, 2018-08-14 01:42:17

How to find dictionary values ​​in a list?

We have a dictionary a={001:'1',010:'2',011:'3'}
and a list b=[0,1,0,0,0,1,0,1,1] the task is to split the list three elements each (those 0,1,0-010 and so on) and the value of this triad was searched for in the dictionary a, by value?
result:
213

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly, 2018-08-14
@trofimovdev

Your task is strange. Not to mention, the number 001 as a dictionary key will cause a syntax error.

a = {'001': '1', '010': '2', '011': '3'}
b = [0, 1, 0, 0, 0, 1, 0, 1, 1]
for i in range(0, len(b), 3):
  print(a[''.join([str(b[i]), str(b[i + 1]), str(b[i + 2])])])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question