0

I would like to create a bunch of empty lists with names such as:

author1_count = []
author2_count = []
...
...

and so on...but a priori I do not know how many lists I need to generate.

Answers to question similar this one suggest to create a dictionary as in (How to create multiple (but individual) empty lists in Python?) or an array of lists. However, I wish to append values to the list as in:

def search_list(alist, aname):
    count = 0
    author_index = 0
    author_list = alist 
    author_name = aname
    for author in author_list:
        if author == author_name:
            author_index = author_list.index(author)+1
            count = 1
    return count, author_index

cehw_list = ["Ford, Eric", "Mustang, Jason", "BMW, James", "Mercedes, Megan"]

  author_list = []
  for author in authors:
  this_author = author.encode('ascii', 'ignore')
  author_list.append(this_author)
# Find if the author is in the authorlist

for cehw in cehw_list:
  if cehw == cehw_list[0]:
    count0, position0 = search_list(author_list, cehw)
    author1_count.append(count0)

  elif cehw == cehw_list[1]:
    count1, position1 = search_list(author_list, cehw)
    author2_count.append(count1)
...
...

Any idea how to create such distinct lists. Is there an elegant way to do this?

Community
  • 1
  • 1
Rohit
  • 5,840
  • 13
  • 42
  • 65

2 Answers2

2

Dictionaries! You only need to be more specific when appending values, e.g.

author_lists = {}

for i in range(3):
    author_lists['an'+str(i)] = []


author_lists

{'an0': [], 'an1': [], 'an2': []}

author_lists['an0'].append('foo')

author_lists

{'an0': ['foo'], 'an1': [], 'an2': []}

cphlewis
  • 15,759
  • 4
  • 46
  • 55
  • How would you proceed to be able to assign the created lists to a variable named after the dictionary key? - I think this is the meta-programming Justin Wood mentioned above ---> in the example in reference, that would mean: an0 = ['foo'], an1 = [], an2 = [] (should I open a new thread with this question?) – Reblochon Masque May 07 '14 at 02:00
  • bit simple for a new thread, I think. I wouldn't bother pulling out new variables if I didn't have them already: anywhere you could refer to `author1_count` (in the original) you can now refer to `author_lists['an1']`. Etc. Metaprogramming is overkill here. – cphlewis May 07 '14 at 02:07
  • 1
    @ReblochonMasque , ctd:Check out [using a string as a variable name](http://stackoverflow.com/questions/21896326/usng-the-value-of-a-string-as-a-variable-name) but *especially* note that most of the answers include "Don't actually do this!" Instead consider iterating over `for k in author_lists.keys():` (compare to `for k in author_lists:`!) – cphlewis May 07 '14 at 02:16
  • @cplewis: Thanks, that answers my question and satisfies my curiosity (I bet you learned something too :)). Here, have some good rep. – Reblochon Masque May 07 '14 at 03:03
0

You should be able to use a dictionary still.

data = {}
for cehw in cehw_list:
    count0, position0 = search_list(author_list, cehw)
    # Or whatever property on cehw that has the unique identifier
    if cehw in data:
        data[cehw].append(count0)
    else:
        data[cehw] = [count0]
schillingt
  • 13,493
  • 2
  • 32
  • 34
  • Thanks for your suggestion but I lose the uniqueness of author. Each author is different. – Rohit May 07 '14 at 01:01