10 月 282020
 
# -*- coding:utf-8 -*-
lyric = 'The night begin to shine, the night begin to shine'
words = lyric.split()
print(words)

# 词频统计
# https://docs.python.org/zh-cn/3/library/stdtypes.html#str.split
# split()方法:返回一个由字符串内单词组成的列表,使用 sep 作为分隔字符串
# https://docs.python.org/zh-cn/3/library/stdtypes.html#str.count
# count()方法:返回子字符串 sub 在 [start, end] 范围内非重叠出现的次数。 可选参数 start 与 end 会被解读为切片表示法。
# https://docs.python.org/zh-cn/3/tutorial/inputoutput.html#methods-of-file-objects
# 文件对象的read()方法,读取文件内容
path = 'C:/Users/harveymei/Desktop/news.txt'
with open(path, 'r') as text:  # 只读打开文件并赋值给变量text
    words = text.read().split()  # 读取文件内容拆分单词并赋值变量
    print(words)
    for word in words:  # for循环遍历列表中元素并赋值给变量
        print('{} -{} times'.format(word, words.count(word)))  # 将元素及元素计数插入字符串

# 问题:
# 1,带标点符号的单词被单独统计
# 2,一些单词展示多次统计
# 3,首字母大写单词被单独统计
C:\Users\harveymei\PycharmProjects\mod3736\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/mod3736/static.py
['The', 'night', 'begin', 'to', 'shine,', 'the', 'night', 'begin', 'to', 'shine']
['The', 'Party', 'leads', 'the', 'formulation', 'work,', 'and', 'the', 'government', 'and', 'the', 'legislature', 'play', 'their', 'due', 'roles', 'in', 'the', 'process,', 'Li', 'said,', 'adding', 'that', 'such', 'a', 'practice', 'is', 'a', 'good', 'experience', 'that', 'the', 'CPC', 'has', 'created', 'in', 'the', 'governance', 'of', 'China.', 'The', 'Party', 'leads', 'the', 'formulation', 'work,', 'and', 'the', 'government', 'and', 'the', 'legislature', 'play', 'their', 'due', 'roles', 'in', 'the', 'process,', 'Li', 'said,', 'adding', 'that', 'such', 'a', 'practice', 'is', 'a', 'good', 'experience', 'that', 'the', 'CPC', 'has', 'created', 'in', 'the', 'governance', 'of', 'China.']
The -2 times
Party -2 times
leads -2 times
the -12 times
formulation -2 times
work, -2 times
and -4 times
the -12 times
government -2 times
and -4 times
the -12 times
legislature -2 times
play -2 times
their -2 times
due -2 times
roles -2 times
in -4 times
the -12 times
process, -2 times
Li -2 times
said, -2 times
adding -2 times
that -4 times
such -2 times
a -4 times
practice -2 times
is -2 times
a -4 times
good -2 times
experience -2 times
that -4 times
the -12 times
CPC -2 times
has -2 times
created -2 times
in -4 times
the -12 times
governance -2 times
of -2 times
China. -2 times
The -2 times
Party -2 times
leads -2 times
the -12 times
formulation -2 times
work, -2 times
and -4 times
the -12 times
government -2 times
and -4 times
the -12 times
legislature -2 times
play -2 times
their -2 times
due -2 times
roles -2 times
in -4 times
the -12 times
process, -2 times
Li -2 times
said, -2 times
adding -2 times
that -4 times
such -2 times
a -4 times
practice -2 times
is -2 times
a -4 times
good -2 times
experience -2 times
that -4 times
the -12 times
CPC -2 times
has -2 times
created -2 times
in -4 times
the -12 times
governance -2 times
of -2 times
China. -2 times

Process finished with exit code 0
10 月 282020
 
# -*- coding:utf-8 -*-
# 列表推导式 == 列表解析式
# list = [ item for item in iterable ]
# iterate:迭代
# 放在列表中的元素,就是后面循环的元素本身

a = [i ** 2 for i in range(1, 10)]  # 将1-9的平方值依次放入列表
c = [j + 1 for j in range(1, 10)]  # 将1-9分别加1的值放入列表
k = [n for n in range(1, 10) if n % 2 == 0]  # 依次判断1-10中是否为偶数并将偶数值放入列表
z = [letter.lower() for letter in 'ABCEDFGHIJKLMN']  # 将字符串中的大写字母依次转换为小写并放入列表
print(a, c, k, z)

# 字典推导式
d = {i: i+1 for i in range(4)}  # 将0-3依次作为key,0-3分别加1作为value,放入字典
print(d)
g = {i: j for i, j in zip(range(1, 6), 'abcde')}  # 将1-5依次作为key,将字符串中字符依次作为value,放入字典
print(g)
m = {i: j.upper() for i, j in zip(range(1, 6), 'abcde')}  # 将1-5依次作为key,将字符依次作为value并转换大小,放入字典
print(m)

# enumerate:枚举
# 循环列表时获取元素的索引
# 列表是有序的
# enumerate()函数,参数为可迭代对象,返回一个枚举对象。
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
for num, letter in enumerate(letters):  # 返回一个包含计数值(默认从0开始)和通过迭代获得的值
    print(letter, 'is', num + 1)

# 示例
print(list(enumerate(letters)))
print(list(enumerate(letters, start=1)))
C:\Users\harveymei\PycharmProjects\mod3736\venv\Scripts\python.exe C:/Users/harveymei/PycharmProjects/mod3736/news.py
[1, 4, 9, 16, 25, 36, 49, 64, 81] [2, 3, 4, 5, 6, 7, 8, 9, 10] [2, 4, 6, 8] ['a', 'b', 'c', 'e', 'd', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n']
{0: 1, 1: 2, 2: 3, 3: 4}
{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}
{1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
a is 1
b is 2
c is 3
d is 4
e is 5
f is 6
g is 7
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (5, 'f'), (6, 'g')]
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g')]

Process finished with exit code 0