简体   繁体   English

这行代码做什么?

[英]What does this line of the code do?

I am a newbie to python. 我是python的新手。 I am solving a problem on Hackerrank https://www.hackerrank.com/challenges/maximize-it/problem . 我正在Hackerrank https://www.hackerrank.com/challenges/maximize-it/problem上解决问题。

I wasn't able to solve the problem so I had opened the discussions. 我无法解决问题,所以我打开了讨论。 I found a code there - 我在那里找到了一个代码-

from itertools import product

K,M = map(int,input().split())
N = (list(map(int, input().split()))[1:] for _ in range(K))
results = map(lambda x: sum(i**2 for i in x)%M, product(*N))
print(max(results))

I have 2 doubts: 我有2个疑问:

  1. First, if I try to print N, it throws "generator object is not subscriptable" error, but the value is already converted into list, so I am unable to understand the reason for error. 首先,如果我尝试打印N,它会引发“生成器对象不可下标”错误,但该值已转换为列表,因此我无法理解错误原因。
  2. I am unable to understand what will be stored as a result of results=map(lambda x: sum(i**2 for i in x)%M, product(*N)) . 我无法理解result results=map(lambda x: sum(i**2 for i in x)%M, product(*N))

I understand what product(*N) does. 我了解product(*N)作用。 But what would be the output of sum(i**2 for i in x)%M ? 但是sum(i**2 for i in x)%M的输出是什么? Is it adding the squares of the values of list and then performing the modulus operation? 是否将list的值的平方相加,然后执行模运算? Or is it something else? 或者是别的什么?

For your 1st doubt: 对于您的第一个疑问:

Why is N a generator? 为什么N是发电机?

The list function is being applied to the map(int, input().split()) statement and indeed that is a list being returned, however, the outermost pair of parentheses and for _ in range(K) means that a generator object is being returned to N and not the list. list函数被应用到map(int, input().split())语句,并且确实是要返回的列表,但是,最外面的一对括号和for _ in range(K)表示生成器对象返回到N而不是列表。 If you want to see the contents of N while debugging, change the outermost parentheses to square brackets [ and ] and add a print(N) statement after that as such: 如果要在调试时查看N的内容,请将最外面的括号更改为方括号[]并在其后添加一个print(N)语句,如下所示:

# Debug & view contents of N like this:
N = [ list(map(int, input().split()))[1:] for _ in range(K) ]
print(N)


For your 2nd doubt: 对于您的第二个疑问:

How does the value of the variable results come about? 变量results的值如何得出?

Just so everyone who's reading this answer is clear, in the below example, I'll let: 为了使每个阅读此答案的人都很清楚,在下面的示例中,我将让:

N = [[1, 2], [3, 4], [5, 6]]

product(*N) will return an iterator for a list of tuples containing the cartesian product of each of the sublists of N, eg product(*N) returns the tuples (1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), ..., (2, 4, 6) . product(*N)将返回包含N个子列表的笛卡尔积的元组列表的迭代器,例如product(*N)返回元组(1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), ..., (2, 4, 6)

You can verify this by doing print([x for x in product(*N)]) or if you iterate through N in a for loop and print out each value as such: 您可以通过执行print([x for x in product(*N)])或在for循环中遍历N并像这样打印出每个值来验证这一点:

N = [[1, 2], [3, 4], [5, 6]]

# You can verify the contents of a generator like this:
print([item for item in product(*N)])

# Or like this:
for item in product(*N):
    print(item)


Breaking down results = map(lambda x: sum(i**2 for i in x)%M, product(*N)) : 分解results = map(lambda x: sum(i**2 for i in x)%M, product(*N))

In (i**2 for i in x) , x refers to one of the tuples, eg (1, 3, 5) in product(*N) , and returns a generator that contains the values (1, 9, 25) . (i**2 for i in x)x指代一个元组,例如product(*N) (1, 3, 5) product(*N) ,并返回包含值(1, 9, 25) 1、9、25)的生成器。 So it's basically squaring every integer in the tuple (1, 3, 5) and returning a tuple of their squares. 因此,基本上是对元组(1, 3, 5)中的每个整数进行平方(1, 3, 5)然后返回其平方的元组。

sum(i**2 for i in x) sums every integer in the tuple x to give you 1 + 9 + 25 = 35. sum(i**2 for i in x)将元组x中的每个整数相加,得出1 + 9 + 25 = 35。

After that, the resultant sum, 35, is applied to the modulus operation % M . 此后,将所得的总和35应用于模运算% M

Finally, results would contain the generator with all of the resultant modulus sums for every tuple. 最后, results将包含生成器,其中包含每个元组的所有结果模量和。 Similarly, you can verify the content of results by doing print([x for x in results]) or if you iterate results in a for loop and print out each value just like how it's done in the code snippet above. 类似地,您可以通过执行print([x for x in results]) resultsprint([x for x in results])来验证results的内容,或者如果您在for循环中迭代results并打印出每个值,就像在上面的代码片段中所做的那样,则可以验证results的内容。

print(max(results)) would then give you the largest value in results . print(max(results))将为您提供最大的results


( I assume Python-3.x is used. ) 我假设使用的是Python-3.x。

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

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