Issue
The Dictionary parm should contain all the keys ['f', 'r', 'b', 'l', 't','u']
and only then the below should happen. The below mentioned iteration prints in an unexpected order, please correct what's wrong.
parm = {'r':'r', 'l':'l', 't':'t', 'u':'u', 'f':'f', 'b':'b'}
if all(key in parm for key in ['f', 'r', 'b', 'l', 't','u']):
parm = [_ for _ in parm.values() for i in range(0,9)]
print (parm)
Returns:
['b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 't', 't', 't', 't', 't', 't', 't', 't', 't']
Expected:
['f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 'l', 't', 't', 't', 't', 't', 't', 't', 't', 't', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u',]
Solution
Your list comprehension produces items according to the order of the values of the parm
dict (which is pretty arbitrary prior to Python 3.6), so naturally it won't follow the order of the keys you use in the condition for the if
statement. If you want the keys to be reordered in the same way as the keys used in the condition you should make it a separate list and use it for both the condition and the list comprehension:
parm = {'r':'r', 'l':'l', 't':'t', 'u':'u', 'f':'f', 'b':'b'}
keys = ['f', 'r', 'b', 'l', 't','u']
if all(key in parm for key in keys):
parm = [parm[key] for key in keys for i in range(0,9)]
print (parm)
Answered By - blhsing
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.