selfteaching / selfteaching/selfteaching-python-camp

Day05 作业打印结果格式问题

Open
#2,413 4 comments 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

在作业中最后输出要求格式是字典形式 {'is': 10,'better': 9, …}
问题是:
排序用的是text_1 = sorted(dict.items(), key=lambda y: y[1], reverse=True)
输出结果是列表形式[('is', 10),。。。。。。。。, ('those', 1)]
问题出在哪来呢?怎么实现在字典中的排序呢?

代码如下
`text=text.replace(',','').replace('.','').replace('--','').replace('!','').replace('*','') #去除标点符号(没有找到更好的办法,如果文本格式很多怎么办?)
text=text.lower() #把单词全部变为小写
text=text.split() #把字符串分割成单个单词列表

dict={}
for x in text:
if x in dict:
dict[x]=dict[x]+1
else:
dict[x]=1
text_1 = sorted(dict.items(), key=lambda y: y[1], reverse=True) #key为排序依据,y【0】为key,1为y,这里不太懂,reverse为true时降序。

print(text_1)`

@oneMoreTime1357
求解答

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

The issue includes the complete Python snippet; start by reviewing the behavior of sorted(dict.items(), ...) and the requested dictionary-shaped output. Done means clarifying the expected result and addressing the question about punctuation handling in the exercise.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
content
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.