selfteaching / selfteaching/selfteaching-python-camp

Day06 字典函数values,keys,items的区别

Open
#844 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

学习心得分享
Dominant language
Python
Stars
151
Forks
875
PR merge metrics
No merged PRs in 30d

Description

#用for循环做字频统计

    for word in text:
        if word in count.keys():
            count[word] = count[word] + 1
        else:
            count[word]=1

这里就用到了keys()
这里我尝试了使用items和values,发现他们的相同点是都是查看字典中的元素,返回一个list;
不同在于items表示的是(键:值),keys表示(键),values表示(值),比如:
dict = { 'a' : 1, 'boy' : 2, 3 : 'word' }

执行 dict.items() 得到:[('a', 1), ('boy', 2), (3 ,'word')]

执行dict.keys()得到:['a', 'boy', 3]

执行dict.values() 得到:[1, 2, 'word']

回到做字频统计的命令,这里如果用values就只能得到所有不相同的单词,用items就得不到结果,所以用keys去寻找键(单词),然后得到最终统计结果。

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or entry point is named in the issue. First verify the stated Python dictionary behavior against the project’s relevant lesson material, then identify where this explanation belongs; done means a clear, corrected explanation of keys(), values(), and items() in the appropriate lesson.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.