Skip to content

Commit 59953c8

Browse files
YangShaohansrvz
authored andcommitted
自学训练营学习18群 Day5 (#6207)
* Create 1001S02E02_hello_python.py * Create 1001S02E03_calculator.py 加减乘除运算 * Create 1001S02E04_control_flow.py 九九乘法表程序 * 1001S02E05 字符串的基本处理和数组操作
1 parent 7bb4b75 commit 59953c8

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

1901100207/1001S02E05_array.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
sample_list=[0,1,2,3,4,5,6,7,8,9]
2+
reversed_list=sample_list[::-1]
3+
print('列表翻转==>',reversed_list)
4+
joined_str=''.join([str(i) for i in reversed_list])
5+
print('翻转后的数组拼接成字符串==>',joined_str)
6+
7+
sliced_str=joined_str[2:8]
8+
print('用字符串切片的方式取出第三到第八个字符==>',sliced_str)
9+
10+
reversed_str=sliced_str[::-1]
11+
print('字符串翻转==>',reversed_str)
12+
13+
int_value=int(reversed_str)
14+
15+
print('转换为int类型==>',int_value)
16+
print('转换为二进制==>',bin(int_value))
17+
print('转换为八进制==>',oct(int_value))
18+
print('转换为十六进制==>',hex(int_value))

1901100207/1001S02E05_stats_text.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
sample_text='''
2+
The Zen of Python, by Tim Peters
3+
Beautiful is better than ugly.
4+
Explicit is better than implicit.
5+
Simple is better than complex.
6+
Complex is better than complicated.
7+
Flat is better than nested.
8+
Sparse is better than dense.
9+
Readability counts.
10+
Special cases aren't special enough to break the rules.
11+
Although practicality beats purity.
12+
Errors should never pass silently.
13+
Unless explicitly silenced.
14+
In the face of ambxiguity, refuse the temptation to guess.
15+
There should be one-- and preferably only one --obvious way to do
16+
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+
25+
elements = sample_text.split()
26+
words=[]
27+
symbols=',.*-!'
28+
for element in elements:
29+
for symbol in symbols:
30+
element = element.replace(symbol,'')
31+
if len(element):
32+
words.append(element)
33+
print('正常的英文单词==>',words)
34+
counter={}
35+
word_set= set(words)
36+
for word in word_set:
37+
counter[word]=words.count(word)
38+
print('英文单词出现的次数==>',counter)
39+
40+
print('从大到小输出所有的单词及出现的系次数==>',sorted(counter.items(),key=lambda x:x[1],reverse=True))

1901100207/1001S02E05_string.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
sample_text='''
2+
The Zen of Python, by Tim Peters
3+
Beautiful is better than ugly.
4+
Explicit is better than implicit.
5+
Simple is better than complex.
6+
Complex is better than complicated.
7+
Flat is better than nested.
8+
Sparse is better than dense.
9+
Readability counts.
10+
Special cases aren't special enough to break the rules.
11+
Although practicality beats purity.
12+
Errors should never pass silently.
13+
Unless explicitly silenced.
14+
In the face of ambxiguity, refuse the temptation to guess.
15+
There should be one-- and preferably only one --obvious way to do
16+
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+
25+
text = sample_text.replace('better','worse')
26+
print('将字符串样本里的better全部替换成worse==>',text)
27+
28+
words = text.split()
29+
filtered=[]
30+
for word in words:
31+
if word.find('ea')<0:
32+
filtered.append(word)
33+
print('将单词中包含ea的单词剔除==>',filtered)
34+
35+
swapcased=[i.swapcase()for i in filtered]
36+
print('进行大小写翻转==>',swapcased)
37+
38+
print('单词按a...z升序排列==>',sorted(swapcased))

0 commit comments

Comments
 (0)