Create a list from Python list_1 to dlist_n and collect the first value for each list.

Asked 1 weeks ago, Updated 1 weeks ago, 2 views

How do I retrieve a specific list using variable names in Python?

setattr(mod, 'dlist_{}'.format(i+1), list(map(int,input().split())))

With this code, you want to create a list from dlist_1 to dlist_n and then add a list within the list with a population.

for i in range(0,n):
    What code should be included in the wlist.append(""?""")

For example, you want to append the first value of each list from the list from dlist_1 to dlist_n among the saved lists and add [1,3,5,7,9] to the wlist in this way.


2022-09-20 16:29

1 Answers

dlists = { k:[] for k in input().split() }

In this way, dlists[1], dlists[2], ... An empty list of the equals is created. dlist_1, .. Don't make the variable name dynamic like this.The dictionary in the list is sufficient.

for k, v in dlists.items():

The items() method is recommended when using a dictionary in a for statement. For questions, values() can be used, but it is often used because keys and values are often used together.

2022-09-20 16:29

If you have any answers or tips

© 2022 pinfo. All rights reserved.