日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

python中倒着输出输入值_十五、深入Python输入和输出

發(fā)布時(shí)間:2024/7/5 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中倒着输出输入值_十五、深入Python输入和输出 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

「@Author:By Runsen」

在很多時(shí)候,你會(huì)想要讓你的程序與用戶(可能是你自己)交互。你會(huì)從用戶那里得到輸入,然后打印一些結(jié)果。我們可以使用input和print語(yǔ)句來(lái)完成這些功能。

input

name?=?input('your?name:')
gender?=?input('you?are?a?boy?(y/n)')

######?輸入?######
your?name:Runsen
you are a boy?:y

welcome_str?=?'Welcome?to?the?matrix?{prefix}?{name}.'
welcome_dic?=?{
????'prefix':?'Mr.'?if?gender?==?'y'?else?'Mrs',
????'name':?name
}

print('authorizing...')
print(welcome_str.format(**welcome_dic))

##########?輸出?##########
authorizing...
Welcome?to?the?matrix?Mr.?Runsen.

input函數(shù)暫停運(yùn)行,等待鍵盤輸入,直到按下回車,輸入的類型永遠(yuǎn)時(shí)字符串

a?=?input()
1
b?=?input()
2

print('a?+?b?=?{}'.format(a?+?b))
##########?輸出?##############
a?+?b?=?12
print('type?of?a?is?{},?type?of?b?is?{}'.format(type(a),?type(b)))
##########?輸出?##############
type?of?a?is?<class?'str'>,?type?of?b?is?<class?'str'>print('a?+?b?=?{}'.format(int(a)?+?int(b)))
##########?輸出?##############a?+?b?=?3

文件輸入和輸出

生產(chǎn)級(jí)別的 Python 代碼,大部分 I/O 則來(lái)自于文件

這里有個(gè)in.text,完成worldcount功能。

Mr.?Johnson?had?never?been?up?in?an?aerophane?before?and?he?had?read?a?lot?about?air?accidents,?so?one?day?when?a?friend?offered?to?take?him?for?a?ride?in?his?own?small?phane,?Mr.?Johnson?was?very?worried?about?accepting.?Finally,?however,?his?friend?persuaded?him?that?it?was?very?safe,?and?Mr.?Johnson?boarded?the?plane.

His?friend?started?the?engine?and?began?to?taxi?onto?the?runway?of?the?airport.?Mr.?Johnson?had?heard?that?the?most?dangerous?part?of?a?flight?were?the?take-off?and?the?landing,?so?he?was?extremely?frightened?and?closed?his?eyes.

After?a?minute?or?two?he?opened?them?again,?looked?out?of?the?window?of?the?plane,?and?said to his friend。

"Look?at?those?people?down?there.?They?look?as?small?as?ants,?don't?they?"

"Those?are?ants,"?answered?his?friend.?"We're?still?on?the?ground."

現(xiàn)在

  • 讀取文件
  • 去掉所有標(biāo)點(diǎn)和換行符,將大寫變?yōu)樾?/li>
  • 合并相同的詞,統(tǒng)計(jì)每個(gè)詞出現(xiàn)的頻率,將詞頻從大到小排序
  • 將結(jié)果按行輸出文件out.txt
import?re

#?你不用太關(guān)心這個(gè)函數(shù)
def?parse(text):
????#?使用正則表達(dá)式去除標(biāo)點(diǎn)符號(hào)和換行符
????text?=?re.sub(r'[^\w?]',?'',?text)

????#?轉(zhuǎn)為小寫
????text?=?text.lower()
????
????#?生成所有單詞的列表
????word_list?=?text.split('?')
????
????#?去除空白單詞
????word_list?=?filter(None,?word_list)
????
????#?生成單詞和詞頻的字典
????word_cnt?=?{}
????for?word?in?word_list:
????????if?word?not?in?word_cnt:
????????????word_cnt[word]?=?0
????????word_cnt[word]?+=?1
????
????#?按照詞頻排序
????sorted_word_cnt?=?sorted(word_cnt.items(),?key=lambda?kv:?kv[1],?reverse=True)
????
????return?sorted_word_cnt

with?open('in.txt',?'r')?as?fin:
????text?=?fin.read()

word_and_freq?=?parse(text)

with?open('out.txt',?'w')?as?fout:
????for?word,?freq?in?word_and_freq:
????????fout.write('{}?{}\n'.format(word,?freq))

##########?輸出?(省略較長(zhǎng)的中間結(jié)果)?##########



但是有個(gè)問(wèn)題,如果文件非常的大容易造成內(nèi)存奔潰

這個(gè)時(shí)候給 read 指定參數(shù) size,還可以通過(guò) readline() 函數(shù),每次讀取一行。

json文件讀取

import?json

params?=?{
????'symbol':?'123456',
????'type':?'limit',
????'price':?123.4,
????'amount':?23
}

params_str?=?json.dumps(params)

print('after?json?serialization')
print('type?of?params_str?=?{},?params_str?=?{}'.format(type(params_str),?params))

original_params?=?json.loads(params_str)

print('after?json?deserialization')
print('type?of?original_params?=?{},?original_params?=?{}'.format(type(original_params),?original_params))

##########?輸出?##########

after?json?serialization
type?of?params_str?=?<class?'str'>,?params_str?=?{'symbol':?'123456',?'type':?'limit',?'price':?123.4,?'amount':?23}
after?json?deserialization
type?of?original_params?=?<class?'dict'>,?original_params?=?{'symbol':?'123456',?'type':?'limit',?'price':?123.4,?'amount':?23}

json.dumps() 這個(gè)函數(shù),接受 Python 的基本數(shù)據(jù)類型 字典,然后轉(zhuǎn)化string (json的字符串)

json.loads() 這個(gè)函數(shù),接受一個(gè)合法字符串(json),然后 轉(zhuǎn)化為字典

「json 的讀入」

import?json

params?=?{
????'symbol':?'123456',
????'type':?'limit',
????'price':?123.4,
????'amount':?23
}

with?open('params.json',?'w')?as?fout:
????params_str?=?json.dump(params,?fout)

with?open('params.json',?'r')?as?fin:
????original_params?=?json.load(fin)

print('after?json?deserialization')
print('type?of?original_params?=?{},?original_params?=?{}'.format(type(original_params),?original_params))

##########?輸出?##########

after?json?deserialization
type?of?original_params?=?<class?'dict'>,?original_params?=?{'symbol':?'123456',?'type':?'limit',?'price':?123.4,?'amount':?23}

參考:https://time.geekbang.org/column/article/96570

?

本文已收錄 GitHub,傳送門~[1] ,里面更有大廠面試完整考點(diǎn),歡迎 Star。

?

Reference

[1]

傳送門~: https://github.com/MaoliRUNsen/runsenlearnpy100

今天的文章到這里就結(jié)束了,如果喜歡本文的話,請(qǐng)來(lái)一波素質(zhì)三連,給我一點(diǎn)支持吧(關(guān)注、在看、點(diǎn)贊)。

更多的文章

點(diǎn)擊下面小程序

- END -

總結(jié)

以上是生活随笔為你收集整理的python中倒着输出输入值_十五、深入Python输入和输出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。