Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109659 > unrolled thread

how to extract a variable as parameter which has index using by a for loop?

Started bymeInvent bbird <jobmattcon@gmail.com>
First post2016-06-08 00:31 -0700
Last post2016-06-09 06:26 +1000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#109659 — how to extract a variable as parameter which has index using by a for loop?

FrommeInvent bbird <jobmattcon@gmail.com>
Date2016-06-08 00:31 -0700
Subjecthow to extract a variable as parameter which has index using by a for loop?
Message-ID<dc4d4599-05e1-4156-8ce6-66ff955d9851@googlegroups.com>
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))]

how to extract b[i][0:1] and b[i][1:2] as parameters?

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])

[toc] | [next] | [standalone]


#109664

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2016-06-08 18:56 +1000
Message-ID<5757ddcd$0$1520$c3e8da3$5496439d@news.astraweb.com>
In reply to#109659
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

[toc] | [prev] | [next] | [standalone]


#109666

FrommeInvent bbird <jobmattcon@gmail.com>
Date2016-06-08 02:20 -0700
Message-ID<190a4224-d05e-44e7-afba-0495e5dba59c@googlegroups.com>
In reply to#109664
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

[toc] | [prev] | [next] | [standalone]


#109668

FromJoonas Liik <liik.joonas@gmail.com>
Date2016-06-08 12:48 +0300
Message-ID<mailman.73.1465379320.2306.python-list@python.org>
In reply to#109666
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../

[toc] | [prev] | [next] | [standalone]


#109688

FromBen Finney <ben+python@benfinney.id.au>
Date2016-06-09 06:28 +1000
Message-ID<mailman.85.1465417809.2306.python-list@python.org>
In reply to#109666
meInvent bbird <jobmattcon@gmail.com> writes:

> it is not convenient to disclose in detail
> just expect to discuss from the view of programming

It is not convenient to discuss programming without a good understanding
of what the program is meant to do.

(Also, please don't top-post, instead post your responses interleaved
<URL:https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>.)

-- 
 \         “The most dangerous man to any government is the man who is |
  `\       able to think things out for himself, without regard to the |
_o__)          prevailing superstitions and taboos.” —Henry L. Mencken |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#109687 — 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?))

FromBen Finney <ben+python@benfinney.id.au>
Date2016-06-09 06:26 +1000
SubjectMAthematical 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?))
Message-ID<mailman.84.1465417626.2306.python-list@python.org>
In reply to#109664
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> On Wednesday 08 June 2016 17:31, meInvent bbird wrote:
>
> > 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.

The nice thing about mathematical formulas is that one has latitude, in
the accompanying academic paper, to define at length the
single-character names. This means the formulas are briefer to write
by hand on the blackboard, and the reader can refer to the author's
extensive explanation of what the character means.

Mathematicians (and other scientists), please don't attempt to cram
meaning into single-letter names in your program code. None of the above
advantages apply, and we get only the disadvantages Steven describes.

Instead, choose meaningful names that someone who knows that meaning has
at least a better-than-random chance of remembering.

-- 
 \       “The fundamental principle of science, the definition almost, |
  `\             is this: the sole test of the validity of any idea is |
_o__)                                 experiment.” —Richard P. Feynman |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web