0

I have an iterator object then when iterated has its each item attributes out of which the interested in time and value

[(point.time, point.value) for point in control_points]
[(Fraction(-1, 23), Fraction(0, 1)), (Fraction(24, 23), Fraction(100, 1))]

both time and value are Fraction object

now I have to build a data structure mapped in a way the first tuple is the one with in_time dict and second tuple to out_time dict

({'in_time': "" , 'in_value': ""} , {'out_time': "", 'out_value': ""})

i have also tried in a different way using list somehting like this:

container = [['in_time', 'in_value'] , ['out_time', 'out_value']]

dict(zip([objects for objects in container, [(point.time, point.value) for point in contol_points]]))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 1; 2 is required

I was hoping somehting like this would be possible: https://stackoverflow.com/a/33737067/9567948

Chang Zhao
  • 631
  • 2
  • 8
  • 24

2 Answers2

0

Ignoring Fractions and using ints to show one approach:

Given

control_points = [point(-1, 0), point(24, 100)]
container = [['in_time', 'in_value'] , ['out_time', 'out_value']]

Notice

{ container[0][0]:control_points[0].time, container[0][1] : control_points[0].value }

gives

{'in_time': -1, 'in_value': 0}

So, that's picked up the first item. If you enumerate over the container like this:

[{ c[0]:control_points[i].time, c[1] : control_points[i].value } for i,c in enumerate(container)]

You get this:

[{'in_time': -1, 'in_value': 0}, {'out_time': 24, 'out_value': 100}]

This does seem over the top for two points, but shows how to do dictionary comps and enumerate.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

Here is my attempt:

I use the * iterator in zip to access both the lists, but the ordering is incorrect, so I need to loop again to adjust the ordering.

from fractions import Fraction
container = [['in_time', 'in_value'] , ['out_time', 'out_value']]
control_points = [(Fraction(-1, 23), Fraction(0, 1)), (Fraction(24, 23), Fraction(100, 1))]

zipped_list = list(zip(*container,*control_points))
new_dict={}
i=0
for element in zipped_list:

    while((i+3)<=len(element)):
        print([element[i]], element[i+2])
        new_dict[element[i]]=element[i+2]
        i+=1
    i=0

print(new_dict)

Output: enter image description here

For Python 2:

from fractions import Fraction
container = [['in_time', 'in_value'] , ['out_time', 'out_value']]
control_points = [(Fraction(-1, 23), Fraction(0, 1)), (Fraction(24, 23), Fraction(100, 1))]


zipped_list = dict(zip([element for x in container for element in x],[element for y in control_points for element in y]))
print(zipped_list)

Output: enter image description here

Richard K Yu
  • 2,152
  • 3
  • 8
  • 21
  • Hello Richard, looks like `list(zip(*container,*control_points))` throwing Syntax error in python 2 – Chang Zhao May 10 '19 at 13:15
  • Hey Chang, it looks like in Python 2 that the * is used to unzip while the zip function already iterates through. [Python 2 documentation](https://docs.python.org/2/library/functions.html#zip). Check the [izip function](https://docs.python.org/2/library/itertools.html#itertools.izip) actually, I believe this is the equivalent but can't test it since I do not believe it is in Python 3. – Richard K Yu May 10 '19 at 13:20
  • I just added a solution that I think works in python 2. – Richard K Yu May 10 '19 at 13:59