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

Joonas Liik liik.joonas at gmail.com
Wed Jun 8 05:48:31 EDT 2016


On 8 June 2016 at 12:20, meInvent bbird <jobmattcon at 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../



More information about the Python-list mailing list