实验二

x = list(range(10))
print('整数输出1: ', end = '')
for i in x:
    print(i, end=' ')
print('\n整数输出2: ', end = '')
for i in x:
    print(f'{i:02d}', end = '-')
print('\n整数输出3: ', end = '')
for i in x[:-1]:
    print(f'{i:02d}', end = '-')
print(f'{x[-1]:02d}')
print('\n字符输出1: ', end = '')
y1 = [str(i) for i in range(10)]
print('-'.join(y1))
print('字符输出2: ', end = '')
y2 = [str(i).zfill(2) for i in range(10)]
print('-'.join(y2))
复制代码

复制代码
name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan',
'cocteau twins']

for name in name_list:
    print(name.title())
print()

name_list_captilize = [name.title() for name in name_list]
print('\n'.join(name_list_captilize))
复制代码

name_list = ['david bowie', 'louis armstrong', 'leonard cohen', 'bob dylan','cocteau twins']
name_list=sorted(name_list)
name_list_captilize = [name.title() for name in name_list]
print('\n'.join(name_list_captilize))

复制代码
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!'''
hang=len(text.splitlines())
zifu=len(text)
words=len(text.split())
kongge=0
for i in text:
    if i==" ":
        kongge+=1
print("行数",hang)
print("字符",zifu)
print("单词",words)
print("空格",kongge)
复制代码

 

复制代码
'''
某.csv格式数据文件内数据如下:
99 81 75
30 42 90 87
69 50 96 77 89, 93
82, 99, 78, 100
'''
data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
x=" ".join(data)
x=x.split(' ')
y=len(x)
sum=0
for i in range(y):
    sum=sum+int(x[i])
print(f'{sum/y:.2f}')

 

 

 

复制代码

words_sensitive_list = ['张三', 'V字仇杀队', '杀']
comments_list = ['张三因生命受到威胁正当防卫导致过失杀人,经辩护律师努力,张三不需负刑事责任。',
                 '电影<V字仇杀队>从豆瓣下架了',
                 '娱乐至死']
comments_list="\n".join(comments_list)
for i in words_sensitive_list:
    x=len(i)
    if i in comments_list:
        comments_list =comments_list .replace(i,"*"*x)
print(comments_list)
复制代码

 

 五。实验总结

我学到了字符串、列表、格式化、列表推导式、遍历等基础操作

posted @ 2022-04-11 22:26  黄海祥  阅读(36)  评论(3编辑  收藏  举报