1

Okay, i'm really stuck with that one-liner.

Yet tried to accomplish my task with zip and map, but none of it worked i want. So my question is: can i generate a list with a custom step like this?

>>wicked cool code snippet
>>[101, 105, 109, 115, 121]

The idea behind this is that i have the start of a sequence X equal to 101. Then i add 4, then again adding 4. Then i add 6 to the previous result and again i add 6.

I believe it should look like this mathematically speaking:

An = A1+4d,A2+4d,A3+6d, A4+6d.

UPD

Ok, let me make it more clear.

range(101, 120, 3) <-Classical arithmetical progression

[101, 104, 107, 110, 113, 116, 119] < - The output

What i need is a combination of two of them. Like add +4 to each element n-times, then add +6 to the last element of add 4 sequence n-times.

Hope, it's clearer now.

4
  • 1
    It's hard to answer your question in it's current form, because there is no obvious way the sequence should continue (should A5 and A6 use +8 instead of +6?) - if it continues at all. Commented Mar 18, 2013 at 9:14
  • Maybe do some functional operation on 101 and [4, 4, 6, 6]? I want to say fold using addition, except that goes all the way, so you'd have to do it in a range or something. Slightly beyond my brain ATM. Commented Mar 18, 2013 at 9:14
  • its not clear. what does 4 or 6 signify? length of the previous word? Commented Mar 18, 2013 at 9:15
  • You can't do that with just zip and map, some sort of accumulator must be involved. Commented Mar 18, 2013 at 9:22

3 Answers 3

2

A bit hardcoded:

In [42]: step=[0,3,6,11,16]

In [43]: [i+step[n] for n, i in enumerate(range(101, 106))]
Out[43]: [101, 105, 109, 115, 121]
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure if this is a wicked cool code snippet, but it returns your answer with one line of code:

 >>> map(lambda x: x+100 if x == 1 else (x+103 if x == 2 else (x+106 if x == 3 else (x+111 if x == 4 else x+116))), range(1,6))
 [101, 105, 109, 115, 121]

Comments

1

This works for me:

>>> reduce(lambda acc, i: acc + [acc[-1] + i], [4,4,6,6], [101])
[101, 105, 109, 115, 121]

Unfortunately, it is not exactly what you've asked for and I am afraid it just can't be done with map and zip alone, because these operations do not involve any kind of accumulator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.