判断题 偏易
#16771

执行以下代码后,输出结果是1。 text = "i love china" char_count = { } for char in text : if cha…

执行以下代码后,输出结果是1。

text = "i love china"
char_count = {}
for char in text:
    if char != ' ':
        char_count[char] = char_count.get(char, 0) + 1
print(char_count.get('i', 0))
未登录也能判分,登录后才会记录
答案与解析
正确答案:对
解析
遍历字符串text中的每个字符;若字符不是空格,则在字典char_count中以该字符为键,值为其出现次数;通过get方法获取字符对应计数,若不存在则返回0并加1;最后通过get方法查询键'i'对应的值,若不存在则返回0。