dict[n]做什么?

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

What does dict[n] do?

问题

def char_freq(s):
    dict = {}
    for n in s:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict

print(char_freq('google'))

I'm a noob and I don't know how this works, specifically the dict[n] += 1 part. what does dict[n] do?

tried just printing the keys and that didn't work either. come to think of it, how does the string get broken into keys / individual letters anyway?
def char_freq(s):
    dict = {}
    for n in s:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict

print(char_freq('google'))

我是个新手不太了解这个特别是 dict[n] += 1 这部分dict[n] 是什么意思

我尝试只打印键但也不起作用想想看字符串是如何被分解成键/单个字母的
英文:
def char_freq(s):
    dict = {}
    for n in s:
        keys = dict.keys()
        if n in keys:
            dict[n] += 1
        else:
            dict[n] = 1
    return dict

print(char_freq('google'))

I'm a noob and I don't know how this works, specifically the dict[n] += 1 part. what does dict[n] do?

tried just printing the keys and that didn't work either. come to think of it, how does the string get broken into keys / individual letters anyway?

答案1

得分: 1

如果我们运行 char_freq("abbabcbdbabdbdbabababcbcbab"),那么每个 n 都是字符串 "abbabcbdbabdbdbabababcbcbab" 中的一个字母。所以对于第一个字母("a"):

  • 它检查是否已经在字典中存在键 "a"(实际上不存在)
  • 然后它在字典中创建键 "a",值为 1

以此类推。如果它发现键已经在字典中存在,它只会将其值增加 1。dict[n] += 1 相当于 dict[n] = dict[n] + 1

运行后的最终结果是 {'a': 7, 'b': 14, 'c': 3, 'd': 3}

你也可以使用以下代码,这可能更容易理解:

def char_freq(s):
    d = {}
    for n in s:
        d[n] = d.get(n, 0) + 1  # 获取字典中键 "n" 的值,然后加 1。如果 "n" 不是字典中的键,则将其视为 0,然后再加 1
    return d
英文:

If we run char_freq("abbabcbdbabdbdbabababcbcbab"), then each n is a letter in the string "abbabcbdbabdbdbabababcbcbab". So for the first letter ("a"):

  • it checks if the "a" is a key in the dictionary already (which it isn't)
  • then it creates the key "a" in the dictionary, with the value 1

and so on. If it finds that the key is already in the dictionary, it just increases the value of it by 1. dict[n] += 1 is the same as dict[n] = dict[n] + 1.

The end result of running it is {'a': 7, 'b': 14, 'c': 3, 'd': 3}

You can also use this, which might make it easier to understand:

def char_freq(s):
    d = {}
    for n in s:
        d[n] = d.get(n, 0) + 1 # get the value of the key "n" in the dictionary, then add 1. If n isn't a key in the dictionary, get 0 instead, then add 1 to that instead
    return d


</details>



# 答案2
**得分**: 1

1. `for n in s: # that means n is g o o g l e successively.`

2. 
```python
        if n in keys:
            d[n] += 1
        else:
            d[n] = 1

这部分代码的意思是,如果字典 d 不包含键 n,那么将值设置为 1,否则将原始值加 1。

  1. 这里不需要使用 d.keys(),只需使用 n in d 即可。
英文:

you cannot use python key word dict as variable name. change dict to other

def char_freq(s):
    d = {}
    for n in s:
        if n in d:
            d[n] += 1
        else:
            d[n] = 1
    return d

print(char_freq(&#39;google&#39;))
  1. for n in s: # that means n is g o o g l e successively.
        if n in keys:
            d[n] += 1
        else:
            d[n] = 1

this piece means if the dict d does not contain n the value will be set 1 or will be set the original plus 1.

  1. here you does not need d.keys(), just use n in d instead.

答案3

得分: 0

这个代码计算给定字符串中每个字母的频率。
s是你的字符串

对于s中的每个字符n:逐个将它们放入n,然后执行循环for n

首先,dict.keys()返回一个空数组,因此keys = []
所以对于n不在keys数组中的任何字符,将执行else部分,dict[n]=1

然后,在下一个循环中,如果任何字符存在于keys数组中(假设你在第三次循环中,keys = ['g', 'o']),那么该键的值将变为value=value + 1({'g':1, 'o':1} -> {'g':1, 'o':2})
最后,char_freq返回一个字典,它在输出中被打印出来。

英文:

this counts each letters frequency in the given string.
s is your string

for n in s: gets every character in s and one by one puts them in n, then execute the loop for n

at first dict.keys() return an empty array so keys = []
so for any character that n is not in keys array else will be executed and dict[n]=1

then in next loops if any of characters exist in keys array (lets say your in third loop and keys = ['g', 'o']) then the value of that key will be value=value + 1 ( {'g':1, 'o':1} -> {'g':1, 'o':2} )
at last char_freq returns dict (a dictionary) which gets printed in the output

答案4

得分: 0

这个函数的作用是接受一个字符串,在这种情况下是'google',它会创建一个空字典{}
然后它会遍历字符串中的所有字符
在'google'中,
首先它会获取字符'g',然后检查它是否存在于字典中,如果不存在,它会将'g'添加到字典中,值为1,例如({'g': 1})
然后它会获取字符'o',并执行相同的操作,查看是否存在于字典中,如果不存在,它会将'o'添加到字典中,值为1,例如({'o': 1}),现在当前的字典看起来是这样的{'g': 1, 'o': 1}
然后它会再次获取字符'o',但这次它已经存在,所以它会增加字典中'o'的数量,例如({'o': 2}),现在当前的字典看起来是这样的{'g': 1, 'o': 2}
以此类推,直到{'g': 2, 'o': 2, 'l': 1, 'e': 1}。

英文:

What this function does is it takes a string in your case it is 'google'
now what it will do is it creates an empty dict {}
It will loop through all the characters in the string
in google
first it will get character 'g' it will check if it exists in the dict if its not it will add the 'g' in dictionary with 1 e.g({'g': 1})
then it will get char 'o' it will do the same and look if its not it will add the 'o' in dictionary with 1 e.g({'o': 1}) and now the present dictionary looks like {'g': 1, 'o': 1}
then it will get char 'o' again but this time it has o in it so it will increment the number of o in the dict e.g({'o': 2}) and now the present dictionary looks like {'g': 1, 'o': 2}
and so on to {'g': 2, 'o': 2, 'l': 1, 'e': 1}

huangapple
  • 本文由 发表于 2023年7月17日 11:37:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701348.html
匿名

发表评论

匿名网友

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

确定