py小练习

21
2020/09

# 第一题

## 杨辉三角

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次

。 出现了 6次

， 出现了 6次

# 第三题

## 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
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

百度已收录

Last modification：September 21st, 2020 at 03:44 pm