python - compare two list of tuples for jinja2 -
so here's deal. have log files im using create email. im using jinja template lists contents of log files. i've decided add data metrics showing the change last days log. ok
my data kept csv file , load them list of tuples in form of [ ('string',int,int), (...) ] know how use list comprehension compare 'string' index, , if theyre equal, check last int in tuple. if integer greater, im adding small arrow showing increase, if lower, show decreased arrow.
so far have bits , pieces of want do. instance piece of code populates table in jinja template file
{% f,r,u in data %} <tr> <td class="tg-031e"><span style="color:blue;font-weight:bold">▲</span>{{f}}</td> <td class="tg-031e">{{r}}</td> <td class="tg-031e">{{u}}</td> </tr> {% endfor %}
i havent yet added conditional show down arrow if result of comparison less than.
i came bad function test comparison of lists. im not condifent in how works.
def change(l1, l2): inc = [x[0] x,y in zip(l1,l2) if x[0] == y[0] , x[2] > y[2] ] dec = [x[0] x,y in zip(l1,l2) if x[0] == y[0] , x[2] < y[2] ] yield inc, dec
what i'd way compare these 2 lists third integer , dynamically add span table illustrating either increase or decrease. thank , hope asked correctly.
your function little weird (why using yield
?), it's largely there; only, if understand correctly, want adding fourth field data, can like:
{% f,r,u, inc_dec in four_tuples %} <tr> <td class="tg-031e"><span style="color:blue;font- weight:bold">▲</span>{{f}}</td> <td class="tg-031e">{{r}}</td> <td class="tg-031e">{{u}}</td> <td> {% if inc_dec == -1 %} <!-- show decrease image --> {% else if inc_dec == 1 %} <!-- show increase image --> {% endif %} </td> </tr> {% endfor %}
if l1
, l2
new_list
, old_list
, respectively, should able like:
def diff_to_int(a, b): if < b: return -1 elif == b: return 0 else: return 1 def change(new_list, old_list): """ :type new_list: list[(str, int, int)] :type old_list: list[(str, int, int)] :rtype list[int] """ return [diff_to_int(new_tuple[2], old_tuple[2]) new_tuple, old_tuple in zip(new_list, old_list)] inc_dec_list = change(new_data, old_data)
then make each of 3-tuples 4-tuple field showing whether or not increase, decrease, or static, can do:
four_tuples = [(a, b, c, inc_dec_static) (a, b, c), inc_dec_static in zip(new_data, inc_dec_list)]
then pass template.
Comments
Post a Comment