cresendez744
cresendez744

Reputation: 91

How to find frequency of values in a list of lists and combine with another existing list by common value?

I have a nested list of music artists comprised of user inputs, lets say:

artists_list = [['A', 'B', 'C'],
                ['A', 'C', 'B'],
                ['B', 'A', 'D']]

I've also managed to create a separate list, based on order of input (not alphabetically), that assigns a genre to each unique artist in the above list:

artist_genre_list = [['A', 'Rock'],
                     ['B', 'Rap'],
                     ['C', 'Rock'],
                     ['D', 'Blues']]

How do I combine these two to make either a master list or dictionary including the frequency count similar to:

master_list = [['A', 'Rock', 3],
               ['B', 'Rap', 3],
               ['C', 'Rock', 2],
               ['D', 'Blues', 1]]

master_dict = {'A': {
                  'Genre': 'Rock',
                  'Frequency': 3},
               'B': {
                  'Genre': 'Rap',
                  'Frequency': 3},
               'C': {
                  'Genre': 'Rock',
                  'Frequency': 2},
               'D': {
                  'Genre': 'Blues',
                  'Frequency': 1}
               }

The order doesn't necessarily have to be alphabetical. Here is a sample of what I'm doing to create the first two lists:

# Counters
count = 1
new_artist_counter = 0

# Generate Lists
artists_input_list = []
aux_artists_list = []
aux_genre_list = []
aux_artists_genre_list = []

def merge(aux_artists_list, aux_genre_list):
    merged_list = [[aux_artists_list[i], aux_genre_list[i]] for i in range(0, 
                    len(aux_artists_list))]
    return merged_list

while count < 4:

    # Inputs
    a1_in = str(input("Artist 1: "))
    a2_in = str(input("Artist 2: "))
    a3_in = str(input("Artist 3: "))

    artists_input_list.append([a1_in, a2_in, a3_in])

    # Determines if new unique artist has been added and asks for it's genre
    while new_artist_counter < len(artists_input_list):

        for entry in artists_input_list:

             for artist in entry:

                 if artist not in aux_artists_list:
                     aux_artists_list.append(artist)
                     genre_input = input("What is "+artist+"'s genre? ")
                     aux_genre_list.append(genre_input)
                 else: continue

        new_artist_counter += 1  

    aux_artists_genre_list = merge(aux_artists_list, aux_genre_list)

    # Counter updates
    count += 1

print(artists_input_list)
print(aux_artists_genre_list)

Upvotes: 0

Views: 35

Answers (1)

Lucan
Lucan

Reputation: 3595

This is what I came up with. It first flattens your artist list, gets the frequencies of each item in the list then combines it with your genre list

from itertools import groupby, chain
import pprint

artists_list = [
  ['A', 'B', 'C'],
  ['A', 'C', 'B'],
  ['B', 'A', 'D']
]

artist_genre_list = [
  ['A', 'Rock'],
  ['B', 'Rap'],
  ['C', 'Rock'],
  ['D', 'Blues']
]

frequencies = {
  key: len(list(value)) for key,
  value in groupby(sorted(chain.from_iterable(artists_list)))
}

frequency = [{
    letter: {
      'Genre': genre,
      'Frequency': next((freq
        for key, freq in frequencies.items() if key is letter), 0)
    }
  }
  for letter, genre in artist_genre_list
]

pprint.pprint(frequency)

I used pprint just to make the output tidier, which shows as

[{'A': {'Frequency': 3, 'Genre': 'Rock'}},
 {'B': {'Frequency': 3, 'Genre': 'Rap'}},
 {'C': {'Frequency': 2, 'Genre': 'Rock'}},
 {'D': {'Frequency': 1, 'Genre': 'Blues'}}]

Upvotes: 1

Related Questions