简体   繁体   English

列表理解困难,而嵌套的 for 循环可能很讨厌

[英]Difficulty in list comprehension while a nested for loop could be nasty

I need to get (-2.0 EM EX W_1_M_X x1/F - 2.0 EN EX W_1_N_X x1/F) in my code.我需要在我的代码中获取 (-2.0 EM EX W_1_M_X x1/F - 2.0 EN EX W_1_N_X x1/F)。 Everything is working well using the nested for loop while if I want to get result using the list comprehension then it(aasum) gives duplicate elements.使用嵌套的 for 循环,一切都运行良好,而如果我想使用列表推导式获得结果,那么它 (aasum) 会给出重复的元素。

import sympy as sp
anions = {'X': 1 }
cations = {'M': 1, 'N': 1}
neutrals = {'1' : 0}
aa = list(-(1 / sp.symbols(f'F')) * sp.symbols(f'E{m}') * sp.symbols(f'E{n}') * \
                            ((x + y) / (x * y)) * sp.symbols(f'x{str(list(neutrals.keys())[0])}') *       sp.symbols(f'W_{str(list(neutrals.keys())[0])}_{str(m)}_{str(n)}') \
                            for m in cations.keys() \
                            for x in cations.values() \
                            for n in anions.keys() \
                            for y in anions.values())

aasum = sum(aa)
asum = 0.0
for k in anions:
    for j in cations:
        for n in neutrals:
            asum += -(1 / sp.symbols(f'F')) * sp.symbols(f'E{str(j)}') * sp.symbols(f'E{str(k)}') * \
            ((cations[j] + anions[k]) / (cations[j] * anions[k])) * sp.symbols(f'x{str(list(neutrals.keys())[0])}') *\
                 sp.symbols(f'W_{str(list(neutrals.keys())[0])}_{str(j)}_{str(k)}')

print(asum,'...', aasum) 

result is: asum:-2.0 EM EX W_1_M_X x1/F - 2.0 EN EX W_1_N_X x1/F... aasum:-4.0 EM EX W_1_M_X x1/F - 4.0 EN EX W_1_N_X x1/FI checked: [https://stackoverflow.com/questions/64091392/nested-list-in-list-comprehension-to-for-loop] But it has nothing to do answering my question.结果是:asum:-2.0 EM EX W_1_M_X x1/F - 2.0 EN EX W_1_N_X x1/F... aasum:-4.0 EM EX W_1_M_X x1/F - 4.0 EN EX W_1_N_X x1/FI 检查:[https://stackoverflow .com/questions/64091392/nested-list-in-list-comprehension-to-for-loop] 但它与回答我的问题无关。 Any suggestion to get correct result using list comprehension would be welcomed.欢迎任何使用列表理解获得正确结果的建议。

Use items instead of keys and values :使用items而不是keysvalues

import sympy as sp
anions = {'X': 1 }
cations = {'M': 1, 'N': 1}
neutrals = {'1' : 0}
aa = [-(1 / sp.symbols(f'F')) * sp.symbols(f'E{m}') * sp.symbols(f'E{n}') *
      ((x + y) / (x * y)) * sp.symbols(f'x{str(list(neutrals.keys())[0])}') * sp.symbols(f'W_{str(list(neutrals.keys())[0])}_{str(m)}_{str(n)}')
          for (m, x) in cations.items()
          for (n, y) in anions.items()]

Output: Output:

>>> aa
[-2.0*EM*EX*W_1_M_X*x1/F, -2.0*EN*EX*W_1_N_X*x1/F]

>>> sum(aa)
-2.0*EM*EX*W_1_M_X*x1/F - 2.0*EN*EX*W_1_N_X*x1/F

Explanation :解释

In your loop, you iterate over keys only so you get the cartesian product of anions x cations.在你的循环中,你只迭代键,这样你就可以得到阴离子 x 阳离子的笛卡尔积。 In your comprehension, you iterate both on keys ans values so you have the product of cations.values x cations.keys, x anions.values x anions.keys.在您的理解中,您对键和值进行了迭代,因此您得到了 cations.values x cations.keys、x anions.values x anions.keys 的乘积。 What I did was replicate what you did in your loop except I get the keys and values at the same time.我所做的是复制你在循环中所做的,除了我同时获得键和值。 I have the product of (anions.keys, anion.values) x (cations.keys, cations.values).我有 (anions.keys, anion.values) x (cations.keys, cations.values) 的乘积。 It avoid j and k indexes.它避免了 j 和 k 索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM