Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83195 > unrolled thread
| Started by | flebber <flebber.crue@gmail.com> |
|---|---|
| First post | 2015-01-04 03:34 -0800 |
| Last post | 2015-01-04 21:22 +0000 |
| Articles | 15 — 10 participants |
Back to article view | Back to comp.lang.python
Help with map python 2 flebber <flebber.crue@gmail.com> - 2015-01-04 03:34 -0800
Re: Help with map python 2 Chris Angelico <rosuav@gmail.com> - 2015-01-04 22:47 +1100
Re: Help with map python 2 Alec Taylor <alec.taylor6@gmail.com> - 2015-01-04 22:47 +1100
Re: Help with map python 2 Alec Taylor <alec.taylor6@gmail.com> - 2015-01-04 22:48 +1100
Re: Help with map python 2 Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-01-04 14:08 +0200
Re: Help with map python 2 flebber <flebber.crue@gmail.com> - 2015-01-05 03:33 -0800
Re: Help with map python 2 Terry Reedy <tjreedy@udel.edu> - 2015-01-05 18:28 -0500
Re: Help with map python 2 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-06 12:30 +1100
Re: Help with map python 2 Peter Otten <__peter__@web.de> - 2015-01-04 17:33 +0100
Re: Help with map python 2 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-04 12:32 -0500
Re: Help with map python 2 Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-04 12:41 -0500
Re: Help with map python 2 Dan Stromberg <drsalists@gmail.com> - 2015-01-04 10:06 -0800
Re: Help with map python 2 Terry Reedy <tjreedy@udel.edu> - 2015-01-04 16:13 -0500
Re: Help with map python 2 flebber <flebber.crue@gmail.com> - 2015-01-05 03:37 -0800
Re: Help with map python 2 Sayth Renshaw <flebber.crue@gmail.com> - 2015-01-04 21:22 +0000
| From | flebber <flebber.crue@gmail.com> |
|---|---|
| Date | 2015-01-04 03:34 -0800 |
| Subject | Help with map python 2 |
| Message-ID | <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com> |
In repsonse to this question: Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6, -7, 8.
This is my solution it works but ugly.
series = range(2,100)
# answer = [(x,(y* -1)) for x, y in series[::2]]
# print(answer)
answer = []
for item in series:
if item % 2 != 0:
answer.append(item * -1)
else:
answer.append(item)
print(answer)
I know I should be better off doing this with map but cannot get it to work. I understand also that map returns a generator so this solution should only working in python2(correct me please if I am wrong).
In [6]: map?
Type: builtin_function_or_method
String Form:<built-in function map>
Namespace: Python builtin
Docstring:
map(function, sequence[, sequence, ...]) -> list
Just getting something wrong
list(map((lambda x: x * -1 if (x%2 != 0)), series))
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-01-04 22:47 +1100 |
| Message-ID | <mailman.17367.1420372032.18130.python-list@python.org> |
| In reply to | #83195 |
On Sun, Jan 4, 2015 at 10:34 PM, flebber <flebber.crue@gmail.com> wrote: > Just getting something wrong > list(map((lambda x: x * -1 if (x%2 != 0)), series)) Okay, and what happens when you run this? Do you get an exception? If so (and I fully expect you will), copy and paste the entire exception traceback and message. That's the best way to show what's going wrong. If you're not getting an exception, what do you get that's wrong? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Alec Taylor <alec.taylor6@gmail.com> |
|---|---|
| Date | 2015-01-04 22:47 +1100 |
| Message-ID | <mailman.17368.1420372055.18130.python-list@python.org> |
| In reply to | #83195 |
map(lambda i: -i if i&1==1 else i, xrange(2, 10)) On Sun, Jan 4, 2015 at 10:34 PM, flebber <flebber.crue@gmail.com> wrote: > In repsonse to this question: Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6, -7, 8. > > This is my solution it works but ugly. > > series = range(2,100) > # answer = [(x,(y* -1)) for x, y in series[::2]] > # print(answer) > answer = [] > for item in series: > if item % 2 != 0: > answer.append(item * -1) > else: > answer.append(item) > > print(answer) > > I know I should be better off doing this with map but cannot get it to work. I understand also that map returns a generator so this solution should only working in python2(correct me please if I am wrong). > > In [6]: map? > Type: builtin_function_or_method > String Form:<built-in function map> > Namespace: Python builtin > Docstring: > map(function, sequence[, sequence, ...]) -> list > > Just getting something wrong > list(map((lambda x: x * -1 if (x%2 != 0)), series)) > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Alec Taylor <alec.taylor6@gmail.com> |
|---|---|
| Date | 2015-01-04 22:48 +1100 |
| Message-ID | <mailman.17369.1420372135.18130.python-list@python.org> |
| In reply to | #83195 |
^To print the first 8. To print the first 100: map(lambda i: -i if i&1==1 else i, xrange(2, 102)) On Sun, Jan 4, 2015 at 10:47 PM, Alec Taylor <alec.taylor6@gmail.com> wrote: > map(lambda i: -i if i&1==1 else i, xrange(2, 10)) > > On Sun, Jan 4, 2015 at 10:34 PM, flebber <flebber.crue@gmail.com> wrote: >> In repsonse to this question: Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6, -7, 8. >> >> This is my solution it works but ugly. >> >> series = range(2,100) >> # answer = [(x,(y* -1)) for x, y in series[::2]] >> # print(answer) >> answer = [] >> for item in series: >> if item % 2 != 0: >> answer.append(item * -1) >> else: >> answer.append(item) >> >> print(answer) >> >> I know I should be better off doing this with map but cannot get it to work. I understand also that map returns a generator so this solution should only working in python2(correct me please if I am wrong). >> >> In [6]: map? >> Type: builtin_function_or_method >> String Form:<built-in function map> >> Namespace: Python builtin >> Docstring: >> map(function, sequence[, sequence, ...]) -> list >> >> Just getting something wrong >> list(map((lambda x: x * -1 if (x%2 != 0)), series)) >> -- >> https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2015-01-04 14:08 +0200 |
| Message-ID | <qotoaqe7nol.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #83195 |
flebber writes: > In repsonse to this question: Write a program that prints the first > 100 members of the sequence 2, -3, 4, -5, 6, -7, 8. > > This is my solution it works but ugly. Seems respectable to me, except that you are taking fewer than 100 elements. But I'd prefer the expressions that you are looking for. > series = range(2,100) > # answer = [(x,(y* -1)) for x, y in series[::2]] You could do what mathematicians do when they deal with alternating signs: they raise -1 to the power of the index to get an appropriate multiplier. >>> [ n * (-1) ** n for n in range(10) ] [0, -1, 2, -3, 4, -5, 6, -7, 8, -9] >>> Or you could do here what you attempt to do with map below. See below. > # print(answer) > answer = [] > for item in series: > if item % 2 != 0: > answer.append(item * -1) > else: > answer.append(item) > > print(answer) > > I know I should be better off doing this with map but cannot get it > to work. I understand also that map returns a generator so this > solution should only working in python2(correct me please if I am > wrong). > > In [6]: map? > Type: builtin_function_or_method > String Form:<built-in function map> > Namespace: Python builtin > Docstring: > map(function, sequence[, sequence, ...]) -> list > > Just getting something wrong > list(map((lambda x: x * -1 if (x%2 != 0)), series)) You are trying to use a binary expression. There are no binary expressions. Add an else branch to make it ternary: lambda x : x if x % 2 == 0 else -x But never mind the number of branches, the serious point is that you didn't specify a value for when the condition is not true. It doesn't make sense without that. There's nothing wrong with a list comprehension, or the corresponding generator expression if you want a generator. It's fine. Map's fine.
[toc] | [prev] | [next] | [standalone]
| From | flebber <flebber.crue@gmail.com> |
|---|---|
| Date | 2015-01-05 03:33 -0800 |
| Message-ID | <597df0f8-234a-468c-bb48-1954db491b3c@googlegroups.com> |
| In reply to | #83199 |
> You could do what mathematicians do when they deal with alternating > signs: they raise -1 to the power of the index to get an appropriate > multiplier. > > >>> [ n * (-1) ** n for n in range(10) ] > [0, -1, 2, -3, 4, -5, 6, -7, 8, -9] > >>> > > Or you could do here what you attempt to do with map below. See below. > > > You are trying to use a binary expression. There are no binary > expressions. Add an else branch to make it ternary: > > lambda x : x if x % 2 == 0 else -x > > But never mind the number of branches, the serious point is that you > didn't specify a value for when the condition is not true. It doesn't > make sense without that. > > There's nothing wrong with a list comprehension, or the corresponding > generator expression if you want a generator. It's fine. Map's fine. Thanks Jussi I really like the multiplier solution.
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2015-01-05 18:28 -0500 |
| Message-ID | <mailman.17406.1420500655.18130.python-list@python.org> |
| In reply to | #83228 |
On 1/5/2015 6:33 AM, flebber wrote: > >> You could do what mathematicians do when they deal with alternating >> signs: they raise -1 to the power of the index to get an appropriate >> multiplier. >> >> >>> [ n * (-1) ** n for n in range(10) ] >> [0, -1, 2, -3, 4, -5, 6, -7, 8, -9] Mathematicians operating in the timeless Platonic universe of mathemtical forms are not concerned with such droll issues as computation time. I think most regard '(-1)**n' as an abbreviation for '(-1 if odd(n) else 1)', where odd(n) is regarded as an O(1) operation, not as an indication to actually raise -1 to an arbitrarily large power. which could take an arbitrarily long time. >> Or you could do here what you attempt to do with map below. See below. >> > >> >> You are trying to use a binary expression. There are no binary >> expressions. Add an else branch to make it ternary: >> >> lambda x : x if x % 2 == 0 else -x >> >> But never mind the number of branches, the serious point is that you >> didn't specify a value for when the condition is not true. It doesn't >> make sense without that. >> >> There's nothing wrong with a list comprehension, or the corresponding >> generator expression if you want a generator. It's fine. Map's fine. > > Thanks Jussi I really like the multiplier solution. > -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2015-01-06 12:30 +1100 |
| Message-ID | <54ab3a9a$0$12980$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #83258 |
Terry Reedy wrote: > On 1/5/2015 6:33 AM, flebber wrote: >> >>> You could do what mathematicians do when they deal with alternating >>> signs: they raise -1 to the power of the index to get an appropriate >>> multiplier. >>> >>> >>> [ n * (-1) ** n for n in range(10) ] >>> [0, -1, 2, -3, 4, -5, 6, -7, 8, -9] > > Mathematicians operating in the timeless Platonic universe of > mathemtical forms are not concerned with such droll issues as > computation time. I think most regard '(-1)**n' as an abbreviation for > '(-1 if odd(n) else 1)', where odd(n) is regarded as an O(1) operation, > not as an indication to actually raise -1 to an arbitrarily large power. > which could take an arbitrarily long time. Theoretically it could, but in practical terms no programming language would use such a poor implementation. At worst, you should expect n**m to take no more than O(log m) multiplications, and in the specific case of n == 1 or -1, you might even hope for constant time. How does CPython go? [steve@ando ~]$ python2.7 -m timeit "(-1)**1000" 10000000 loops, best of 3: 0.0479 usec per loop [steve@ando ~]$ python2.7 -m timeit "(-1)**1000000000" 10000000 loops, best of 3: 0.0477 usec per loop But wait! Maybe I'm tripping over the keyhole optimizer. Let's try again: [steve@ando ~]$ python2.7 -m timeit -s "n = -1; m = 1000" "n**m" 1000000 loops, best of 3: 0.195 usec per loop [steve@ando ~]$ python2.7 -m timeit -s "n = -1; m = 1000000000" "n**m" 1000000 loops, best of 3: 0.447 usec per loop [steve@ando ~]$ python2.7 -m timeit -s "n = -1; m = 1000000000000000" "n**m" 100000 loops, best of 3: 9.32 usec per loop So it appears that CPython 2.7 eschews some possible ** optimizations, but even so, it's pretty fast. Increasing the power by a factor of one million only doubles the time, so long as the power is an int. Increasing it by another factor of a million makes the power a long, and the time taken increases by a factor of about 20. But in absolute terms, it's still very fast: less than 10 microseconds. How does Python 3.3 go? For small powers, it is slower than 2.7, but for large powers, it is faster. [steve@ando ~]$ python3.3 -m timeit -s "n = -1; m = 1000" "n**m" 1000000 loops, best of 3: 0.606 usec per loop [steve@ando ~]$ python3.3 -m timeit -s "n = -1; m = 1000000000" "n**m" 1000000 loops, best of 3: 1.16 usec per loop [steve@ando ~]$ python3.3 -m timeit -s "n = -1; m = 1000000000000000" "n**m" 1000000 loops, best of 3: 1.97 usec per loop Let's keep going: [steve@ando ~]$ python3.3 -m timeit -s "n = -1; m = 10**100 + 1" "n**m" 100000 loops, best of 3: 9.47 usec per loop [steve@ando ~]$ python3.3 -m timeit -s "n = -1; m = 10**1000 + 1" "n**m" 10000 loops, best of 3: 83.9 usec per loop I don't think that this will be a problem in practical terms. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-01-04 17:33 +0100 |
| Message-ID | <mailman.17374.1420389211.18130.python-list@python.org> |
| In reply to | #83195 |
flebber wrote: > In repsonse to this question: Write a program that prints the first 100 > members of the sequence 2, -3, 4, -5, 6, -7, 8. > > This is my solution it works but ugly. > > series = range(2,100) > # answer = [(x,(y* -1)) for x, y in series[::2]] > # print(answer) > answer = [] > for item in series: > if item % 2 != 0: > answer.append(item * -1) > else: > answer.append(item) > > print(answer) Hm, that's the only first 98 members. > > I know I should be better off doing this with map but cannot get it to > work. I understand also that map returns a generator so this solution > should only working in python2(correct me please if I am wrong). > > In [6]: map? > Type: builtin_function_or_method > String Form:<built-in function map> > Namespace: Python builtin > Docstring: > map(function, sequence[, sequence, ...]) -> list > > Just getting something wrong > list(map((lambda x: x * -1 if (x%2 != 0)), series)) Here's another way to look at the problem -- start with an infinite series and then slice it: >>> from itertools import * >>> list(islice(count(2), 10)) [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] Factor out the slicing to focus on the important stuff: >>> def first_ten(items): ... return list(islice(items, 10)) ... >>> first_ten(count(2)) Now add the alternating sign: >>> first_ten(cycle([1, -1])) [1, -1, 1, -1, 1, -1, 1, -1, 1, -1] Combine the two: >>> from operator import mul >>> first_ten(imap(mul, cycle([1, -1]), count(2))) [2, -3, 4, -5, 6, -7, 8, -9, 10, -11] It is important that you use itertools.imap() in Python 2 because the map() builtin would try to build the infinite list... Finally put the slicing back into the expression if you like: >>> list(islice(imap(mul, cycle([1, -1]), count(2)), 10)) [2, -3, 4, -5, 6, -7, 8, -9, 10, -11]
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2015-01-04 12:32 -0500 |
| Message-ID | <mailman.17377.1420392749.18130.python-list@python.org> |
| In reply to | #83195 |
On Sun, 4 Jan 2015 03:34:21 -0800 (PST), flebber <flebber.crue@gmail.com>
declaimed the following:
>In repsonse to this question: Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6, -7, 8.
>
>This is my solution it works but ugly.
>
<snip>
Let's see... a sequence of positive even integer values, interspersed with
the negative of the intervening odd integers...
>>> for i in range(50):
... j = 2 * (i + 1)
... print j, -(j + 1),
...
2 -3 4 -5 6 -7 8 -9 10 -11 12 -13 14 -15 16 -17 18 -19 20 -21 22 -23 24 -25
26 -27 28 -29 30 -31 32 -33 34 -35 36 -37 38 -39 40 -41 42 -43 44 -45 46
-47 48 -49 50 -51 52 -53 54 -55 56 -57 58 -59 60 -61 62 -63 64 -65 66 -67
68 -69 70 -71 72 -73 74 -75 76 -77 78 -79 80 -81 82 -83 84 -85 86 -87 88
-89 90 -91 92 -93 94 -95 96 -97 98 -99 100 -101
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2015-01-04 12:41 -0500 |
| Message-ID | <mailman.17378.1420393285.18130.python-list@python.org> |
| In reply to | #83195 |
On Sun, 04 Jan 2015 12:32:22 -0500, Dennis Lee Bieber
<wlfraed@ix.netcom.com> declaimed the following:
>On Sun, 4 Jan 2015 03:34:21 -0800 (PST), flebber <flebber.crue@gmail.com>
>declaimed the following:
>
<SNIP>
Talking to myself...
Yes, you did ask about using map()... But I started from your initial
statement: PRINT the first 100 elements in the sequence... Nothing in
"print..." requires building a list of values first, and the definition for
adjacent value pairs is easily generated given merely a stream of integer
values with no fancy/complex set operations.
Now -- if you wanted the first 99 values -- then some complexity creeps
in as you'd have to skip the final -(x+1) term.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2015-01-04 10:06 -0800 |
| Message-ID | <mailman.17380.1420394768.18130.python-list@python.org> |
| In reply to | #83195 |
I'm partial to:
#!/usr/local/cpython-3.4/bin/python
def gen_series(top=None):
'''
Generate the numbers from 2 to top, with alternating signs.
If top is None, generate forever.
If top is an integer, generate from 2 to top.
'''
sign = 1
if top is None:
number = 2
while True:
yield number * sign
number += 1
sign = -sign
else:
for number in range(2, top):
yield number * sign
sign = -sign
def main():
list_ = list(gen_series(100))
print(list_)
main()
It's not the shortest, but it's pretty clear what's going on, and
gen_series() is reusable.
On Sun, Jan 4, 2015 at 3:34 AM, flebber <flebber.crue@gmail.com> wrote:
> In repsonse to this question: Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6, -7, 8.
>
> This is my solution it works but ugly.
>
> series = range(2,100)
> # answer = [(x,(y* -1)) for x, y in series[::2]]
> # print(answer)
> answer = []
> for item in series:
> if item % 2 != 0:
> answer.append(item * -1)
> else:
> answer.append(item)
>
> print(answer)
>
> I know I should be better off doing this with map but cannot get it to work. I understand also that map returns a generator so this solution should only working in python2(correct me please if I am wrong).
>
> In [6]: map?
> Type: builtin_function_or_method
> String Form:<built-in function map>
> Namespace: Python builtin
> Docstring:
> map(function, sequence[, sequence, ...]) -> list
>
> Just getting something wrong
> list(map((lambda x: x * -1 if (x%2 != 0)), series))
> --
> https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2015-01-04 16:13 -0500 |
| Message-ID | <mailman.17382.1420406037.18130.python-list@python.org> |
| In reply to | #83195 |
On 1/4/2015 6:34 AM, flebber wrote:
> In repsonse to this question: Write a program that prints the first 100 members of the sequence 2, -3, 4, -5, 6, -7, 8.
>
> This is my solution it works but ugly.
>
> series = range(2,100)
> # answer = [(x,(y* -1)) for x, y in series[::2]]
> # print(answer)
> answer = []
> for item in series:
> if item % 2 != 0:
> answer.append(item * -1)
> else:
> answer.append(item)
for item in series:
if item % 2: # since result is a number, same as != 0
item *= -1
answer.append(item)
> print(answer)
>
> I know I should be better off doing this with map but cannot get it to work. I understand also that map returns a generator so this solution should only working in python2(correct me please if I am wrong).
>
> In [6]: map?
> Type: builtin_function_or_method
> String Form:<built-in function map>
> Namespace: Python builtin
> Docstring:
> map(function, sequence[, sequence, ...]) -> list
>
> Just getting something wrong
> list(map((lambda x: x * -1 if (x%2 != 0)), series))
In py2, map produces a list already. In any case, above is syntax error
without else clause.
map(lambda x: x * -1 if x%2 else x, series)
If you do not have a function already, a list comp is better.
[(-1*k if k%2 else k) for k in range(2, N)]
Change [] to () and you have a generator expression.
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | flebber <flebber.crue@gmail.com> |
|---|---|
| Date | 2015-01-05 03:37 -0800 |
| Message-ID | <f6e70150-5e6a-4b41-be79-53e888eb4f0d@googlegroups.com> |
| In reply to | #83220 |
> In py2, map produces a list already. In any case, above is syntax error > without else clause. > > map(lambda x: x * -1 if x%2 else x, series) > > If you do not have a function already, a list comp is better. > > [(-1*k if k%2 else k) for k in range(2, N)] > > Change [] to () and you have a generator expression. Thanks Terry [(-1*k if k%2 else k) for k in range(2, N)] is really short and I think pythonic being a list comprehension and works in py2 and py3. Sayth
[toc] | [prev] | [next] | [standalone]
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2015-01-04 21:22 +0000 |
| Message-ID | <mailman.17383.1420406548.18130.python-list@python.org> |
| In reply to | #83195 |
[Multipart message — attachments visible in raw view] — view raw
Thank you for those solutions so varied. Am going to have a party with them to see what works best for me. Strange is there a way too use lousy comprehension without returning just the matching odd values? Sayth On Mon, 5 Jan 2015 5:06 AM Dan Stromberg <drsalists@gmail.com> wrote: > I'm partial to: > > #!/usr/local/cpython-3.4/bin/python > > def gen_series(top=None): > ''' > Generate the numbers from 2 to top, with alternating signs. > If top is None, generate forever. > If top is an integer, generate from 2 to top. > ''' > sign = 1 > if top is None: > number = 2 > while True: > yield number * sign > number += 1 > sign = -sign > else: > for number in range(2, top): > yield number * sign > sign = -sign > > > def main(): > list_ = list(gen_series(100)) > print(list_) > > main() > > > It's not the shortest, but it's pretty clear what's going on, and > gen_series() is reusable. > > > On Sun, Jan 4, 2015 at 3:34 AM, flebber <flebber.crue@gmail.com> wrote: > > In repsonse to this question: Write a program that prints the first 100 > members of the sequence 2, -3, 4, -5, 6, -7, 8. > > > > This is my solution it works but ugly. > > > > series = range(2,100) > > # answer = [(x,(y* -1)) for x, y in series[::2]] > > # print(answer) > > answer = [] > > for item in series: > > if item % 2 != 0: > > answer.append(item * -1) > > else: > > answer.append(item) > > > > print(answer) > > > > I know I should be better off doing this with map but cannot get it to > work. I understand also that map returns a generator so this solution > should only working in python2(correct me please if I am wrong). > > > > In [6]: map? > > Type: builtin_function_or_method > > String Form:<built-in function map> > > Namespace: Python builtin > > Docstring: > > map(function, sequence[, sequence, ...]) -> list > > > > Just getting something wrong > > list(map((lambda x: x * -1 if (x%2 != 0)), series)) > > -- > > https://mail.python.org/mailman/listinfo/python-list >
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web