Home > Back-end >  Get sum of specific list elements
Get sum of specific list elements

Time:07-04

At my python application I want to get the sum of segment_upload_list but only for objects that are stored locally, so basically every entry starting with "/tmp/VODProcessing/". This is how my full list might look like:

[['/tmp/VODProcessing/output/1 Se Pone Mas/master.m3u8', "Music/D'can/Se Pone Mas/1 Se Pone Mas/master.m3u8"], ['/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0007.m4s', "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0007.m4s"], ['/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0022.m4s', "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0022.m4s"], ['/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0030.m4s', "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0030.m4s"], ['/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0001.m4s', "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0001.m4s"]]

I wanted to get the sum like this:

totalsize = sum([os.stat(f).st_size for f in segment_upload_list[0]])

but [0] does not really make much sense here. How can I sum all local paths here?

Thanks in advance

CodePudding user response:

Put the pieces together logically:

   [                         for      in                    ]

We want to iterate over the outer list; each of the elements that is in segment_upload_list has something useful for us, and we'll give it a name.

   [                         for pair in segment_upload_list]

Within each of those pairs, the first value is the local name, and the second value is the remote name. Indexes start at zero, so we are interested in pair[0]. (Alternately, we could unpack the pairs into two separate variables, and only use the first.) We want to... do something with that.

   [        pair[0]          for pair in segment_upload_list]

We know from the existing code what we want to do with it, in fact: get the st_size out of the os.stat result for the path.

   [os.stat(pair[0]).st_size for pair in segment_upload_list]

Finally, sum those values. We don't actually need to build a list; we can instead use a generator expression, and take advantage of the special syntax rule that lets us drop the redundant parentheses.

sum(os.stat(pair[0]).st_size for pair in segment_upload_list)

CodePudding user response:

Do you mean like this?

pth = [['/tmp/VODProcessing/output/1 Se Pone Mas/master.m3u8', "Music/D'can/Se Pone Mas/1 Se Pone Mas/master.m3u8"], ['/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0007.m4s', "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0007.m4s"], ['/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0022.m4s', "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0022.m4s"], ['/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0030.m4s', "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0030.m4s"], ['/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0001.m4s', "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0001.m4s"]]


print([i for p in pth for i in p if i.startswith('/tmp')])
['/tmp/VODProcessing/output/1 Se Pone Mas/master.m3u8', '/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0007.m4s', '/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0022.m4s', '/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0030.m4s', '/tmp/VODProcessing/output/1 Se Pone Mas/a-flac/f-0001.m4s']

Another nice trick:

print([p for p in sum(pth,[]) if p.startswith('/tmp')])

That sum(pth,[]) essentially flattens the nested lists to a single one.

For the Music path, you can do (provided that it is fixed at second position):

print([p[1] for p in pth])

["Music/D'can/Se Pone Mas/1 Se Pone Mas/master.m3u8", "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0007.m4s", "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0022.m4s", "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0030.m4s", "Music/D'can/Se Pone Mas/1 Se Pone Mas/a-flac/f-0001.m4s"]
  • Related