str ='''
China’s ’24 solar terms’ is a knowledge system and social practice formedthrough observations of the sun’s annual motion,and cognition of the year’s changes in season,climate and phenology.
The ancient Chinese divided the sun’s annual circular motion into 24 segments.Each segment was called a specific ‘Solar Term’.The 24 terms include Start ofSpring,Rain Water,Awakening of Insects,Spring Equinox,Clear and Bright,Grain Rain,Start of Summer,Grain Buds,Grain in Ear,Summer Solstice,Minor Heat,Major Heat,Start of Autumn,End of Heat,White Dew,Autumn Equinox,ColdDew,Frost’s Descent,Start of Winter,Minor Snow,Major Snow,Winter Solstice,Minor Cold and Major Cold.The elementof Twenty-Four Solar Termsoriginated in the Yellow River reaches of China.
The criteria for its formulation were developed through the observation ofchanges of seasons,astronomyand other natural phenomena in this region and has been progressively appliednationwide.It starts from the Beginning of Spring and ends with the GreaterCold,moving in cycles.The element has been transmitted from generation to generation and usedtraditionally as a timeframe to direct production and daily routines. Itremains of particular importance to farmers for guiding their practices.Havingbeen integrated into the Gregorian calendar,it is used widely by communities and shared by many ethnic groups inChina. Some rituals and festivities in China are closely associated with theSolar Terms for example,theFirst Frost Festival of the Zhuang People and the Ritual for the Beginning ofSpring in Jiuhua.
The terms may also be referenced in nursery rhymes,ballads and proverbs.The “24 solarterms” was added to the United Nations Educational,Scientific and Cultural Organization’s(UNESCO) world intangible cultural heritage list in 2017.These variousfunctions of the element have enhanced its viability as a form of intangiblecultural heritage and sustain its contribution to the community’s culturalidentity.
'''# 过滤规则:过滤掉所有非字母的字符
str1= str.replace(","," ").replace("."," ").replace("\n"," ").replace(" "," ").replace(" "," ")
#去除前后空格
str1=str1.strip()
print("过滤后的字符串:",str1)
#拆分成列表
list1 = str1.split(" ")
print("拆分成的列表:",list1)
# 生成列表作为字典的key
dict_keys = []
#使用增强的for循环遍历列表list1中的每一个元素ifor i in list1:
if i notin dict_keys:
dict_keys.append(i)
print("key列表:",dict_keys)
# 定义空字典
words_dict = {}
# 往字典写入key值
words_dict.fromkeys(dict_keys)
# 遍历key列表,利用count函数统计单词出现次数作为字典的valuefor j in dict_keys:
words_dict[j] = list1.count(j)
print("字典:",words_dict)
#统计单词的个数
print("单词的个数为:",len(words_dict.keys()),"个")
#对value进行降序排序
words_dict=sorted(words_dict.items(),key=lambda x:x[1],reverse=True)
print(words_dict)