Python实验、组合数据类型的使用及中文文本词频统计、词云展示

实验代码

1.编写程序,实现生成随机六位验证码的功能。验证码是随机生成的、包含、多个大写字母、小写字母或数字的随机字符序列。

import random
lst = []
for i in range(6):
    num = random.randint(0,3)
    if num == 0:
        lst.append(chr(random.randint(65,90)))
    elif num == 1:
        lst.append(chr(random.randint(97,122)))
    else:
        lst.append(str(random.randint(0,9)))
print(''.join(lst))

2.求解一组不定长数据的基本统计值,即平均值、标准差、中位数。

import math
def getNum():
    num = input('请输入一组数据,以空格分隔:')
    num = num.split(' ')
    return num
def mean(numbers):
    sum = 0
    for i in numbers:
        sum += float(i)
    return sum / len(numbers)
def dev(numbers, mean):
    sum = 0
    for i in numbers:
        sum += (float(i) - mean) ** 2
    return math.sqrt(sum / len(numbers))
def median(numbers):
    numbers.sort()
    if len(numbers) % 2 == 0:
        return (float(numbers[int(len(numbers) / 2) - 1]) + float(numbers[int(len(numbers) / 2)])) / 2
    else:
        return float(numbers[int(len(numbers) / 2)])
def main():
    nums = getNum()
    print('平均值:%.2f' % mean(nums))
    print('标准差:%.2f' % dev(nums, mean(nums)))
    print('中位数:%.2f' % median(nums))
main()

3.英文文章进行词频统计及词云展示。

import wordcloud
def getText():
    txt = open("ABC.txt", "r",encoding = 'utf-8').read()  //ABC.TXT
    txt = txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
        txt = txt.replace(ch, " ")
    return txt

wordTxt = getText()
words = wordTxt.split()
counts = {}
for word in words:
    counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))

#词云展示
wc = wordcloud.WordCloud(width=1000, height=700, background_color='white',
                         font_path='D:\Hub\src/simhei.ttf',
                         min_font_size=10, max_font_size=200)
wc.generate_from_frequencies(counts)
wc.to_file('pywcloud.png')  //生成jpg文件

4.中文文章进行词频统计及词云展示。

import jieba
import wordcloud
txt = open("xyj.txt", "r", encoding="gb18030").read()  //xyj.txt 西游记
excludes = {"一个","怎么","我们","不知","不能","如此","不是","甚么","那里","和尚"}
words = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word) == 1:
        continue
    elif word == "行者" or word == "大圣" or word == "悟空":
        rword = "悟空"
    elif word == "三藏" or word == "唐僧" or word == "师父":
        rword = "唐僧"
    else:
        rword = word
    counts[word] = counts.get(word,0) + 1
for word in excludes:      //排除词
    del counts[word]
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))

#词云展示
w = wordcloud.WordCloud(width=1000, height=700, background_color="white", font_path="D:\Hub\src/simhei.ttf",max_words = 10)
w.generate_from_frequencies(counts)
w.to_file("wordcloud.png")
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值