table of Contents
- day07 (Data Structure)
- 1. if determined - is divided into three branch structure
- 2. for loop structure
- 3. Exception Handling (that is, handle exceptions, error Error)
- 4. Common string built-in method
- (1) index value
- (2) slices
- (3) members of the operation
- (4) for circulation
- (5) len () string length
- (6) strip (): remove the default string ends spaces
- (7) lstrip () / rstrip (): removing the string left / right
- (8) startwith () / endwith (): begin with ... / ... to end
- (9) find () / index (): Get the index position of an element
- (10) join (): the list of elements within the spliced out, generating a string
- (11) split (): cutting string, a list of
- (12) center () / ljust () / rjust (): print more beautiful, center / Left / right of abode
- (13) isdigit () / isalpha (): whether pure digital / letter if it is pure
- (14) count () Count
- == Split () Compare join () and jieba modules ==
day07 (Data Structure)
1. if determined - is divided into three branch structure
== run down follow on from ==
(1) Single branched structure
if 条件:
代码块
(2) a branched structure bis
if 条件:
代码块 条件1成立执行这里
else:
代码块 条件1不成立执行这里
(3) third structure
In the end there is no else == =='s if elif elif. . . elif, the equivalent if if if. . . if
if 条件1:
代码块 条件1成立执行这里
elif 条件2:
代码块 条件1不成立且条件2成立,执行这里
elif 条件3:
代码块 条件1,2都不成立,条件3成立,执行这里
。。。
else:
代码块 以上条件都不成立,执行这里
2. for loop structure
(1)for + break
This layer interrupt cycle, continue to the next one cycle
(2) for +continue
Interrupt this cycle, the following code is not performed (continue the last row should not, continue generally in the middle)
for i in range(1,13):
for j in range(1,32):
if i==2 and j==28:
break
elif (i in[4,6,9,11])and j==31:#elif
continue
print(f'{i}月{j}日')
#部分结果为:
...
2月25日
2月26日
2月27日
3月1日
4月28日
4月29日
4月30日
5月1日
5月2日
...
3. Exception Handling (that is, handle exceptions, error Error)
(1) capture error
- try method (try)
1.
try:
print(1/0) #0不能做分母,这里会报错,但会跳过,继续执行后面的代码
2.
try:
print(1/0)
except Exception #Exception 为万能异常,错误被捕捉了,继续执行后面的代码
pass #pass表示什么也不做
3.
try:
key = input('输入一个key获取字典中的值')
dic = {'a': 1}
dic[key] # ,输入一个错误值,如'b',而列表里没有'b',这是KeyError
except Exception as e: # Exception万能异常 # as e,是把错误信息输入出来,同时一般把该错误记录到日志中
# logging.info(e) -> 日志是给程序员看
print('你输入有问题') # -》 给用户看
# 预运行(类似把代码抽离到另外一个文件中运行,这样的运行对本文件不会造成任何影响),能不用则不用
4. Common string built-in method
Built-in method: the built-in method (will be used on the line)
String built-in method to use only string
Such as string:
s = 'nick handsome'
(1) index value
print(s[0]) #结果是 n
(2) slices
print(s[0:6:2]) #结果:nc
(3) members of the operation
print('nick' in s) #结果: True
print('nick1' in s) #结果:False
(4) for circulation
for i in s: # n i c k h a
print(i) #结果:nick handsome
#每输出一个字母都要换行,这里就为了方便,写成这样
(5) len () string length
print(len(s)) #结果:13
The following methods are strings . (Points) out of the method
(6) strip (): remove the default string ends spaces
(7) lstrip () / rstrip (): removing the string left / right
(8) startwith () / endwith (): begin with ... / ... to end
(9) find () / index (): Get the index position of an element
(10) join (): the list of elements within the spliced out, generating a string
(11) split (): cutting string, a list of
(12) center () / ljust () / rjust (): print more beautiful, center / Left / right of abode
(13) isdigit () / isalpha (): whether pure digital / letter if it is pure
(14) count () Count
# 接下来讲的都是字符串.出来的方法
s = '**** ni ck '
print(s)
# 6. strip(): 默认去掉两端空格,可以
# print(s.strip()) # 去空格
# print(s.strip('*')) # 去*
print(s.strip('n* ')) # 去 和*和n
# 7.lstrip()/rstrip(): 左端/右端
s = '**nick**'
print(s.lstrip('*'))
print(s.rstrip('*'))
# 8.startswith()/endswith(): 以。。开头/以。。结尾
s = 'nick'
print(s.startswith('ni'))
print(s.endswith('k'))
# 9.find()/index() : 获取某一个元素的索引位置
s = 'nick'
print(s.find('a')) # 找不到返回-1
# print(s.index('a')) # 找不到报错
# 10.join() : 把列表内的元素拼接出来
print('*'.join(['a', 'b', 'c']))
# 11.split(): 切割
s = 'a*b*c'
print(s.split('*')) # 以*为切割符号,切割字符串
# 12.center/ljust/rjust : 打印更美观,居中/居左/居右
print(s.center(50, '-'))
print(s.ljust(50, '-'))
print(s.rjust(50, '-'))
# 13.isdigit()/isalpha()
s = 'a'
print(s.isdigit()) # 是否为纯数字
print(s.isalpha()) # 是否为纯字母
# 14.count(): 计数
s = 'nick nick'
print(s.count('nick'))
#打印结果:
**** ni ck
i ck
nick**
**nick
True
True
-1
a*b*c
['a', 'b', 'c']
----------------------a*b*c-----------------------
a*b*c---------------------------------------------
---------------------------------------------a*b*c
False
True
2
== Split () Compare join () and jieba modules ==
== string is a list of the words that cut ==
# 2. 使用jieba模块对文件内容切割
jieba.add_word('回家的诱惑') # 让"回家的诱惑"成为一个单词
jieba.del_word('女士') #删除全部 "女士" 字符串
jieba.del_word('先生')
jieba.del_word('小马')
data_list = jieba.lcut(data) #把字符串按词语切割为列表,
data = ' '.join(data_list) #用空格把列表拼接成字符串