-2

If I have a list like this (Notice that it skips one after 5, 12, 19, 26 .. etc. So 6, 13, 20, 27 are all missing) :

lst1 = [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61]

And I have a list like this (this represents which skips)

lst2 = [0,2] # this means the 0'st skip and 2nd skip which is 13)

And combining the lst1 and lst2,

I want to make a new lst3 that has [0, 1, 2, 3, 4, 5, 14, 15, 16, 17, 18, 19]

I don't know if my explanation made sense, but I basically want to use values from two lists and make a new list. This is what I've tried:

lst1 = [0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61]

def colorChecker(color):
    for c in color: 
    return 6 + 7 * color

lst3 = [ p for p in lst1 if p < colorChecker([0,2])] 

This only seems to work for colorChecker[0], not colorChecker[1].

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Blee
  • 87
  • 2
  • 12
  • 3
    So...what have you tried? – Marcin Dec 09 '14 at 18:07
  • 3
    This question appears to be off-topic because it is about getting us to show you teh codez – Marcin Dec 09 '14 at 18:08
  • 1
    lst 3= [ p for p in playablePieces if p < colorChecker(playingColors)] – Blee Dec 09 '14 at 18:10
  • @jonrsharpe please check the edited main thread! Thanks – Blee Dec 09 '14 at 18:14
  • Somewhere you are probably going to have to have `for whatever in [0, 2]`. You can't just pass a list to a function and expect it to go *"Oh, I guess they mean call the function recursively"*. Split this into multiple steps: 1. Break up `lst1` into contiguous sections; 2. Index into the new list of lists to get the parts you need; and 3. Join them back together. Don't try to put it all in one line, that helps nobody. – jonrsharpe Dec 09 '14 at 18:15
  • *Sigh* you can only `return` **once** from a function. Once you do that, wherever you were in whatever loops, it's over. Stop trying to do this in a single line. – jonrsharpe Dec 09 '14 at 18:20
  • @jonrsharpe please check again! – Blee Dec 09 '14 at 18:20

2 Answers2

1

If you already know where the missing elements will be, there's no need at all for lst1. Your code becomes as simple as:

>>> lst2 = [0, 2]
>>> lst3 = []
>>> for i in lst2:
        lst3.extend(range(7*i, 7*i+6))


>>> lst3
[0, 1, 2, 3, 4, 5, 14, 15, 16, 17, 18, 19]
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Good point about using `range()`; I foolishly assumed that the given definition of `lst1` was just an absurdly minimized example. :-} – Frerich Raabe Dec 09 '14 at 18:29
0

Since each element in lst2 appears to reference a group of 6 elements from lst1, I'd first create a list of those chunks like

[lst1[i*6:(i+1)*6] for i in lst2]

...which evaluates to

== [lst1[0*6:(0+1)*6], lst1[2*6:(2+1)*6]]
== [lst1[0:6], lst1[12:18]]
== [[0, 1, 2, 3, 4, 5], [14, 15, 16, 17, 18, 19]]

Finally, you can just flatten that list to get your result.

Community
  • 1
  • 1
Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207