寒光博客

py小练习
第一题 杨辉三角 N = 8 t = [[0 for i in range(N)] for i in range(...
扫描右侧二维码阅读全文
21
2020/09

py小练习

第一题

杨辉三角

N = 8
t = [[0 for i in range(N)] for i in range(N)]  # 二维list 8*8
for i in range(N):
    for j in range(i + 1):
        if j == 0 | j == i:
            t[i][j] = 1
        else:
            t[i][j] = t[i - 1][j] + t[i - 1][j - 1]
        print(t[i][j], end=' ')
    print()
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
[6, 1, 3, 3, 1, 3, 7, 2, 1, 6, 3, 1, 6, 1, 6, 2, 2, 6, 3, 1, 3]

第二题

set 解法

poetry = """
桃之夭夭,灼灼其华。
之子于归,宜其室家。
桃之夭夭,有蕡其实。
之子于归,宜其家室。
桃之夭夭,其叶蓁蓁。
之子于归,宜其家人。
"""

p_set = list(set(poetry))
p_count = [[] for i in range(len(p_set))]  # 创建二维数组
for i in range(len(p_set)):
    p_count[i] = poetry.count(p_set[i])
print(p_count)
for i in range(len(p_set)):
    if p_set[i] == "\n" or p_set[i] == "":
        continue
    print("{} 出现了 {}次".format(p_set[i], p_count[i]))

poetry 简单方法

print("*******方法二*******")
for i in p_set:
    if i == '\n': continue
    count_s = poetry.count(i)
    print("{} 出现了 {}次".format(i, count_s))
之 出现了 6次
实 出现了 1次
于 出现了 3次
桃 出现了 3次
有 出现了 1次
宜 出现了 3次
蓁 出现了 2次
人 出现了 1次
夭 出现了 6次
子 出现了 3次
华 出现了 1次
。 出现了 6次
叶 出现了 1次
其 出现了 6次
灼 出现了 2次
室 出现了 2次
, 出现了 6次
归 出现了 3次
蕡 出现了 1次
家 出现了 3次

第三题

Star

english = """Twinkle, twinkle, little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky.
Twinkle, twinkle, little star
How I wonder what you are
When the blazing sun is gone,
When he nothing shines upon
Then you show your little light
Twinkle, twinkle, all the night.
Twinkle, twinkle, little star
How I wonder what you are
Then the traveller in the dark,
Thanks you for your tiny spark,
Could he see which way to go,
If you did not twinkle so.
Twinkle, twinkle, little star
How I wonder what you are
In the dark blue sky you keep,
Often through my curtains peep,
For you never shut your eye,
Till the sun is in the sky.
Twinkle, twinkle, little star.

一闪一闪小星星

How I wonder what you are."""
english = english.replace(',', '')
english = english.replace('\n', '')
english = english.replace('.', ' ')
english = english.replace('一闪一闪小星星', '')
english = english.lower()
english = english.split(' ')
e_set = list(set(english))
print(e_set)
for i in e_set:
    if i == '': continue
    count_s = english.count(i)
    print("{}  {}".format(i, count_s))
['', 'peepfor', 'my', 'diamond', 'above', 'little', 'dark', 'in', 'all', 'world', 'lighttwinkle', 'areup', 'he', 'how', 'you', 'star', 'highlike', 'darkthanks', 'eyetill', 'traveller', 'i', 'blue', 'keepoften', 'shines', 'your', 'curtains', 'so', 'arethen', 'are', 'uponthen', 'is', 'nothing', 'see', 'which', 'to', 'for', 'what', 'arein', 'night', 'not', 'twinkle', 'starhow', 'did', 'show', 'tiny', 'way', 'sparkcould', 'shut', 'arewhen', 'through', 'blazing', 'gonewhen', 'sun', 'wonder', 'the', 'goif', 'sky', 'never', 'a']

peepfor  1
my  1
diamond  1
above  1
little  6
dark  1
in  3
all  1
world  1
lighttwinkle  1
areup  1
he  2
how  1
you  10
star  1
highlike  1
darkthanks  1
eyetill  1
traveller  1
i  5
blue  1
keepoften  1
shines  1
your  3
curtains  1
so  2
arethen  1
are  1
uponthen  1
is  2
nothing  1
see  1
which  1
to  1
for  1
what  5
arein  1
night  1
not  1
twinkle  12
starhow  4
did  1
show  1
tiny  1
way  1
sparkcould  1
shut  1
arewhen  1
through  1
blazing  1
gonewhen  1
sun  2
wonder  5
the  9
goif  1
sky  3
never  1
a  1
本文作者:Author:     文章标题:py小练习
本文地址:https://dxoca.cn/StudyNotes/382.html       百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
Last modification:September 21st, 2020 at 03:44 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment

One comment

  1. 银联POS机 Firefox 79.0 Windows 10 中国 广东 惠州

    赞!混个脸熟,博客真好看୧(๑•̀⌄•́๑)૭