简体   繁体   English

如何将值的出现时间乘以键以获得新值?

[英]How to multiply value's occurence time by the key to get the new value?

The goal:目标:

I want to coune the letters in a sentence, and pprint it as a dictionary, where the value is the occurence of the letters in the sentence, not just its time.我想把句子中的字母组合起来,并将其作为字典打印出来,其中的值是句子中字母的出现次数,而不仅仅是它的时间。

在此处输入图像描述


import pprint
from collections import Counter

get_the_sentence = input()

# Now scatter the sentence into different letters.

# First remove the period.

get_the_sentence = get_the_sentence.replace(".","")

# Second make all the letters its lower case.

get_the_sentence.lower()

# Third scatter the sentence.

get_the_sentence = list(get_the_sentence)

# Now Counter it, then make the Counter a dict.

Counter_the_letter = Counter(get_the_sentence)

Counter_the_letter = dict(Counter_the_letter)

# Then make the final result.

for letter , time in Counter_the_letter:
time = letter*time

pprint(Counter_the_letter)

However I get the Error:但是我得到错误:


python -u "d:\\新建文件夹 (3)\\python_module\\poor_man_bar_chart.py"
Traceback (most recent call last):
File "d:\\新建文件夹 (3)\\python_module\\poor_man_bar_chart.py", line 28, in \<module\>
for letter , time in Counter_the_letter:
ValueError: not enough values to unpack (expected 2, got 1)
PS C:\\Users\\Babe\>

And if I change the如果我改变


for letter , time in Counter_the_letter

to


for letter , time in Counter_the_letter():

Which is Add one more brackets。也就是多加一个括号。

then the Error would become:那么错误将变为:


Traceback (most recent call last):
File "d:\\新建文件夹 (3)\\python_module\\poor_man_bar_chart.py", line 28, in \<module\>
for letter , time in Counter_the_letter():
TypeError: 'dict' object is not callable
PS C:\\Users\\Babe\>

by the way I didn't name other variables as dict before.顺便说一句,我之前没有将其他变量命名为dict。

I wonder what the Error means, an how I can fix it.我想知道错误是什么意思,以及如何修复它。

My python version is Python 3.10.5.我的python版本是Python 3.10.5。

ValueError: not enough values to unpack (expected 2, got 1) ValueError:没有足够的值来解压(预期 2,得到 1)

If you want to iterate over key-value pairs of a dictionay, you should use如果你想遍历字典的键值对,你应该使用

for key, value in some_dict.items():
   print(key, value)

In your case, you are trying to iterate over only keys, which cannot be unpacked to two variables ( letter and time ).在您的情况下,您试图仅迭代密钥,这些密钥不能解压缩为两个变量( lettertime )。


TypeError: 'dict' object is not callable TypeError: 'dict' object 不可调用

This means you are trying to call a dict object, which is not callable .这意味着您正在尝试调用dict object,这是不可调用的。


You can further simplify your code.您可以进一步简化代码。

  1. Omit the type conversion from str to list .省略从strlist的类型转换。

  2. Omit the type conversion from Counter to dict .省略从Counterdict的类型转换。

How to do this without using extra modules:如何在不使用额外模块的情况下做到这一点:

input_string = "absdav"
unique_chars = set(input_string)  # keys of the expected dict
expected_dict = { ch: input_string.count(ch) for ch in unique_chars }

Here,str.count is used to count each unique char in the input string anddict comprehension is used to construct a dictionary.这里,str.count用于计算输入字符串中的每个唯一字符,dict comprehension用于构建字典。

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

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