grigs
grigs

Reputation: 1150

Python - Iterating through python list using another list

I'm stuck on the following problem:

I have a list with a ton of duplicative data. This includes entry numbers and names.

The following gives me a list of unique (non duplicative) names of people from the Data2014 table:

   tablequery = c.execute("SELECT * FROM Data2014")
   tablequery_results = list(people2014)
   people2014_count = len(tablequery_results)
   people2014_list = []
    for i in tablequery_results:
        if i[1] not in people2014_list:
            people2014_list.append(i[1])
    people2014_count = len(people2014_list)
    # for i in people2014_list:
    #     print(i)

Now that I have a list of people. I need to iterate through tablequery_results again, however, this time I need to find the number of unique entry numbers each person has. There are tons of duplicates in the tablequery_results list. Without creating a block of code for each individual person's name, is there a way to iterate through tablequery_results using the names from people2014_list as the unique identifier? I can replicate the code from above to give me a list of unique entry numbers, but I can't seem to match the names with the unique entry numbers.

Please let me know if that does not make sense.

Thanks in advance!

Upvotes: 1

Views: 52

Answers (1)

grigs
grigs

Reputation: 1150

I discovered my answer after delving into SQL a bit more. This gives me a list with two columns. The person's name in the first column, and then the numbers of entries that person has in the second column.

def people_data():
    data_fetch = c.execute("SELECT person, COUNT(*) AS `NUM` FROM Data2014 WHERE ACTION='UPDATED' GROUP BY Person ORDER BY NUM DESC")
    people_field_results = list(data_fetch)
    people_field_results_count = len(people_field_results)
    for i in people_field_results:
        print(i)
    print(people_field_results_count)

Upvotes: 1

Related Questions