0

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

  • By "sum of list" do you mean the sum of the file lengths of the files listed? Or perhaps the sum of the lengths of the file names? – tdelaney Jul 03 '22 at 23:42
  • Are you looking for `os.stat(f[1]).st_size`? – Brian61354270 Jul 03 '22 at 23:42
  • Okay, so you want to get the `[0]` element (not `[1]`) *of each inner list*? Think about the problem carefully. You want to iterate over `segment_upload_list`, right? What will each `f` element look like? How could you transform that into the value you want to use in the `os.stat(...).st_size` call? Now, put the pieces together. – Karl Knechtel Jul 03 '22 at 23:43
  • @KarlKnechtel that's right! I just want to sum the local paths –  Jul 03 '22 at 23:43
  • As already described by @Brian I guess, set the position for each, but I guess it has to be os.stat(f[0]).st_size not 1 –  Jul 03 '22 at 23:47

2 Answers2

1

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)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0

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"]
AboAmmar
  • 5,439
  • 2
  • 13
  • 24