Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'none,': 0.07; 'none:': 0.07; 'odd': 0.07; 'string': 0.09; '-1)': 0.09; 'builtin': 0.09; 'dan': 0.09; 'main()': 0.09; 'skip:# 30': 0.09; 'cc:addr:python-list': 0.11; 'subject:Help': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; "'''": 0.16; '->': 0.16; 'cc:name:python list': 0.16; 'integer,': 0.16; 'main():': 0.16; 'sequence,': 0.16; 'true:': 0.16; 'values?': 0.16; 'wrong).': 0.16; '\xc2\xa0if': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'form:': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; '>': 0.26; 'function': 0.29; 'on,': 0.29; 'am,': 0.29; 'matching': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'work.': 0.31; 'getting': 0.31; 'prints': 0.31; 'question:': 0.31; 'url:python': 0.33; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'returning': 0.36; 'sequence': 0.36; 'yield': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'url:org': 0.36; 'should': 0.36; 'wrong': 0.37; 'too': 0.37; 'list': 0.37; 'clear': 0.37; 'thank': 0.38; 'url:mail': 0.40; 'numbers': 0.61; 'first': 0.61; '8bit%:10': 0.64; 'map': 0.64; 'to:addr:gmail.com': 0.65; 'series': 0.66; '100': 0.79; '2015': 0.84; 'partial': 0.84; 'repsonse': 0.84; 'top.': 0.84; 'forever.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:from:date:message-id:subject:to:cc :content-type; bh=ZRk68OSnNBnc2t57tp/zGi8+SKWG5ALaqNPc96hXJcg=; b=SmkuWbRSbLdX4eEk3lIedfd3QnIpHbyeNxJ7b62/C5tadCNHpu+wjo2OLTX+wTEhx2 2gsFBegB+9tmJrFwWdUo3KK0Kj+clC6cxZdryl3j1XLBCiS7qNio1szT8l0JRpuW0LIS 68mKXa6mTHeO4Zx+hzxuWu+I4F7KLcyazPB7E43cVHoeHK6bZQo30fej9W0IB8Zjk5f4 iSuvaHkq2PUbBYI4CbQgd0D/ml7r5G+wIcIZjOa8ow5xn9f0N5B5Jmdvtaije+kFKrGj 0L4srSqsRZxpos0KlhDhIuMXrbwpVxIKQOqon/VlE+tV8algxxN/tUnRqAgP7DQEBWUd 2rlw== X-Received: by 10.140.41.169 with SMTP id z38mr131888350qgz.56.1420406540261; Sun, 04 Jan 2015 13:22:20 -0800 (PST) MIME-Version: 1.0 References: <60e62263-6cd1-48b9-acbc-f1a0b947b8c9@googlegroups.com> From: Sayth Renshaw Date: Sun, 04 Jan 2015 21:22:19 +0000 Subject: Re: Help with map python 2 To: Dan Stromberg Content-Type: multipart/alternative; boundary=001a11c11dba44e291050bda2b86 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 169 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420406548 news.xs4all.nl 2924 [2001:888:2000:d::a6]:55542 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83221 --001a11c11dba44e291050bda2b86 Content-Type: text/plain; charset=UTF-8 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 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 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: > > 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 > --001a11c11dba44e291050bda2b86 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

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 r= eturning just the matching odd values?

Sayth


On Mon, 5 Jan 2015 5:06 AM=C2=A0Dan Stromber= g <drsalists@gmail.com> wr= ote:
I'm partial to:

#!/usr/local/cpython-3.4/bin/python

def gen_series(top=3DNone):
=C2=A0 =C2=A0 '''
=C2=A0 =C2=A0 Generate the numbers from 2 to top, with alternating signs. =C2=A0 =C2=A0 If top is None, generate forever.
=C2=A0 =C2=A0 If top is an integer, generate from 2 to top.
=C2=A0 =C2=A0 '''
=C2=A0 =C2=A0 sign =3D 1
=C2=A0 =C2=A0 if top is None:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 number =3D 2
=C2=A0 =C2=A0 =C2=A0 =C2=A0 while True:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 yield number * sign
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 number +=3D 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sign =3D -sign
=C2=A0 =C2=A0 else:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 for number in range(2, top):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 yield number * sign
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 sign =3D -sign


def main():
=C2=A0 =C2=A0 list_ =3D list(gen_series(100))
=C2=A0 =C2=A0 print(list_)

main()


It's not the shortest, but it's pretty clear what's going on, a= nd
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 10= 0 members of the sequence 2, -3, 4, -5, 6, -7, 8.
>
> This is my solution it works but ugly.
>
> series =3D range(2,100)
> # answer =3D [(x,(y* -1)) for x, y in series[::2]]
> # print(answer)
> answer =3D []
> for item in series:
>=C2=A0 =C2=A0 =C2=A0if item % 2 !=3D 0:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0answer.append(item * -1)
>=C2=A0 =C2=A0 =C2=A0else:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0answer.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 shou= ld only working in python2(correct me please if I am wrong).
>
> In [6]: map?
> Type:=C2=A0 =C2=A0 =C2=A0 =C2=A0builtin_function_or_method
> String Form:<built-in function map>
> Namespace:=C2=A0 Python builtin
> Docstring:
> map(function, sequence[, sequence, ...]) -> list
>
> Just getting something wrong
> list(map((lambda x: x * -1 if (x%2 !=3D 0)), series))
> --
> https://mail.python.org/mailman/listinfo/python-list
--001a11c11dba44e291050bda2b86--