简体   繁体   English

如何将字典中的数字列表平方?

[英]How can I square the list of numbers in a dictionary?

I want to square all the values in a Python dictionary, and am going about it the wrong way. 我想将所有值平方在Python字典中,并以错误的方式进行处理。 Below is my attempt which obviously fails and yields a 下面是我的尝试,显然失败了,并产生了

TypeError: unsupported operand type(s)

And the program is 程序是

tides = {'G3': [3, 8, 9, 7], 'G2': [2, 7], 'G1': [1, 6, 7]}

for v in tides.values():
    print v**2

The desired output is 所需的输出是

{'G3': [9, 64, 81, 49], 'G2': [4, 49], 'G1': [1, 36, 49]}

The dict.values gives you a list of items and each element of that list is a list. dict.values给您一个项目列表,该列表的每个元素都是一个列表。 So, when you say v ** 2 , you are actually trying to square a list, which is not possible in Python as such. 因此,当您说v ** 2 ,您实际上是在尝试对列表进行平方运算,而这在Python中是不可能的。 That is why it is failing. 这就是为什么它失败了。

Instead, just recreate the dictionary with dictionary comprehension, like this 相反,只需使用字典理解来重新创建字典,就像这样

>>> tides = {'G3': [3, 8, 9, 7], 'G2': [2, 7], 'G1': [1, 6, 7]}
>>> {tide: [value ** 2 for value in tides[tide]] for tide in tides}
{'G3': [9, 64, 81, 49], 'G2': [4, 49], 'G1': [1, 36, 49]}

Or you can use dict.iteritems like this 或者您可以像这样使用dict.iteritems

>>> {tide: [value ** 2 for value in values] for tide, values in tides.iteritems()}
{'G3': [9, 64, 81, 49], 'G2': [4, 49], 'G1': [1, 36, 49]}

Note: In both the examples I have shown above, I have avoided using dict.values , because that will create a temporary list of all the values from the dictionary. 注意:在上面显示的两个示例中,我避免使用dict.values ,因为这将创建字典中所有值的临时列表。 We don't want that. 我们不想要那个。

In the first example, we iterate through the keys in the dictionary and get values corresponding to the key and square each value in it. 在第一个示例中,我们遍历字典中的键并获取与键相对应的值,并对其中的每个值求平方。

In the second example, we take one pair of key and value at a time from the dictionary. 在第二个示例中,我们一次从字典中获取一对键和值。 So, we don't create the temporary list. 因此,我们不创建临时列表。

The code within {...} shown above is called dictionary comprehension and within that, whatever is within [...] is called list comprehension . 内的代码{...}上面示出被称为字典解析和范围内的,无论是内[...]被称为列表理解

When you do tides.values() what you get is: 当您执行tides.values() ,您会得到:

[[3, 8, 9, 7], [2, 7], [1, 6, 7]]

Therefore when you do 因此,当你做

for v in tides.values():
    print v ** 2

you're trying to do 你正在尝试做

[3, 8, 9, 7] ** 2 # and [2, 7] ** 2 and [1, 6, 7] ** 2

which is obviously gibberish. 这显然是胡言乱语。 Instead you should try to do: 相反,您应该尝试执行以下操作:

for key, value in tides.items():
    tides[key] = [num**2 for num in value]

You can use a dictionary comprehension: 您可以使用字典理解:

tides = {'G3': [3, 8, 9, 7], 'G2': [2, 7], 'G1': [1, 6, 7]}
tides = {k: map(lambda num: num ** 2, v) for k, v in tides.items()}

Result: 结果:

{'G3': [9, 64, 81, 49], 'G2': [4, 49], 'G1': [1, 36, 49]}

map(lambda num: num ** 2, v) squares the values of each value v of tides . map(lambda num: num ** 2, v)tides的每个值v的值平方。

If you just want to replace in place, you can do the following: 如果您只想就地更换,则可以执行以下操作:

for v in tides.values():
    for i in range(len(v)):
        v[i] **= 2

tides.values() returns a list of lists. tides.values()返回列表列表。 You are trying to square the lists rather than the values in the lists. 您正在尝试使列表平方而不是列表中的值。 You can add a second loop to iterate through the list. 您可以添加第二个循环以遍历列表。

for v in tides.values(): for x in v: print x**2 对于v在tides.values()中:对于v在x中:打印x ** 2

使用字典理解:

print {k: map(lambda x: x**2, [x for x in v]) for k, v in tides.items()}

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

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