0

For example I've created a set of list:

LIST_chr1=[]
LIST_chr2=[]
LIST_chr3=[]
...
...

Now when processing the text, for certain line, if I get "chr2", then I need to use LIST_chr2 thus select this list, how should I point to this specific list according to the list name?

Thanks

0

2 Answers 2

5

Usually, you don't want to do something like this ...

You could try:

vname = 'chr3'
list_I_want = globals()['LIST_'+vname]

Although, usually the MUCH BETTER way to do something like this is to store the lists in a dict in the first place

my_list_dict = dict(LIST_chr1=[],LIST_chr2=[],LIST_chr3=[],...)

and then:

list_I_want = my_list_dict['LIST_'+vname]
Sign up to request clarification or add additional context in comments.

Comments

1

This seems like a better job for a list of lists or a dictionary. For example, you could just use a structure like this:

>>> lists = [["list 1","contents"],[],[]]
>>> lists[0]
['list 1', 'contents']

(where the elements 'list 1' and 'contents' are the legitimate elements of LIST_chr1).

To be clear, if you need to access the lists by some identifier, use a dictionary as in the other example, but if you are just getting the list based on a specific number, this is the way to go.

Comments

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.