user1774501
user1774501

Reputation: 23

Python - Subtract 2 lists from each other that are in a dictionary

So I have to take information from a large data file that has 14 properties (columns). Using this information I was able to take the data and combine it into a list of floats. I have to analyse it and was required to normalise the values (value - minvalue)/(maxvalue - minvalue). I then put the original value list into a dictionary with the normalised values to that they are still related.

I need to now take 2 different keys of this dictionary which correspond to 2 different lists of normalised values and then subtract them from each other for further analysis.

Sample of my dictionary info:

    (3.0, 13.73, 4.36, 2.26, 22.5, 88.0, 1.28, 0.47, 0.52, 1.15, 6.62, 0.78, 1.75, 520.0): 
    [0.7105263157894738, 0.7154150197628459, 0.4812834224598929, 0.6134020618556701, 0.1956521739130435, 0.10344827586206898, 0.02742616033755273, 0.7358490566037735, 0.2334384858044164, 0.4556313993174061, 0.2439024390243903, 0.1758241758241758, 0.17261055634807418]

there are over 100 similar entries

Using Python3 and no libraries apart from math Any help is appreciated but if you feel there is an easier way to do this please let me know.

Edit: I cannot use any imported libraries

Ill add in some of my code but I have to snip a large portion of it out as it is much too large to include in this post.

     for line in temp_file: 
        line = line.strip()                                         #remove white space
        line_list = line.split(",")                                 #split the list into components seperated by commas
        temp_list2 = []
        for item in line_list[0:]:                                 
              value_float = float(item)                             #make values currently of type string into type float
              temp_list2.append(value_float)
        tuple_list = tuple(temp_list2)                              #make each item into a seperate tuple and then list
        data_list.append(tuple_list)                                #these tuples in a master list data_list
  prop_elts = [(x[1:]) for x in data_list]

------snip-------- (here is just where i defined each of the columns and then calculated the normalised values)

   i =  0
  while i < len(data_list):
      all_props_templist = [prop1_list[i],prop2_list[i],prop3_list[i],prop4_list[i],prop5_list[i],prop6_list[i],prop7_list[i],prop8_list[i],prop9_list[i],prop10_list[i],prop11_list[i],prop12_list[i],prop13_list[i]] 
      all_properties.append(all_props_templist)  
      i = i + 1  
  my_data_Dictionary = {el1: el2 for el1, el2 in zip(data_list,all_properties )}

Upvotes: 2

Views: 1028

Answers (1)

unutbu
unutbu

Reputation: 879201

If data is your dict,

[a-b for a, b in zip(data[key1], data[key2])]

is a list whose elements are the diffference between the corresponding elements in data[key1] and data[key2].


PS. When you see numbered variable names:

  all_props_templist = [prop1_list[i],prop2_list[i],prop3_list[i],prop4_list[i],prop5_list[i],prop6_list[i],prop7_list[i],prop8_list[i],prop9_list[i],prop10_list[i],prop11_list[i],prop12_list[i],prop13_list[i]] 

know that the situation is crying out for a list with an index in place of the number:

all_props_templist = [prop_list[j][i] for j in range(13)]

Upvotes: 2

Related Questions