Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Joonas Liik Newsgroups: comp.lang.python Subject: Re: how to extract a variable as parameter which has index using by a for loop? Date: Wed, 8 Jun 2016 12:48:31 +0300 Lines: 120 Message-ID: References: <5757ddcd$0$1520$c3e8da3$5496439d@news.astraweb.com> <190a4224-d05e-44e7-afba-0495e5dba59c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de 7ghshhlhGllsuusFnUFfYAIboMOY1RiQY+Mp68F64Amg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'yet.': 0.03; 'params': 0.07; 'wednesday,': 0.07; 'cc:addr:python-list': 0.09; 'defined,': 0.09; 'meaningful': 0.09; 'subject:using': 0.09; 'subject:which': 0.09; 'english.': 0.13; 'question.': 0.13; 'def': 0.13; 'do,': 0.15; 'subject: \n ': 0.15; 'wednesday': 0.15; '"b"': 0.16; '2016': 0.16; 'b):': 0.16; 'cc:name:python': 0.16; 'dependant': 0.16; 'originally,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sth': 0.16; 'subject:variable': 0.16; 'useless': 0.16; 'useless.': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'example.': 0.18; 'steve': 0.18; 'variable': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fix': 0.21; 'do.': 0.22; "aren't": 0.22; 'arguments': 0.22; 'assign': 0.22; 'trying': 0.22; 'programming': 0.22; 'code.': 0.23; 'header:In-Reply-To:1': 0.24; 'separate': 0.27; 'least': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'actual': 0.28; 'looks': 0.29; 'attempting': 0.29; 'omitted': 0.29; 'url:mailman': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'guess': 0.31; 'supposed': 0.31; 'post': 0.31; 'another': 0.32; 'possibly': 0.32; 'url:python': 0.33; 'usually': 0.33; "d'aprano": 0.33; 'extract': 0.33; 'steven': 0.33; 'url:listinfo': 0.34; 'add': 0.34; 'info': 0.34; 'received:google.com': 0.35; 'trouble': 0.35; 'next': 0.35; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'totally': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'expect': 0.37; 'things': 0.38; 'detail': 0.38; 'received:209': 0.38; 'feedback': 0.38; 'names': 0.38; 'mean': 0.38; 'data': 0.39; 'does': 0.39; 'received:209.85.214': 0.39; 'url:mail': 0.40; 'some': 0.40; 'your': 0.60; 'hope': 0.61; 'discuss': 0.61; 'fire': 0.63; 'times': 0.63; 'intent': 0.66; 'here': 0.66; 'disclose': 0.71; '100%': 0.72; 'b01': 0.84; 'horrible': 0.84; 'received:209.85.214.47': 0.84; 'burn': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=FX40y+ikXUGB/PDSAinOa7Z54WO7HRi3KRwuo09WZog=; b=Ifiw03p06ujQhU95G4SubdvQBSeWoH3yeJo4r8TVthU/llDWXD/c6o5JiGYANCiYit ZGlE1reIKFNnbyGQnLUPxkt5TyAWJG2FWutsX2R9NcpRXW56i3Dj48xwhwYYNOFo188Z u40PxOX2HvzGtkVY0n0G/810d9++Eh7X8YngGm6SwtutnLwO+Js+XRlN56SKDuxns9vs 7JBVG6zRlxfObr72ZxQ/iWFGqnu3NGr9S1x/5+ESNGzhI2Vc0I9QEWPryZaLaCr7J9Mx xVjddmuAYU/HKk9OU96MhhLOl/ihga8L70Xqxjmz+5Wz1wzE6/4HvclGQiFCVebqz+81 Q03Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=FX40y+ikXUGB/PDSAinOa7Z54WO7HRi3KRwuo09WZog=; b=ep7vFnk7a/yIdlf9txrZ10JAsrIIPFN2983fZRjzEMBZpL12k/uFGr/lGvmBGOSiBi oP4BPY8ajlCfo3ItjnOuGjg4GAxU/IXxH8xRZ6uA8Uhg1JcPd3+6vZcRxSz4mH7ZDqhT aDfGvX7Itx1RtUwbn25uAJo95gMwMhLJ6nrpXliut/bQnFCP00aO3Jq1b1JhnV0qrZCg 5uePZHWVrO/H+isD5sFuVCZRQdzMX1rmOS6KuB0FP/uNoJiWNkLMUGZOqjOjgsQKYIDm iUSrKQqjrzx9lykpCMsk/BwHAOtEO60RLa/0VoGcbdursReUkLV5snyhWxIJmgLTmyLl y9+w== X-Gm-Message-State: ALyK8tIMBiqoiV2nuIVAlN3vTabvxZzrFwj5b206LTBBp5q5Tv+MaLsGPV/gHOeGWI36WC0AzUEPeZS0B6Dn7w== X-Received: by 10.36.134.3 with SMTP id u3mr6728546itd.23.1465379311827; Wed, 08 Jun 2016 02:48:31 -0700 (PDT) In-Reply-To: <190a4224-d05e-44e7-afba-0495e5dba59c@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <5757ddcd$0$1520$c3e8da3$5496439d@news.astraweb.com> <190a4224-d05e-44e7-afba-0495e5dba59c@googlegroups.com> Xref: csiph.com comp.lang.python:109668 On 8 June 2016 at 12:20, meInvent bbird wrote: > just extract b[i][0:1] and b[i][1:2] out of for loop > > but i depend on for loop > > def node(mmm, A, B): > H2 =3D [MM[mmm][A+B] for i in range(len(b))] > return H2 > > node(5, b[i][0:1], b[i][1:2]) > > it is not convenient to disclose in detail > just expect to discuss from the view of programming > > On Wednesday, June 8, 2016 at 4:56:56 PM UTC+8, Steven D'Aprano wrote: >> On Wednesday 08 June 2016 17:31, meInvent bbird wrote: >> >> > b =3D [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k = in >> > range(m)] >> > b[21][0:1]+b[21][1:2] >> > b[21][1:2]+b[21][2:3] >> > b[21][0:1]+b[21][2:3] >> > >> > >> > originally, >> > >> > mmm =3D 5 >> > H2 =3D [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))] >> >> This is a mess. I don't understand what you are trying to do. You have t= hese >> variable names that don't mean anything, like "b" and "H2", and others w= hich >> aren't defined, like MM. I don't understand what you are trying to accom= plish, >> or the purpose of your code. >> >> >> > how to extract b[i][0:1] and b[i][1:2] as parameters? >> >> I don't understand the question. >> >> >> > def node(mmm, b[i][0:1], b[i][1:2]): >> > H2 =3D [MM[mmm][A+B] for i in range(len(b))] >> > return H2 >> > >> > node(5, b[i][0:1], b[i][1:2]) >> >> Explain what node() is supposed to do, in English. Don't write any code = yet. >> What is its purpose? What does it return? What arguments does it need to= take >> in order to perform its purpose? >> >> >> -- >> Steve > -- > https://mail.python.org/mailman/listinfo/python-list for a start.. a few things to improve readability.. (as well as perf maybe) b[i][0:1]+b[i][1:2] =3D=3D b[i][0]+b[i][1] possibly dependant of on the data type of b[i] possibly.. b[i][0]+b[i][1] =3D=3D b[i][0:2] also.. >> > b[21][0:1]+b[21][1:2] >> > b[21][1:2]+b[21][2:3] >> > b[21][0:1]+b[21][2:3] this is.. really confusing.. usually when you add things you would assign the result to something or use it in another way.. so this code looks like it is totally useless. it might not be 100% useless but if it is.. it is such horrible code u rly need to burn it with fire . since u use b[21] 6 times here you should just extract it as a separate variable eg.. b21 =3D b[21] A =3D b21[0:1]+b21[1:2] B =3D b21[1:2]+b21[2:3] C =3D b21[0:1]+b21[2:3] that is ofc if now this is rly interesting.. i think there is a big misunderstanding about what function arguments are.. def node(mmm, b[i][0:1], b[i][1:2]): H2 =3D [MM[mmm][A+B] for i in range(len(b))] return H2 node(5, b[i][0:1], b[i][1:2]) if i can guess your intent correctly.. what you actually want is sth like def node(mmm, b01, b12): # b01 =3D b[i][0:1]; b12 =3D b[i][1:2] # since you obviously omitted some code you might want to take in some other params as well.. like the variable =C2=B4b=C2=B4 for example. # i hope this is at least a little helpful, it really is hard to give constructive feedback if you cant even post the actual code nor can you give any info about what you are attempting to do H2 =3D [MM[mmm][A+B] for i in range(len(b))] return H2 node(5, b[i][0:1], b[i][1:2]) also it helps to give meaningful names to things, i guarantee next week even you will be having trouble reading this code... /now what the heck was that MM thing.. i wonder.. i could totally fix it if i only named it sth decent../