“python:函数1”解析

第一题

题目:请定义一个函数WordCount,实现词频统计的功能,要求:
输入:一段英文字符串
输出: 按照出现次数排序

例子

输入:s = “Beautiful is better than ugly,explicit is better than implicit simple is better than complex,complex is better than complicated “

输出:[(‘is’, 4), (‘better’, 4), (‘than’, 4), (‘beautiful’, 1), (‘ugly,explicit’, 1), (‘implicit’, 1), (‘simple’, 1), (‘complex,complex’, 1), (‘complicated’, 1)]

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def WordCount(text):



text = text.lower()



word_count = {}



words = text.split()



for i in words:

if i in word_count:

word_count[i] += 1

else:

word_count[i] = 1



sorted_word_count = sorted(word_count.items(), key=lambda item: item[1], reverse=True)



return sorted_word_count



text = "Beautiful is better than ugly,explicit is better than implicit simple is better than complex,complex is better than complicated. "

print(WordCount(text))


函数定义与参数

首先,我们定义一个名为WordCount的函数,它接受一个参数text,表示待处理的英文字符串。

1
def WordCount(text):

文本预处理

为了确保统计时不区分大小写,我们将输入的文本转换为小写。

1
text = text.lower()

创建字典进行词频统计

我们使用一个字典word_count来存储每个单词及其出现的次数。字典的键是单词,值是该单词出现的次数。

1
word_count = {}

分割文本并统计词频

我们将文本按照空格和标点符号分割成单词列表,并遍历该列表,更新字典中每个单词的计数。

1
2
3
4
5
6
words = text.split()
for i in words:
if i in word_count:
word_count[i] += 1
else:
word_count[i] = 1

按出现次数排序

最后,我们按照字典中每个单词的出现次数对结果进行排序。这里使用sorted函数,并指定排序的关键值为元组的第一个元素(即
单词),同时设置reverse=True以实现降序排序。

1
sorted_word_count = sorted(word_count.items(), key=lambda item: item[1], reverse=True)

返回排序结果

函数最终返回按照出现次数排序的词频统计结果。

1
return sorted_word_count

示例用法

在主程序中,我们调用WordCount函数并传入一个示例文本。根据输出结果,我们可以判断每个单词的出现次数,并按顺序输出。

1
2
3
text = "Beautiful is better than ugly,explicit is better than implicit simple is better than complex,complex is
better than complicated. "
print(WordCount(text))

结果

运行上述代码,我们将得到以下输出:

1
2
[('is', 4), ('better', 4), ('than', 4), ('beautiful', 1), ('ugly,explicit', 1), ('implicit', 1), ('simple', 1),
('complex,complex', 1), ('complicated', 1)]

这个结果表明每个单词在输入文本中出现的次数,并且已经按照出现次数进行排序。

第二题

题目:
编写函数leap实现以下功能:根据“四年闰百年不闰,四百年又闰”判断是否闰年。在主程序输入一个年份,调用leap函数判断其是否为闰年,并输出判断结果。
注意:参照代码模板完善代码,删除横线及数字部分,实现题目所述功能,不得修改其他代码。
def leap(1):
if 2
:
return 3
else:
return 4_
y=int(input())#调用函数
if leap(5)==True:
print(“%d年是闰年”%y)
else:
print(“%d年不是闰年”%y)

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def leap(year):

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):

return True

else:

return False



year = int(input())

if leap(year) == True:

print("%d年是闰年" % year)

else:

print("%d年不是闰年" % year)


函数定义与参数

首先,我们定义一个名为leap的函数,它接受一个参数year,表示待判断的年份。

1
def leap(year):

判断闰年的条件

根据题目要求,“四年闰百年不闰,四百年又闰”。我们可以将这些规则写成一个条件表达式:

  • 如果年份能被4整除但不能被100整除,则是闰年。
  • 如果年份能被400整除,则也是闰年。

我们将这些条件组合在一起,并返回相应的结果。

1
2
3
4
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False

输入并调用函数

在主程序中,我们首先通过input()函数获取用户输入的年份,并将其转换为整数类型。

1
year = int(input())

然后,我们调用leap函数,并传入输入的年份。根据函数的返回值,我们输出相应的判断结果。

1
2
3
4
if leap(year) == True:
print("%d年是闰年" % year)
else:
print("%d年不是闰年" % year)

示例用法

在主程序中,我们可以调用leap函数并传入一个示例年份。根据输出结果,我们可以判断该年份是否为闰年。

1
2
3
4
5
year = int(input())
if leap(year) == True:
print("%d年是闰年" % year)
else:
print("%d年不是闰年" % year)

结果

运行上述代码,我们输入2024将得到以下输出:

1
2024年是闰年

这个结果表明2024年是一个闰年。

通过本博客的介绍,我们了解了如何实现一个简单的判断闰年的函数leap,并对其代码进行了详细解释。希望这篇博客对你有所帮
助!

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023-2025 John Doe
  • Visitors: | Views:

请我喝杯茶吧~

支付宝
微信