首页 > 其他分享 >实验二

实验二

时间:2023-03-23 18:33:53浏览次数:32  
标签:list better while 实验 print data than

task1
x='nba FIFA' print(x.upper()) print(x.lower()) print(x.swapcase()) print() x='abc' print(x.center(10,'*')) print(x.center(10,'*')) print(x.ljust(10,'*')) print(x.rjust(10,'*')) print() x='123' print(x.zfill(10)) x=123 print(str(x).zfill(10)) print() x='phone_number'

print(x.isidentifier())
print()

x=' '
print(x.isspace())
x='\n'
print(x.isspace())
print()

x='python is fun'
table=x.maketrans('thon','1234')
print(x.translate(table))

task2

x=[1,9,8,4,2,0,49]

print('整数输出1:',end='')
i=0
while i<len(x):
    print(x[i],end='')
    i+=1

print('\n整数输出2:',end='')
i=0
while i<len(x):
    print(f'{x[i]:02d}',end='-')
    i+=1

print('\n整数输出3:',end='')
i=0
while i<len(x)-1:
    i+=1
print(f'{x[-1]:02d}')

print('\n字符输出1:',end='')
y1=[]
i=0
while i<len(x):
    y1.append(str(x[i]))
    i+=1
print('-'.join(y1))

print('字符输出2:',end='')
y2=[]
i=0
while i<len(x):
    y2.append(str(x[i]).zfill(2))
    i+=1
print('-'.join(y2))

task3

name_list=['david bow','louis arm','leonard coh','bob dvl','cocteau twi']

i=0
while i<len(name_list):
    print(name_list[i].title())
    i+=1

print()


t=[]
i=0
while i<len(name_list):
    t.append(name_list[i].title())
    i+=1

print('\n'.join(t))

task4

name_list=['david bow','louis arm','leonard coh','bob dvl','cocteau twi']
i=0
while i<len(name_list):
    print(name_list[i].title())
    i+=1
name_list.sort()
print(name_list)
i=1
while i<6:
    print(i,'.',name_list[i-1].title())
    i+=1

 

task5

x=""
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('行数:',x.count('\n'))
alpha=0
b=0
for i in x:
    if i.isalpha():
        alpha+=1
    else:
        b+=1
print('字符数:',alpha+b)
word=len(x.split())
print('单词数:',word)
print('空格数:',x.count(' '))

 

 Task6 

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

 

 Task7

data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
title='Remote Interpreter Reinitialized'
print(title.center(40,'*'))
data=','.join(data)
import re
data_list = re.findall(r"\d+",data)
data_list=list(map(int,data_list))
i=0
sum=0

while i< len(data_list):
    sum+=data_list[i]
    i+=1
av=sum/(len(data_list))
import decimal
decimal.getcontext().rounding="ROUND_HALF_UP"
av=decimal.Decimal(av).quantize(decimal.Decimal("0.00"))

print(av)

 

 

 Task8

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

 

标签:list,better,while,实验,print,data,than
From: https://www.cnblogs.com/M-H-Bi/p/17248462.html

相关文章

  • 实验2
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1568#defineR2701intmain(){intnumber;inti;sr......
  • 实验2 输入输出和控制语句应用编程
     任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sran......
  • 实验二
    x='nbaFIFA'print(x.upper())print(x.lower())print(x.swapcase())print()x='abc'print(x.center(10,'*'))print(x.ljust(10,'*'))print(x.rjust(10,'*'......
  • 实验2
    1、实验任务1task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;int......
  • 实验2 字符串和列表
    实验任务1task11x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())5print()67x='abc'8print(x.center(10,'*'))9p......
  • 实验2
    task1源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<math.h>#defineN5#defineR1663#defineR2701intmain(){intnumber,i......
  • 实验2
    #include"stdafx.h"#include<stdlib.h>#include<stdio.h>int_tmain(intargc,_TCHAR*argv[]){inta1,a2,a3;charc1,c2,c3;doublex,y;scanf("......
  • 实验二
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){ intnumber; inti; srand(time(0)); f......
  • 实验二
    实验任务1task1.c源代码#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;int......
  • 实验二
    #task1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineR1586#defineR2701intmain(){intnumber;inti;sran......