0

I have multiple lists of equal length I want to compare in order to append some words in a dictionary of list. I've already compared those lists to get the max number for each index.

maxlist = [2,9,6,4,8] #This is the list of max number from the three different lists

a_list = [2,1,4,2,8]
b_list = [1,9,6,3,4]
c_list = [0,3,2,4,1]

Now, I have another list of words of the same length:

words = ["boy", "girl", "git", "tall", "boss"]

What I want to do here is compare each list to maxlist, and if the number in maxlist is found in either of the three list at the same index, I want to create a dictionary of list that will append words to that specific list. So my end result will be like:

For index 0, maxlist is found in a_list, so I will have:

 {a_list: ["boy"]}

for index 1, maxlist is found in b_list, so I will have:

 {a_list: ["boy"], b_list: ["girl"]}

After comparing all each list with maxlist, I want to have:

 {a_list: ["boy", "boss"], b_list:["girl","git"], c_list: ["tall"]}

I used three list as an example here, but in my case I have 40 lists. Is there any effective way to implement this? I am currently stuck. Here is the code I'm currently writing:

 label_data = {}
 for i in range(len(maxlist)):
   if maxlist[i] > 1: #I don't want to consider a max of 1.
     if maxlist == a_list[i]:
        if a_list in label_data:
           label_data["a_list"].append(words[i])
        else:
           dates_dict["key"] = [words[i]]

Not sure if the above code will work fine, plus I have to keep constructing if function for all the lists. Is there any effective way to solve this, and please post your code.

Thanks

Eniola
  • 133
  • 10

1 Answers1

1

Usually when comparing corresponding elements in a list, zip is pretty helpful. A defaultdict from the collections module will also help with creating a dictionary of lists:

from collections import defaultdict

label_data = defaultdict(list)

words = ["boy", "girl", "git", "tall", "boss"]
a_list = [2,1,4,2,8]
b_list = [1,9,6,3,4]
c_list = [0,3,2,4,1]

# iterate over corresponding words and entries in your three lists
for word, a, b, c in zip(words, a_list, b_list, c_list):
    m = max(a, b, c)
    # check for the max value and append accordingly
    if m == a:
        label_data['a_list'].append(word)
    elif m == b:
        label_data['b_list'].append(word)
    else:
        label_data['c_list'].append(word)

Which outputs

defaultdict(<class 'list'>, {'a_list': ['boy', 'boss'], 'b_list': ['girl', 'git'], 'c_list': ['tall']})
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
  • another guideline for Eniola - when you're wanting to use your variable names as dict keys, you probably should refactor those variables so they're in a dict to begin with. – Kenny Ostrom Jul 07 '20 at 17:57
  • @KennyOstrom true, but I think the dictionary that OP is after solves this problem for the most part – C.Nivs Jul 07 '20 at 18:00
  • @C.Nivs, how can I exclude from my output? Any idea? – Eniola Jul 07 '20 at 18:03
  • Printing a dict (or defaultdict) is another question, and you should be able to manage it. You could just print the keys and values as you iterate over the dict. – Kenny Ostrom Jul 07 '20 at 19:08