首页 > 其他分享 >实验2 字符串和列表

实验2 字符串和列表

时间:2023-03-29 20:36:23浏览次数:42  
标签:end list len 列表 better print while 实验 字符串

task1.py

x = 'nba FIFA'
print(x.upper())
print(x.lower())
print(x.swapcase())
print()
x = 'abc'
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())
x = '222test'
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.py

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):
    print(f'{x[i]:02d}', end='-')
    i += 1


print('\n整数输出3:',end='')
i = 0
while i < len(x) - 1:
    print(f'{x[i]:02d}',end='-')
    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.py

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

#方法1
i = 0
while i < len(name_list):
    print(name_list[i].title())
    i += 1
print()
#方法2
t = []
i = 0
while i < len(name_list):
    t.append(name_list[i].title())
    i += 1
print('\n'.join(t))

task4.py

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

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

 task5.py

str1='''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(str1.splitlines())}')
print(f'单词数:{len(str1.split())}')
print(f'字符数:{len(str1)}')
print(f'空格数:{str1.count(" ")}')

 

book_list = [['静静的顿河','肖洛霍夫','金人', '人民文学出版社'],
['大地之上','罗欣顿.米斯特里','张亦琦', '天地出版社'],
['夜航西飞', '柏瑞尔.马卡姆', '陶立夏', '人民文学出版社'],
['来自民间的叛逆', '袁越', '','新星出版社'],
['科技与恶的距离', '珍妮.克里曼', ' 詹蕎語', '墨刻出版社'],
['灯塔','克里斯多夫.夏布特','吕俊君','北京联合出版公司'],
['小行星掉在下午','沈大成', '', '广西师范大学出版社']]
print(f"{'图书信息':*^40}")
import copy
b = copy.deepcopy(book_list)     #后续del操作会对book-list修改,因此此处先保存原有的book-list,待del操作后再还原,以便需要查询原book-list时可以查到
i = 0
while i < len(book_list):
    a = book_list[i]
    del(a[2])
    print(f'{i + 1}. {"|".join(a)}')
    i += 1
book_list = b

data = ['99 81 75', '30 42 90 87', '69 50 96 77 89 93', '82 99 78 100']
i = 0
count = 0
ct = 0
while i < len(data):
    a = data[i]
    b = a.split()
    ct += len(b)
    x = 0
    while x < len(b):
        c = b[x]
        count += int(c)
        x += 1
    i += 1
d = count/ct
print(f'{d:.2f}')

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

"""
家用电器销售系统
v1.1
"""

#欢迎信息
print('欢迎使用家用电器销售系统!')

#商品数据
products=[
['0001','电视机','海尔',5999.00,20],
['0002','冰箱','西门子',6998.00,15],
['0003','洗衣机','小天鹅',1999.00,10],
['0004','空调','格力',3900.00,0],
['0005','热水器','格力',688.00,30],
['0006','笔记本','联想',5699.00,10],
['0007','微波炉','苏泊尔',480.00,33],
['0008','投影仪','松下',1250.00,12],
['0009','吸尘器','飞利浦',999.00,9]
]

#产品信息列表
print('产品和价格信息如下:')
print('*'*57)
print(f"{'编号': <10} {'名称': <10} {'品牌': <10} {'价格': <10} {'库存数量': <10} ")
for i in products:
    j = 0
    while j < len(i):
        print(i[j], end = ' '*(12 - len(str(i[j]))))
        j += 1
    print('',end = '\n')
print('-'*51)

#用户输入信息
product_id = input('请输入您要购买的产品编号:')
count = int(input('请输入您要购买的产品数量:'))

#输入编号对应商品的信息
product_index = int(product_id) - 1
product = products[product_index]

#计算金额
print('购买成功,您需要支付', product[3]*count, '元')

#退出系统
print('谢谢您的光临,下次再见!')

 

标签:end,list,len,列表,better,print,while,实验,字符串
From: https://www.cnblogs.com/yuanlinuist/p/17243192.html

相关文章

  • 实验2
    task1:1x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())5print()67x='abc'8print(x.center(10,'*'))9print(x.ljust......
  • 实验二
    任务一1x='nbaFIFA'2print(x.upper())3print(x.lower())4print(x.swapcase())5x='abc'6print(x.center(10,'*'))7print(x.ljust(10,'*'))8print......
  • 拼多多商品列表接口,关键词搜索拼多多商品接口,拼多多优惠券接口代码封装教程
    业务场景:作为全球最大的B2C电子商务平台之一,拼多多平台提供了丰富的商品资源,吸引了大量的全球买家和卖家。为了方便开发者接入拼多多平台,拼多多平台提供了丰富的API接口......
  • JavaScript系列 -> 字符串方法 replace 的第二个参数为函数
    本篇文章要介绍,当字符串的replace方法第二个参数为函数的使用。示例代码:functionfn(str){this.str=str;}fn.prototype.format=function(){vararg=......
  • Stanford CS 144, Lab 0: networking warmup 实验
    StanfordCS144,Lab0:networkingwarmupFinishStanfordCS144lab0andpassthetest.2023/03/29-01:45目录StanfordCS144,Lab0:networkingwarmup2Ne......
  • 轴刻度为字符串
    上面的程序运行起来,X轴的刻度是数字,如果我们希望轴刻度是文字怎么做呢?我们参考了这个网址的介绍: https://stackoverflow.com/questions/31775468/show-string-values-......
  • 字符串函数_Linux_python_R_SQL
    字符串处理grep查找命令筛选信息awkcut按照指定要求分割-awk截取列信息cut截取字符串信息awkFS对第一行没作用要用大写的BEGIN方法01.利用exp......
  • python-docx表格样式列表
    示例源码importdocxwdoc=docx.Document()table=wdoc.add_table(rows=2,cols=2)row=table.rows[0]row.cells[0].text='书号'row.cells[1].text='我的著......
  • linux在多个文件中查找指定字符串
    Linux使用grep命令检索多个文件点击查看代码grep<searchingstring><patternsearchingfile>如果我要检索当前所有md文件中的Hello关键字,可以这么用点击查看代......
  • 了解一下实验室马弗炉面对不同试验的方法
    实验室马弗炉原理高温炉加热室用耐火材料及碳化硅、氧化镁、氧化铝等制成,电热丝为镍铬合金丝。实验室马弗炉外部由铁板制成,涂以皱纹漆,炉门上有一小孔,嵌以透明的云母片,......