Python序列與檔案程式設計練習

2021-09-26 13:25:30 字數 4247 閱讀 2536

使用以下語句儲存乙個字串:

string = 『my moral standing is: 0.98765』

將其中的數字字串轉換成浮點數並輸出。

# -*- coding: utf-8 -*-

"""spyder editor

this is a temporary script file.

"""s =

"my moral standing is: 0.98765"

p =float

(s.split(

":")[1

])print

(p)

自定義函式move_substr(s, flag, n),將傳入的字串s按照flag(1代表迴圈左移,2代表迴圈右移)的要求左移或右移n位(例如對於字串abcde12345,迴圈左移兩位後的結果為cde12345ab,迴圈右移兩位後的結果為45abcde123),結果返回移動後的字串,若n超過字串長度則結果返回-1。__main__模組中從鍵盤輸入字串、左移和右移標記以及移動的位數,呼叫move_substr()函式若移動位數合理則將移動後的字串輸出,否則輸出「the n is too large」。

# -*- coding: utf-8 -*-

"""spyder editor

this is a temporary script file.

"""def

mv(s, flag, n):if

(n>

len(s)):

return-1

else:if

(flag==1)

:return s[n:

]+s[

:n]else

:return s[

-n:]

+s[:

-n]s,flag,n=

input

("please input s flag n"

).split(

)ans = mv(s,

int(flag)

,int

(n))

if(ans==-1

):print

("n's too large"

)else

:print

(ans)

定義函式countchar()按字母表順序統計字串中26個字母出現的次數(不區分大小寫)。例如字串「hope is a good thing.」的統計結果為:

[1, 0, 0, 1, 1, 0, 2, 2, 2, 0, 0, 0, 0, 1, 3, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0]

"""

spyder editor

this is a temporary script file.

"""def

coalpha

(s):

lst =[0

]*26for w in

range

(len

(s)):if

(s[w]

>

'a')

and(s[w]

<

'z')

: lst[

ord(s[w])-

ord(

'a')]+=

1return lst

s =input

("please input s:"

)s = s.lower(

)print

(coalpha(s)

)

1 latte

2 americano

4 mocha

# -*- coding: utf-8 -*-

"""spyder editor

this is a temporary script file.

"""def

pos(lis)

: cleaned_list =

for item in lis:

for key in item:

if key.isalpha()==

false

: item = item.replace(key,'')

return cleaned_list

coffee_list =

['32latte'

,'_americano30',,

'mocha35'

]cleaned = pos(coffee_list)i=1

for key in cleaned:

print

(i,key)

i+=1

從鍵盤輸入整數n(1-9之間),對於1-100之間的整數刪除包含n並且能被n整除的數,例如如果n為6,則要刪掉包含6的如6,16這樣的數及是6的倍數的如12和18這樣的數,輸出所有滿足條件的數,要求每滿10個數換行。

測試資料:

enter the number: 6

螢幕輸出:

1,2,3,4,5,7,8,9,10,11

13,14,15,17,19,20,21,22,23,25

27,28,29,31,32,33,34,35,37,38

39,40,41,43,44,45,47,49,50,51

52,53,55,57,58,59,70,71,73,74

75,77,79,80,81,82,83,85,87,88

89,91,92,93,94,95,97,98,99,100

# -*- coding: utf-8 -*-

"""spyder editor

this is a temporary script file.

這裡為了保證輸出的10個數在同一行中不使用列表而使用字串型別

"""n =

int(

input

("please input n"))

#這裡再次注意輸入的為字串型別

num=

0#用於判斷個數

lis=

''for i in

range(1

,101):

if(i %

10==n)or(

(i//10)

%10==n)or(

(i//

100)%10

==n)

or(i % n==0)

:continue

lis+=

str(i)

num+=

1if num==10:

print

(lis)

lis=

'' num=

0else

: lis+=

','if num!=0:

print

(lis)

請用隨機函式產生500行1-100之間的隨機整數存入檔案random.txt中,程式設計尋找這些整數的眾數並輸出,眾數即為一組數**現最多的數。

# -*- coding: utf-8 -*-

"""spyder editor

this is a temporary script file.

"""import random

with

open

('random.txt'

,'w+'

)as f:

for i in

range(1

,501):

f.write(

str(random.randint(1,

100)))

f.write(

'\n'

) f.seek(0)

ans=f.readlines(

)for key in ans:

key = key.strip(

)co =[0

]*120for key in ans:

co[int(key)]+=

1maxx=

-10000

ret=-1

for i in

range(1

,101):

if maxx < co[i]

: maxx = co[i]

ret=i

else

:continue

print

(ret)

序列與檔案程式設計練習

請用隨機函式產生500行1 100之間的隨機整數存入檔案random.txt中,程式設計尋找這些整數的眾數並輸出,眾數即為一組數中出現最多的數 import random with open random.txt w as f for i in range 500 f.write str rando...

Python 程式設計練習

進製轉換 import math def main t int input while t 0 t 1 a,b map int input split if a 0 print end a a if a 0 print 0 end t while a 0 a a bfor i in range le...

python程式設計練習1

0,設計乙個函式zip lista,listb,repl 輸入兩個列表和佔位符,返回乙個新的列表,列表裡面的每乙個元素是乙個元組,元組的元素是對應同樣順序的在lista和listb中的元素,如果長度不相同,則用佔位符來代替 例如 zip 1,2,3 4,5,6 a 返回 1,4 2,5 3,6 zi...