0

I edited this to specifics: see part B for specifics: updated agian, a dictionary does not work because it's values aren't' indexed. *''' I have a bunch of lists names that vary slightly. And rather than writing the code that manipulates these lists multiple times, how can I input the name of the list to open? so: (and the following IS NOT code:)

list1: txt_masterlist=[]
list2: img_masterlist=[]
list3: png_masterlist=[]
list4: vid_masterlist=[]

etc... for example, to find the cardinality of lists 1-4 i'd need: len(img_masterlist) but 4x for each uniquely named list. I could use a def with a .split('_')[1] to return the 2nd part of the name, but id how to call, a list. Hope this makes sense, really what I'm asking is how to return fstring as a variable name, and call this variable in code. Because then I could create list{i}. '''*

partB:

a=print('this is code')

how can I do this: in python, or via numpy. its just a folder structure. i just don't know how to create a listi where i∈R (all real numbers)


type(a,bi,ci,di)=these are all lists 
 


a ⊃ bi
bi ⊃ ci

or specifically:

a ⊃ b1 b2 b3...
b1 ⊃ c1 c2 c3....
c1∋x
c2∋y

so y∈c2⊂b1⊂a != y∈c2⊂b2⊂a because b2 != b1

to do this in python i need to create listi=[]

its super important to note that ( c2 ⊃ b1 ) != ( c2 ⊃ b2) c2 != c2 (in name only)

  • Could you describe it more clearly? – s.paszko Nov 14 '22 at 22:59
  • 5
    You should never get a user to provide input which should then be matched to variable names (like the names of list variables). If you need data in a data structure where a specific element should be selected based on user input, use a dictionary instead, so that you can access the appropriate list in the dictionary by using the user input as a key. – Grismar Nov 14 '22 at 23:01
  • 1
    You can put lists into a list, `whatever=[txt_masterlist,img_masterlist,png_masterlist,vid_masterlist]`, and then you can access `img_masterlist` both as `img_masterlist` and `whatever[1]`. – tevemadar Nov 14 '22 at 23:02
  • How did you get in the first place all these list with similar names and on which you may want to apply similar operations? Probably you should be using a dictionary instead. What you want to do can be done with [this answer](https://stackoverflow.com/a/1373201/15032126). But note that this is not a good practice and in the long term a cleaner code will pay more. – Ignatius Reilly Nov 14 '22 at 23:03
  • "I have a bunch of lists names that vary slightly" don't do that. Put your lists into *a container*, like *another list*, or a dict. Then you can manipulate them as a group – juanpa.arrivillaga Nov 14 '22 at 23:07
  • i updated my post for specifics. I need to: "create ith list in python" – Marlon D. Alessandra Nov 14 '22 at 23:37
  • update, i need to reference the lists and sublists(subsets) via list indes, dictionaires do not have indexes, so to call ith element, i'd need ith index – Marlon D. Alessandra Nov 15 '22 at 00:14
  • 1
    You seem to be asking a completely different question now, though one which isn't very clear. In any event -- a dictionary key doesn't have to be a string. It can be numerical. – John Coleman Nov 15 '22 at 00:27

1 Answers1

2

You should never get a user to provide input which should then be matched to variable names (like the names of list variables). If you need data in a data structure where a specific element should be selected based on user input, use a dictionary instead, so that you can access the appropriate list in the dictionary by using the user input as a key.

Something like:

masterlists = {'txt': [], 'img': [], 'png': [], 'vid': []}

val = input('Enter a value to add: ')
list_name = input('Enter a list to add it to: ')

masterlists[list_name].append(val)

print(masterlists)

Example run:

Enter a value to add: myfile.csv
Enter a list to add it to: txt
{'txt': ['myfile.csv'], 'img': [], 'png': [], 'vid': []}

Note that this doesn't take into account what should happen if the user enters a list name that doesn't exist yet, etc. But the basic principle should be clear.

Grismar
  • 27,561
  • 4
  • 31
  • 54