Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109668
| From | Joonas Liik <liik.joonas@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: how to extract a variable as parameter which has index using by a for loop? |
| Date | 2016-06-08 12:48 +0300 |
| Message-ID | <mailman.73.1465379320.2306.python-list@python.org> (permalink) |
| References | <dc4d4599-05e1-4156-8ce6-66ff955d9851@googlegroups.com> <5757ddcd$0$1520$c3e8da3$5496439d@news.astraweb.com> <190a4224-d05e-44e7-afba-0495e5dba59c@googlegroups.com> <CAB1GNpQAz4wwtX1E1Cs=aNpGwnh4=KNMufQLOekqE1UxFhgrpA@mail.gmail.com> |
On 8 June 2016 at 12:20, meInvent bbird <jobmattcon@gmail.com> 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 = [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 = [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 = 5 >> > H2 = [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 these >> variable names that don't mean anything, like "b" and "H2", and others which >> aren't defined, like MM. I don't understand what you are trying to accomplish, >> 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 = [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] == b[i][0]+b[i][1] possibly dependant of on the data type of b[i] possibly.. b[i][0]+b[i][1] == 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 = b[21] A = b21[0:1]+b21[1:2] B = b21[1:2]+b21[2:3] C = 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 = [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 = b[i][0:1]; b12 = b[i][1:2] # since you obviously omitted some code you might want to take in some other params as well.. like the variable ´b´ 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 = [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../
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
how to extract a variable as parameter which has index using by a for loop? meInvent bbird <jobmattcon@gmail.com> - 2016-06-08 00:31 -0700
Re: how to extract a variable as parameter which has index using by a for loop? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-06-08 18:56 +1000
Re: how to extract a variable as parameter which has index using by a for loop? meInvent bbird <jobmattcon@gmail.com> - 2016-06-08 02:20 -0700
Re: how to extract a variable as parameter which has index using by a for loop? Joonas Liik <liik.joonas@gmail.com> - 2016-06-08 12:48 +0300
Re: how to extract a variable as parameter which has index using by a for loop? Ben Finney <ben+python@benfinney.id.au> - 2016-06-09 06:28 +1000
MAthematical formula terms make for terrible names in a program (was: What's good for mathematical formulas can be bad for program code (was: how to extract a variable as parameter which has index using by a for loop?)) Ben Finney <ben+python@benfinney.id.au> - 2016-06-09 06:26 +1000
csiph-web