Compare elements of two lists and calculate median value

Question:

I have a list of keywords:

list1 = ['key(1)', 'key(2)' ........, 'key(x)']

And another 2D list:

list2 = [['key1','str(11)','value(11)'],['key1','str(12)','value(12)'].....,['key(1)','str(1n)','value(1n)'],['key2','str(21)','value(21)'],...,['key(2)','str(2n)','value(2n)'],........., ['key(n)','str(n1)','value(n1)'],...,['key(n)','str(nn)','value(nn)']]

What I am trying to do is to calculate the Median of the values for each keyword from list 1 which is included in the elements of list 2 and the output would be like this:

output_list=[['key(1)',median(value(11),...value(1n)], ['key(2)',median(value(21),...value(2n)],.....,['key(x)',median(value(x1),...value(xn)]]

I started with an if statement:

import statistics
for i in range(0,len(list1)):
    for j in range (0,len(list2)):
        if list1[i] in list2[j]:
            print(list1[i],statistics.median(int(list2[j][2])))

I am trying to print the result but I am getting ‘int’ object is not iterable

Asked By: JeanJack

||

Answers:

median should receive an iterable containing all values for key, whereas you give it only one value.

list1 = ["key(1)", "key(2)"]
list2 = [
    ["key(1)", "str(11)", "11"],
    ["key(1)", "str(12)", "12"],
    ["key(1)", "str(1n)", "19"],
    ["key(2)", "str(21)", "21"],
    ["key(2)", "str(2n)", "23"],
    ["key(2)", "str(21)", "21"],
    ["key(2)", "str(2n)", "253"],
]


import statistics

def values_for_key(searched_list, searched_key):
    for key, string, value in searched_list:
        if key == searched_key:
            yield int(value)

# other solution:
# def values_for_key(searched_list, searched_key):
#     return (int(value) for key, string, value in searched_list if key == searched_key)


for key in list1:
    print(key, statistics.median(values_for_key(list2, key)))

Like said in a comment, this is not an efficient algorithm if your lists are big, you should consider another way of storing them.

Answered By: Gelineau
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.