实验2 字符串4.9

def:

所有  感悟  总结用法……都在截图 代码的注释里面

 

 

task 7

 

#task   4 敏感词汇打码
words_sensitive_list = ['张三', 'V字仇杀队', '']
comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。',
                 '电影<V字仇杀队>从豆瓣下架了',
                 '娱乐至死']

for com in comments_list:
    for word in words_sensitive_list:
        if word in com:
            com = com.replace(word,'*'*len(word))
    print(com)

task6

sum = 0
num = 0
csv = [99,81,75,
       30,42,90,87,
       69,50,96,77,89,93,
       82,99,78,100]
for i in csv:
    sum +=int(i)
    num +=1
print('%.2f'%(sum/num))
print(f'{sum/num:<4.4}')

 

book_list = [['静静的顿河', '肖洛霍夫', '金人', '人民文学出版社'],
             ['大地之上', '罗欣顿.米斯特里', '张亦琦', '天地出版社'],
             ['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'],
             ['来自民间的叛逆', '袁越', '', '新星出版社'],
             ['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'],
             ['灯塔', '克里斯多夫.夏布特', '吕俊君', '北京联合出版公司'],
             ['小行星掉在下午', '沈大成', '', '广西师范大学出版社']]
for i in range(7):
    print(f'{i+1}.《{book_list[i][0]}》|{book_list[i][1]}|{book_list[i][3]}')

task5

task4

text = '''The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!'''

print(f'字符数:{len(text)}')
print('空格数',text.count(' '))
print('单词数:',len(text.split()))
print('行数:',len(text.splitlines()))

task 3

name_list2 = ['DAnox turmo','li vion',
              'hh op']
name_list3 = sorted(name_list2)
for name1 in name_list3:
    print(f'{name1}'.title(),end='.')

name_list = ['DAxk','Tuermp','obanma','van','baNajxja',
             'tshe']
for name in name_list:
    print(name.title(),end=',')

name_list_cap = [name.title() for  name in name_list]
print('\n'.join(name_list_cap))

# 里面元素打印出来还是数字 range与list
x = list(range(10))

print('打印整数:')
for i in x :
    print(i,end='')

#输出整数宽度为2 不足用2补齐   zfill    {i:02d/f}
print('\n整数数据类型补齐:',end='')
for i in x :
    print(f'{i:02d}',end='-')
print(f'{i:}'.zfill(4),end='-')       #'???'.zfill   or    'f'{x:}'.zfill  补齐0'   (数据长度)
print('x'.zfill(2))

print('整数输出2:',end='')
for i in x[:-1]:                    #切片 1-倒数第一个数据
    print(f'{i:02d}',end='-')
print(f'{x[-1]:02d}')             #索引x的最后一个数据

#zfill join只有字符类型与{}才能用
print('\n数字输出3:')
y = [str(i) for i in x]
print('-'.join(y))              #join 是后面加前面的

print('\n数据输出3:',end='')
y1 = [str(i).zfill(2) for  i in x]
print('-'.join(y1))

 

posted @ 2022-04-09 20:28  奈落——  阅读(45)  评论(7编辑  收藏  举报