1

I`ve a dictionary defined like this :

d = {"date": tuple(date),"open":tuple(open),"close":tuple(close),"min":tuple(min),"max":tuple(max),"MA":tuple(ma)}

Each one of those tuples contains a list of values ( same number of values for each tuple ), how can I iterate trough each value of paticular keys to compare if "close" is superior to "MA" ?

Finger twist
  • 3,546
  • 9
  • 42
  • 52
  • suggested edit: "same *number* of values for each tuple", instead of "same *amount* of values for each tuple". – Geoffrey Dec 10 '10 at 10:47

2 Answers2

1

what am I missing? d['close'] > d['MA']?

Edit: Re, your comments

[...] what I want to return is how many times one element of "close" is > to the matching element of MA . (same tuple index)

sum( pair[0] > pair[1] for pair in zip(d['close'], d['MA']) )
SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • yeah, that works ( I mean it returns True ) but what I want to return is how many times one element of "close" is > to the matching element of MA . (same tuple index) – Finger twist Dec 10 '10 at 04:14
1

From the Python docs:

Tuples and lists are compared lexicographically using comparison of corresponding elements. This means that to compare equal, each element must compare equal and the two sequences must be of the same type and have the same length.

If not equal, the sequences are ordered the same as their first differing elements. For example, cmp([1,2,x], [1,2,y]) returns the same as cmp(x,y). If the corresponding element does not exist, the shorter sequence is ordered first (for example, [1,2] < [1,2,3]).

So as @TokenMacGuy says, you can simply use d['close'] > d['MA'] to compare the respective tuples.

moinudin
  • 134,091
  • 45
  • 190
  • 216