Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I multiply elements in one list while providing a range in another

Tags:

python

list

I have two lists that I will later use do determine on how many pages of a document I'm looking at. The first list (l_name) contains the name of the document. The second list (l_depth) contains the number of pages I will look at, always starting from the first one.

The original lists look like this:

l_name = ['Doc_1', 'Doc_2', 'Doc_3']
l_depth = [1, 3, 2]

As I want to use a for loop to indicate each page I will be looking at

for d,p in zip(l_doc, l_page):
  open doc(d) on page(p)
  do stuff

I need the new lists to look like this:

l_doc = ['Doc_1', 'Doc_2', 'Doc_2', 'Doc_2', 'Doc_3', 'Doc_3']
l_page = [1, 1, 2, 3, 1, 2]

How can I multiply the names (l_name --> l_doc) based on the required depth and provide the range (l_depth --> l_page) also based on the depth?

like image 893
user9092346 Avatar asked Dec 05 '22 09:12

user9092346


1 Answers

You can get your list with comprehension:

[k for i, j in zip(l_name, l_depth) for k in [i]*j]
like image 155
zipa Avatar answered Jan 05 '23 00:01

zipa