Zanam - 9 months ago 66

Python Question

I have data in

`.csv`

`Valid Date MAX`

1/1/1995 51

1/2/1995 45

1/3/1995 48

1/4/1995 45

Another csv called 'Min.csv' looks like:

`Valid Date MIN`

1/2/1995 33

1/4/1995 31

1/5/1995 30

1/6/1995 39

I want two generate two dictionaries or any other suggested data structure so that I can have two separate variables Max and Min in python respectively as:

`Valid Date MAX`

1/2/1995 45

1/4/1995 45

Valid Date MIN

1/2/1995 33

1/4/1995 31

i.e. select the elements from Max and Min so that only the common elements are output.

I am thinking about using numpy.intersect1d, but that means I have to separately compare the Max and Min first on date column, find the index of common dates and then grab the second columns for Max and Min. This appears too complicated and I feel there are smarter ways to intersect two curves Max and Min.

Answer

The `set()`

builtin must be enough as follows:

```
>>> max = {"1/1/1995":"51", "1/2/1995":"45", "1/3/1995":"48", "1/4/1995":"45"}
>>> min = {"1/2/1995":"33", "1/4/1995":"31", "1/5/1995":"30", "1/6/1995":"39"}
>>> a = set(max)
>>> b = set(min)
>>> {x:max[x] for x in a.intersection(b)}
{'1/4/1995': '45', '1/2/1995': '45'}
>>> {x:min[x] for x in a.intersection(b)}
{'1/2/1995': '33', '1/4/1995': '31'}
```