Skip to content

Commit 2433c53

Browse files
Sally-luosrvz
authored andcommitted
【21】自学训练营 DAY5 (#6129)
* Day 1 task * Create 1001S02E02_hello_python.py Day two task. * Create 1001S02E03_calculator.py design a calculator * Day04 * Day05
1 parent 26efb6d commit 2433c53

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# DAY5 掌握基本数据类型 2019-11-03
2+
#Task3:数组操作,进制转换
3+
4+
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5+
list. reverse()
6+
print("原列表被翻转:",list)
7+
8+
# 翻转后的数组拼接成字符串
9+
list_str = "".join((str(i) for i in list))
10+
print("翻转后的数组拼接成字符串:",list_str)
11+
12+
# 用字符串切片的方式取出第二个到第八个 字符 (包含第三到第八个字符)
13+
jiequ_str = list_str[2:8]
14+
print("取出第二个到第八个 字符 (包含第三到第八个字符)",jiequ_str)
15+
16+
# 将获得的字符串进行翻转
17+
fz_str = jiequ_str[::-1]
18+
print("将获得的字符串进行翻转 :",fz_str)
19+
20+
# 将结果转换为 int 类型
21+
m = int(fz_str)
22+
print("将结果转换为 int 类型:",m)
23+
24+
print("转换为二进制:",bin(m))
25+
print("转换为八进制:",oct(m))
26+
print("转换为十六进制:",hex(m))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# DAY5 掌握基本数据类型 2019-11-03
2+
#Task2:统计字符串串样本中英⽂文单词出现的次数
3+
4+
text = '''
5+
The Zen of Python, by Tim Peters
6+
7+
Beautiful is better than ugly.
8+
Explicit is better than implicit.
9+
Simple is better than complex.
10+
Complex is better than complicated.
11+
Flat is better than nested.
12+
Sparse is better than dense.
13+
Readability counts.
14+
Special cases aren't special enough to break the rules.
15+
Although practicality beats purity.
16+
Errors should never pass silently.
17+
Unless explicitly silenced.
18+
In the face of ambiguity, refuse the temptation to guess.
19+
There should be one-- and preferably only one --obvious way to do it.
20+
Although that way may not be obvious at first unless you're Dutch.
21+
Now is better than never.
22+
Although never is often better than *right* now.
23+
If the implementation is hard to explain, it's a bad idea.
24+
If the implementation is easy to explain, it may be a good idea.
25+
Namespaces are one honking great idea -- let's do more of those!
26+
'''
27+
28+
aa = text.replace(","," ").replace("."," ").replace("--"," ").replace("!"," ").replace("*"," ").replace("\n","")
29+
new = aa.split(" ") #分割字符串,变为列表,以空格进行分割。
30+
31+
32+
print("统计单词出现次数:")
33+
d ={} #这里要用大括号
34+
for i in new :
35+
j= new.count(i) # 一个元素出现的: 次数 ,此处的i 就表示一个单词,上面已经分割好;另外用法 也可以是一个字符串中的 某一个或者几个字母,比如 'ea'
36+
d[i] = j # 这里就是把这个单词 和 出现的次数 添加到 字典中,当然也可以用 update 函数
37+
# d={i:j} #这里要用大括号
38+
d.pop("")
39+
print(d)
40+
41+
42+
print("按出现频次数从大到小输出:")
43+
# sorted 函数 排序:https://docs.python.org/zh-cn/3/howto/sorting.html#sortinghowto
44+
d1=sorted(d.items(),key=lambda x:x[1],reverse=True)
45+
print(d1)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# DAY5 掌握基本数据类型 2019-11-03
2+
3+
text='''
4+
The Zen of Python, by Tim Peters
5+
Beautiful is better than ugly.
6+
Explicit is better than implicit.
7+
Simple is better than complex.
8+
Complex is better than complicated. 9 Flat is better than nested.
9+
Sparse is better than dense.
10+
Readability counts.
11+
Special cases aren't special enough to break the rules.
12+
Although practicality beats purity.
13+
Errors should never pass silently.
14+
Unless explicitly silenced.
15+
In the face of ambxiguity, refuse the temptation to guess.
16+
There should be one-- and preferably only one --obvious way to do it.
17+
Although that way may not be obvious at first unless you're Dutch.
18+
Now is better than never.
19+
Although never is often better than *right* now.
20+
If the implementation is hard to explain, it's a bad idea.
21+
If the implementation is easy to explain, it may be a good idea.
22+
Namespaces are one honking great idea -- let's do more of those!
23+
'''
24+
#Task1-1:字符串的基本处理-替换
25+
textnew = text.replace('better','worse')
26+
print(textnew)
27+
28+
#Task1-2:字符串的基本处理-去除
29+
#首先是要找到含有ea的单词,要找到单词首先要用分割规律分割出每个单词(现在是每个单词后面有个空格)
30+
#step1 分割
31+
listnew = textnew.split( )
32+
print(listnew)
33+
#step2 把没有ea的增加到新的数列里面去
34+
listnew2 = []
35+
for i in listnew:
36+
if "ea" in i:
37+
pass
38+
else:
39+
listnew2.append(i)
40+
#step3 转化成字符串
41+
result = ""
42+
for i in listnew2:
43+
result += i + " "
44+
print(result)
45+
46+
#Task1-3:字符串的基本处理-进行⼤小写翻转
47+
textnew2 = result.swapcase()
48+
print(textnew2)
49+
50+
#Task1-4:#所有单词按a...z升序排列列,并输出结果
51+
listnew3 = textnew2.split( )
52+
listnew3.sort()
53+
print('\n单词首字母按a-z升序后新列表text5如下:\n',listnew3)

0 commit comments

Comments
 (0)