Python元素的总和

huangapple go评论74阅读模式
英文:

Python sum of elements

问题

可以有人解释一下这段代码吗?我不明白为什么sumOfLetter对于inputText中的每个i,还有sumOfLetter。第二个“for”是做什么的?

sum([sumOfLetter for i in inputText for sumOfLetter, weigthOfCh in pointsInRu.items() if i in weigthOfCh])

我找到这段代码来解决我的问题,更改了变量名称,它可以正常工作,但不明白它是如何工作的。

英文:

Can some one explain me this code? I can't understand why sumOfLetter for i in inputText for sumOfLetter. What does second "for" do?

sum([sumOfLetter for i in inputText for sumOfLetter, weigthOfCh in pointsInRu.items() if i in weigthOfCh])

I find this code for my solution, changed variable names and it's work correct, but don't understand how it works.

答案1

得分: 1

这种表示方法是上面代码的简写。方括号表明正在创建一个list并直接用于sum()函数。由于列表是在创建时直接使用的,所以没有变量,也没有append()(这是隐式完成的)。

new_list = []
for i in inputText:
  for sumOfLetter, weigthOfCh in pointsInRu.items():
    if i in weigthOfCh:
      new_list.append(sumOfLetter)

sum(new_list)
英文:

This notation is shorthand for the code below. The square brackets show there's a list being created and directly used for the sum() function. Since the list is created and directly used, there's no variable and also no append() (this is done implicitely).

new_list = []
for i in inputText:
  for sumOfLetter, weigthOfCh in pointsInRu.items():
    if i in weigthOfCh:
      new_list.append(sumOfLetter)

sum(new_list)

答案2

得分: 1

pointsInRu是一个键-值对(可能是字典),其中包含字母作为键。

inputText不言自明。

[sumOfLetter for i in inputText for sumOfLetter, weigthOfCh in pointsInRu.items() if i in weigthOfCh] 是一个列表推导式,它遍历inputText中的每个字符i,对于每个字符,它检查它是否存在于pointsInRu的键中。如果存在,它提取相应的值weigthOfCh 并将其附加到名为sumOfLetter的列表中。

然后使用sum()函数来计算sumOfLetter列表中的所有值并返回总值。

英文:

Not a python expert, but i think this is what is going on:

pointsInRu is a key-value pair (dictionary might be what it is called) that contains letters as keys.

inputText self explanatory.

[sumOfLetter for i in inputText for sumOfLetter, weigthOfCh in pointsInRu.items() if i in weigthOfCh] is where things get sketchy, its a list comprehension that iterates over each character i in inputText, and for each character, it checks if it exists in the keys of pointsInRu. If it does, then it extracts the corresponding value weigthOfCh and appends it to a list called sumOfLetter.

Then the sum() function is used to sum up all the values in the sumOfLetter list and return the total value.

答案3

得分: 0

  1. 原始代码:
sum([sumOfLetter for i in inputText for sumOfLetter, weigthOfCh in pointsInRu.items() if i in weigthOfCh])
  1. 以上代码等同于:
my_values = [sumOfLetter for i in inputText 
            for sumOfLetter, weigthOfCh in pointsInRu.items() 
            if i in weigthOfCh]

sum(my_values)

要进一步分析,我将做一些假设:

  • inputText 是一个固定的字符串
  • pointsInRu 是一个将数字映射到字符串的字典

然后:

my_values = [sumOfLetter for i in inputText 
            for sumOfLetter, weigthOfCh in pointsInRu.items() 
            if i in weigthOfCh]

等同于:

my_values  = []

for i in inputText:
   for sumOfLetter, weigthOfCh in pointsInRu.items():
      if i in weigthOfCh:
          my_values.append(sumOfLetter)

这段代码展示了正在使用的语法:

[f'{i=}{j=}' for i in [1,2,3] for j in [5,6,7]]

>>> [
 'i=1j=5',
 'i=1j=6',
 'i=1j=7',
 'i=2j=5',
 'i=2j=6',
 'i=2j=7',
 'i=3j=5',
 'i=3j=6',
 'i=3j=7']

希望这有助于您理解这段代码。

英文:

To be fair, this is pretty nasty code, let's give it a go:

  1. The original code:
sum([sumOfLetter for i in inputText for sumOfLetter, weigthOfCh in pointsInRu.items() if i in weigthOfCh])

  1. The above is equivalent to this:
my_values = [sumOfLetter for i in inputText 
            for sumOfLetter, weigthOfCh in pointsInRu.items() 
            if i in weigthOfCh]

sum(my_values)

To go further I'll make some assumptions,

inputText is some fixed string
pointsInRu is a dictionary mapping a number to a string (?)

Then:

my_values = [sumOfLetter for i in inputText 
            for sumOfLetter, weigthOfCh in pointsInRu.items() 
            if i in weigthOfCh]

is equivalent to:

my_values  = []

for i in inputText:
   for sumOfLetter, weigthOfCh in pointsInRu.items():
      if i in weigthOfCh:
          my_values.append(sumOfLetter)

This code illustrates the syntax that is being used:

[f'{i=}{j=}' for i in [1,2,3] for j in [5,6,7]]

>>> [
 'i=1j=5',
 'i=1j=6',
 'i=1j=7',
 'i=2j=5',
 'i=2j=6',
 'i=2j=7',
 'i=3j=5',
 'i=3j=6',
 'i=3j=7']

huangapple
  • 本文由 发表于 2023年3月9日 22:44:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定