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?

3
  • Look into something called meta programming. I just haven't done it in Python. Commented May 7, 2014 at 0:57
  • could you let us know what cehw_list is and author_list and what search_list returns. Commented May 7, 2014 at 1:03
  • @Back2Basics I've added the information you requested. Commented May 7, 2014 at 1:09

2 Answers 2

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': []}

Sign up to request clarification or add additional context in comments.

4 Comments

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?)
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.
@ReblochonMasque , ctd:Check out using 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:!)
@cplewis: Thanks, that answers my question and satisfies my curiosity (I bet you learned something too :)). Here, have some good rep.
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]

1 Comment

Thanks for your suggestion but I lose the uniqueness of author. Each author is different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.