0

is there a neat way to include conditions in list comprehensions in order to do the following:

index = [y for y, s in enumerate(data_time_filtered) if next0 in s]

I'd like to add the following conditions in the index definition above:

if next0 in s:
    data_filtered.append(data_time_filtered[index[0]])

else:
    missing_data.append(next0)

currently what's happening in my code is:

IndexError: list index out of range

when the value can not be found. Is it more efficient to handle it with else and if conditions or is there another / better way for error handling in this case?

EXTENSION:

data_time_filtered is a list of strings like:

https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/002/MYD021KM.A2018002.1330.006.2018003152138.hdf
https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/004/MYD021KM.A2018004.1330.006.2018005220236.hdf
https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/006/MYD021KM.A2018006.1330.006.2018007165439.hdf
https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/009/MYD021KM.A2018009.1330.006.2018010190624.hdf

next0 are strings in the form of: /XXX/ for example /002/

The index function is looking for the line in data_time_filtered where next0 appears and is returning a index which is used in order to extract that line and append it to a different list.

The problem is that sometimes, the string given by next0 is not contained in the list creating the error message above.

What I would like is:

If index comes a cross such a number it should append this number in a missing_data list instead of breaking and producing the error.

UPDATE:

i tried this:

try:
    index = [y for y, s in enumerate(data_time_filtered) if next0 in s]
    data_filtered.append(data_time_filtered[index[0]])

except IndexError:
    missing_data.append(next0)

and it worked :). However, in a later stage on a different line an IndexError is occurring. still checking how to sort this out

Shaun
  • 461
  • 3
  • 5
  • 22
  • You have two different lists here to create – Sheldore Sep 15 '18 at 13:02
  • Possible duplicate of https://stackoverflow.com/questions/4260280/if-else-in-pythons-list-comprehension – Sruthi Sep 15 '18 at 13:04
  • @Bazingaa you mean the missing_data list? yes of course. But I was wondering if it's possible to add the if and else conditions all within the index definition – Shaun Sep 15 '18 at 13:04
  • could you please add some example list of values and desired output you want. It will easy to understand your requirement. – Vijay Anand Pandian Sep 15 '18 at 13:11
  • 2
    Currently it is unclear what you exactly want. Provide some sample input/output. I think what you want is a single line list comprehension to generate two different lists. I don't think it can be done because on the left hand side of the list comprehension code, you will have one list name. – Sheldore Sep 15 '18 at 13:13
  • I don't see where next0 is being defined, so that might be why your lists are empty and giving you the error. – user1209675 Sep 15 '18 at 13:22
  • @user1209675: the lists are not empty. the code works until index is looking for a string like /XXX/ but cant find it in data_time_filtered, producing the error. – Shaun Sep 15 '18 at 13:25

3 Answers3

0

I don't quite get what you're trying to do with this code, but maybe this will get you a bit closer.

index = [data_filtered.append(data_time_filtered[s]) if next0 in s else missing_data.append(next0) for s in data_time_filtered]
user1209675
  • 296
  • 7
  • 18
0

I have tried to get your requirement - Let me know if the below code helps or not,

data_time_filtered = [
    'https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/002/MYD021KM.A2018002.1330.006.2018003152138.hdf',
    'https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/004/MYD021KM.A2018004.1330.006.2018005220236.hdf',
    'https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/006/MYD021KM.A2018006.1330.006.2018007165439.hdf',
    'https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/6/MYD021KM/2018/009/MYD021KM.A2018009.1330.006.2018010190624.hdf']

# combinations of exist & non-exist values
next0_list = ['/002/', '/004/', '/111/', '/222/']
data_filtered = list()
missing_data = list()

# for next0 in next0_list:
#     index = [data_filtered.append(s) for s in data_time_filtered if next0 in s]
#     if not index:
#         missing_data.append(next0)


index = [missing_data.append(next0) for next0 in next0_list if
         not [data_filtered.append(s) for s in data_time_filtered if next0 in s]]

print(missing_data)
print(data_filtered)
Vijay Anand Pandian
  • 1,027
  • 11
  • 23
0

Instead of putting the conditions into the definition of index, it turns out that the error handling like:

try:
    index = [y for y, s in enumerate(data_time_filtered) if next0 in s]
    data_filtered.append(data_time_filtered[index[0]])

except IndexError:
    missing_data.append(next0)

is more practical as one can include further conditions or loops in the except IndexError part.

Shaun
  • 461
  • 3
  • 5
  • 22