简体   繁体   中英

pass two different functions on even and odd elements in a list

I want to pass two different functions on a list. One is for odd elements and the other one is for even element.

The list looks like this: `

lst = [2856064, 5819, 2856936, 8557, 2858224, 7744, 2859392, 8333, 2859718, 7588, 2860220, 10556, 2861336, 9298, 2863080, 6912, 2865340, 7929, 2866040, 9513, 2866350, 7828, 2867228, 10795, 2868044, 12431, 2869740, 8922, 2870080, 10792, 2870392, 7382, 2871584, 6873, 2872800, 7271, 2873864, 6776, 2875664, -1, 2876294, 8495]

For the even element I want to convert it to UTC format timestamp. My current code: [datetime.utcfromtimestamp((time+21564000)*60).strftime('%Y%m%d') for time in lst[::2]] On the other hand, the even element just stay the same unless there is -1, return 0. My current code:

[0 if tag==-1 else tag for tag in lst[1::2]]

Additionally, I also just want to get the information after '20160618'. That is to say, my expect result looks like this [20160618, 7271, 20160618, 6776, 20160619, 0, 20160620, 8495]

ps Is it possible to do it in one list comprehension?

Here's how to solve this generally using a comprehension:

# a is some list
# f1 is a function that's supposed to map the odd elements
# f2 is a function that's supposed to map the even elements
[f1(x) if i%2 else f2(x) for i, x in enumerate(a)]

You can use enumerate() and % 2 with a ternary expression to do either thing -replacing -1 by 0 can be done using max(x,0) because -1 is the only negative value you got:

from datetime import datetime

lst = [2856064, 5819, 2856936, 8557, 2858224, 7744, 2859392, 8333, 2859718, 
      7588, 2860220, 10556, 2861336, 9298, 2863080, 6912, 2865340, 7929, 2866040, 
      9513, 2866350, 7828, 2867228, 10795, 2868044, 12431, 2869740, 8922, 2870080,
      10792, 2870392, 7382, 2871584, 6873, 2872800, 7271, 2873864, 6776, 2875664, 
      -1, 2876294, 8495]

k = [datetime.utcfromtimestamp((x+21564000)*60).strftime('%Y%m%d') 
     if i%2 == 0 else max(0,x) for i,x in enumerate(lst)]

print(k)

Output:

['20160606', 5819, '20160606', 8557, '20160607', 7744, '20160608', 8333, 
 '20160608', 7588, '20160609', 10556, '20160610', 9298, '20160611', 6912, 
 '20160612', 7929, '20160613', 9513, '20160613', 7828, '20160614', 10795, 
 '20160614', 12431, '20160615', 8922, '20160616', 10792, '20160616', 7382, 
 '20160617', 6873, '20160618', 7271, '20160618', 6776, '20160619', 0, 
 '20160620', 8495]

To get the part after 20160618 get its index and slice it:

b = k[ k.index('20160618'):]
print(b) # ['20160618', 7271, '20160618', 6776, '20160619', 0, '20160620', 8495]

Your date-strings are well formed for lexicografical comparison '20170101' is > then '20161231' -so you can use lexicographical comparison to find "dates" after "a date not in the list":

t= ['20160606', 5819, '20160606', 8557, '20160607', 7744, '20160608', 8333, 
 '20160608', 7588, '20160609', 10556, '20160610', 9298, '20160611', 6912, 
 '20160612', 7929, '20160613', 9513, '20160613', 7828, '20160614', 10795, 
 '20160614', 12431, '20160615', 8922, '20160616', 10792, '20160616', 7382, 
 '20160617', 6873, '20160618', 7271, '20160618', 6776, '20160619', 0, 
 '20160620', 8495, '20170618', 7271, '20180618', 7271,]

index = 0
for idx, k in enumerate(t):
    if idx % 2 == 0 and k > "20160620":
        index = idx
        break

print(t[index:]) # ['20170618', 7271, '20180618', 7271]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM