0

after i test,

len_stat = [len(x) if len(x) > len_stat[i] else len_stat[i] for i, x in enumerate(a_list)]

is same with

for i, x in enumerate(a_list):
    if len(x) > len_stat[i]:
        len_stat[i] = len(x)

of course, but,

len_stat = [len(x) if len(x) > len_stat[a_list.index(x)] else len_stat[a_list.index(x)] for x in a_list]

is different with

for x in a_list:
    if len(x) > len_stat[a_list.index(x)]:
        len_stat[a_list.index(x)] = len(x)

later, i know <list>.index is a bad method used here.

but why they are different in last two examples?


my fault! here is my test code,

a_list = ['Bacteroides fragilis YCH46', 'YCH46', '20571', '-', 'PRJNA13067', 'FCB group', 'Bacteroidetes/Chlorobi group', 'GCA_000009925.1 ', '5.31099', '43.2378', 'chromosome:NC_006347.1/AP006841.1; plasmid pBFY46:NC_006297.1/AP006842.1', '-', '2', '4717', '4625', '2004/09/17', '2016/08/03', 'Complete Genome', 'ftp://ftp.ncbi.nlm.nih.gov/genomes/all/GCF_000009925.1_ASM992v1', 'ftp://ftp.ncbi.nlm.nih.gov/genomes/all/GCA_000009925.1_ASM992v1']

len_stat_1 = [0 for x in a_list]
len_stat_2 = [0 for x in a_list]
len_stat_3 = [0 for x in a_list]
len_stat_4 = [0 for x in a_list]

len_stat_1 = [len(x) if len(x) > len_stat_1[i] else len_stat_1[i] for i, x in enumerate(a_list)]

for i, x in enumerate(a_list):
    if len(x) > len_stat_2[i]:
        len_stat_2[i] = len(x)

len_stat_3 = [len(x) if len(x) > len_stat_3[a_list.index(x)] else len_stat_3[a_list.index(x)] for x in a_list]

for x in a_list:
    if len(x) > len_stat_4[a_list.index(x)]:
        len_stat_4[a_list.index(x)] = len(x)

print len_stat_1
print len_stat_2
print len_stat_3
print len_stat_4

output:

[26, 5, 5, 1, 10, 9, 28, 16, 7, 7, 72, 1, 1, 4, 4, 10, 10, 15, 63, 63]
[26, 5, 5, 1, 10, 9, 28, 16, 7, 7, 72, 1, 1, 4, 4, 10, 10, 15, 63, 63]
[26, 5, 5, 1, 10, 9, 28, 16, 7, 7, 72, 1, 1, 4, 4, 10, 10, 15, 63, 63]
[26, 5, 5, 1, 10, 9, 28, 16, 7, 7, 72, 0, 1, 4, 4, 10, 10, 15, 63, 63]

as you can see, the last two is different!

it really confused me.

3
  • What is different? What input do you use, what output do you get, and what output do you expect? Commented Oct 10, 2016 at 2:17
  • how does your a_list look like? Can you add the input that you tested with? Commented Oct 10, 2016 at 2:18
  • Thank you guys help me! Sorry, i add my test code. Commented Oct 10, 2016 at 3:09

1 Answer 1

1

This list comprehension:

len_stat = [len(x) if len(x) > len_stat[a_list.index(x)] else len_stat[a_list.index(x)] for x in a_list]

Is equivalent to:

temp = []
for x in a_list:
    temp.append(len(x) if len(x) > len_stat[a_list.index(x)] else len_stat[a_list.index(x)])
len_stat = temp

Which is equivalent to:

temp = []
for x in a_list:
    if len(x) > len_stat[a_list.index(x)]:
        val = len(x)
    else:
        val = len_stat[a_list.index(x)]
    temp.append(val)
len_stat = temp

Equivalent, of course, except for the temp list. The comprehension approach will replace len_stat with a new list, that will always be the same length as len_stat that will have either len(x) for every x in len_stat or will have len_stat[a_list.index(x)]. The for-loop you wrote will mutate len_stat based on the conditional, and it's hard to say exactly what will happen without knowing the content of your lists, but it could potentially change values of len_stat all over the place.

To be clear

I do not recommend this approach, but I am trying to illustrate how the comprehension is different than the for-loop.

Sign up to request clarification or add additional context in comments.

3 Comments

I think it's better if you do, len_stat = temp[:]
@kmario23 I think it's better if you don't use this approach at all.
Yes! you are right! I mutate the list when i for-loop it, it case unexpected results. And sorry for not accepting for a long time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.