0

I'm writing a tool to plot wich word was how many times used in a large number of rap genius lyrics.

I already extracted to two lists: one for the name of the word. the other one for the amount the word was used.

list_name=["I","you","the"]
list_count=[23,42,12]

the values at the same index are the pairs. I'm not sure if this is the most useful way but that's how I can do a bar chart representing every word and the count of it. Now I want to sort it by the highest count number. If I sort list_count I can't really transfer this to make the list_name in the same order as the list_count.

Any suggestions? I tried dictionarys but I couldn't find a way how they were useful.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Nic0lodeon
  • 77
  • 6
  • 2
    Put each pair into a container, then sort using the container. One common container is a tuple, e.g. `("I", 23)`. Then you just need to do `sorted(pairs, key=lambda x: x[1])`. – Mateen Ulhaq Jun 15 '20 at 23:35
  • 1
    How do I sort a list of tuples by one of its items? – Nic0lodeon Jun 15 '20 at 23:36
  • See the following post: https://stackoverflow.com/questions/3121979/how-to-sort-a-list-tuple-of-lists-tuples-by-the-element-at-a-given-index – ddastrodd Jun 15 '20 at 23:42

4 Answers4

2

Use pandas

import pandas as pd
import matplotlib.pyplot as plt

# create dataframe
df = pd.DataFrame({'name': list_name, 'count': list_count}).sort_values('count', ascending=False)

# plot
p = plt.bar(x='name', height='count', data=df)

enter image description here

Use numpy

  • list(zip(list_name, list_count)) zips the two list into a list of tuples
  • np.sort sorts by ascending (smallest to largest)
    • [::-1] reverses the array
import numpy as np

# create numpy array with dtypes
t = np.array(list(zip(list_name, list_count)), dtype = [('name', 'S10'), ('count', int)])

# sort array
t = np.sort(t, order=['count'])[::-1]

# plot
plt.bar(x=t['name'], height=t['count'])
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
1

you can create a tuple, sort by a value in the tuple and get your lists again afterwards. This is not optimal, but it suffices

eg.:

list_name=["I","you","the"]
list_count=[23,42,12]

aux = [(name, count) for name, count in zip(list_name, list_count)]
aux = sorted(aux, key=lambda k: k[1], reverse=True)

list_name=[x[0] for x in aux]
list_count=[x[0] for x in aux]

I suggest that you take a look at zip and tuples they are really handy

0

The way you have structured your data will make it difficult to perform this task. Have you considered instead creating tuples instead?

Your data above would look like this:

names_and_counts = [("I", 23), ("you", 42), ("the", 12)]

Then, you could sort the list above like so:

sorted_by_count = sorted(names_and_counts, key=lambda tup: tup[1])
ddastrodd
  • 710
  • 1
  • 10
  • 22
0

You can create a tuple using your lists.

list_name=["I","you","the"]
list_count=[23,42,12]
pairs = list(zip(list_name, list_count))

The output for pairs will look like this:

[('I', 23), ('you', 42), ('the', 12)]

You can now sort them based on values using:

sorted(pairs)

Your sorted list will look like.

[('the', 12), ('I', 23), ('you', 42)]
Abbas
  • 623
  • 4
  • 6