0
keys = ["x","y","z","x*x","2*y","2*z+3*x*x","w"]
values = [[0.00221024, 0.00289774, 0.00362644, 0.00222213, 0.00289774, 0.00243777, 0.00346482]]
dict_values = dict(zip(keys,zip(*values)))

output:

{'x' : (0.00221024,)
'y' : (0.00289774,)
'z' : (0.00362644,)
'x*x' : (0.00222213,)
'2*y' : (0.00289774,)
'2*z+3*x*x':('0.00243777,')
'w' : (0.00346482,)}

Excepted Output:

{'x' : 0.00221024,
'y' : 0.00289774,
'z' : 0.00362644,
'x*x' : 0.00222213,
'2*y' : 0.00289774,
'2*z+3*x*x': 0.00243777,
'w' : 0.00346482,}

I tried dict and zip function but can anyone explain how to write a loop or any logic to match with the excepted output.

4
  • Please add code, output of the code and error messages as a properly formatted text to your question, not as a screenshot. Commented Nov 18, 2020 at 16:17
  • Why are you using zip twice? Commented Nov 18, 2020 at 16:28
  • Not a machine-learning` question, kindly do not spam irrelevant tags (removed). Commented Nov 19, 2020 at 0:07
  • If my answer is useful, please accept it. Commented Nov 19, 2020 at 13:17

1 Answer 1

1
dict(zip(keys,values[0]))

output:

{'x': 0.00221024,
 'y': 0.00289774,
 'z': 0.00362644,
 'x*x': 0.00222213,
 '2*y': 0.00289774,
 '2*z+3*x*x': 0.00243777,
 'w': 0.00346482}

values is a list of lists with only one element, so you have to select the first list (values[0]).

zip() function creates an iterator that aggregates elements from two or more iterables. In your case, you have to use it only once.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.