1. 程式人生 > 其它 >實驗2:字串和列表

實驗2:字串和列表

1.實驗任務1

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))

2.實驗任務2

 

name_list=['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
print('實現方式1')
for name in name_list:
    print(name.title())
print('實現方式2')
name_list_captilize=[name.title() for name in name_list]
print('\n'.join(name_list_captilize))

3.實驗任務3

name_list=['david bowie','louis armstrong','leonard cohen','bob dylan','cocteau twins']
name_list.sort()
name_list_captilize=[name.title() for name in name_list]
for i in range(5):
    print(i+1,'.',name_list_captilize[i])

4.實驗任務4

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!
'''
h=len(text.splitlines())
d=len(text.split())
z,k=0,0
for i in text:
    if i==' ':
        k+=1
        z+=1
    elif 'a'<=i<='z' or 'A'<=i<='Z':
        z+=1
    else:
        z+=1
print(f'行數:{h}')
print(f'單詞數:{d}')
print(f'字元數:{z}')
print(f'空格數:{k}')

5.實驗任務5

 



book_list = [['靜靜的頓河','肖洛霍夫','金人','人民文學出版社'],
             ['大地之上','羅欣頓.米斯特里','張亦琦','天地出版社'],
             ['夜航西飛','柏瑞爾.馬卡姆','陶立夏','人民文學出版社'],
             ['來自民間的叛逆','袁越','','新星出版社'],
             ['科技與惡的距離','珍妮.克里曼','詹蕎語','墨刻出版社'],
             ['燈塔','克里斯多夫.夏布特','呂俊君','北京聯合出版公司'],
             ['小行星掉在下午','沈大成','','廣西師範大學出版社']]
for i in range(7):
    print(i+1,end='.')
    for j in range(4):
        if j==0:
            print('《'+book_list[i][j]+'》',end=' |')
        elif book_list[i][j]=='' or j==2:
            pass
        elif j==3:
            print(book_list[i][j])
        else:
            print(book_list[i][j],end='|')

6.實驗任務6

data = ['99 81 75','30 42 90 87','69 50 96 77 89 93','82 99 78 100']
l=[]
for d in data:
    x=d.split()
    for i in x:
        l.append(i)
a,s=0,0
for i in l:
    a+=1
    s+=int(i)
average=s/a
print(f'{average:.2f}')

7.實驗任務7

words_sensitive_list = ['張三','V字仇殺隊','殺']
comments_list = ['張三因生命受到威脅正當防衛導致過失殺人,經辯護律師努力,張三不需負刑事責任。',
                 '電影<V字仇殺隊>從豆瓣下架了',
                 '娛樂至死']
for i in range(3):
    for j in range(3):
        comments_list[j]=comments_list[j].replace(words_sensitive_list[i],'*'*len(words_sensitive_list[i]))
for comment in comments_list:
    print(comment)

實驗總結:

1.第一次瞭解.zfill()用法:用於對字串格式化。.zfil(2)表示寬度為2,不足左邊補0。

2.知道了'\n'如何在print中使用:print('\n')

3.複習了.join()的用法:a,b均為字串,a.join(b)表示用a字串連線b字串。

4.'s.replace(a,b)'才是替換後的新字串,原字串並未發生變化。