1. 寫一個20行以上的python簡單代碼
classPrice:
ticket_d=100
ticket_w=ticket_d*1.2
defrq(self):
self.a=int(input('請輸入是平日還是周末(平日:1/周末:0):'))
ifself.a==0:
self.p=self.ticket_w
ifself.a==1:
self.p=self.ticket_d
defpj(self):
input('請輸入是人數'+' ')
self.ad=int(input('大人數量:'))
self.ch=int(input('兒童數量:'))
self.money=self.p*self.ad+self.p*self.ch/2
print('%f'%self.money)
classTicket():
def__init__(self,weekend=False,child=False):
self.exp=100
ifweekend:
self.inc=1.2
else:
self.inc=1
ifchild:
self.discount=0.5
else:
self.discount=1
defcalcPrice(self,num):
returnself.exp*self.inc*self.discount*num
alt=Ticket()
child=Ticket(child=True)
print("2個成人+1個小孩平日票價為:%.2f"%(alt.calcPrice(2)+child.calcPrice(1)))
2. 如何用python統計單詞的頻率
代碼:
passage="""Editor』s Note: Looking through VOA's listener mail, we came across a letter that asked a simple question. "What do Americans think about China?" We all care about the perceptions of others. It helps us better understand who we are. VOA Reporter Michael Lipin begins a series providing some answers to our listener's question. His assignment: present a clearer picture of what Americans think about their chief world rival, and what drives those perceptions.
Two common American attitudes toward China can be identified from the latest U.S. public opinion surveys published by Gallup and Pew Research Center in the past year.
First, most of the Americans surveyed have unfavorable opinions of China as a whole, but do not view the country as a threat toward the United States at the present time.
Second, most survey respondents expect China to pose an economic and military threat to the United States in the future, with more Americans worried about the perceived economic threat than the military one.
Most Americans view China unfavorably
To understand why most Americans appear to have negative feelings about China, analysts interviewed by VOA say a variety of factors should be considered. Primary among them is a lack of familiarity.
"Most Americans do not have a strong interest in foreign affairs, Chinese or otherwise," says Robert Daly, director of the Kissinger Institute on China and the United States at the Washington-based Wilson Center.
Many of those Americans also have never traveled to China, in part because of the distance and expense. "That means that like most human beings, they take short cuts to understanding China," Daly says.
Rather than make the effort to regularly consume a wide range of U.S. media reports about China, analysts say many Americans base their views on widely-publicized major events in China's recent history."""
passage=passage.replace(","," ").replace("."," ").replace(":"," ").replace("』","'").
replace('"'," ").replace("?"," ").replace("!"," ").replace(" "," ")#把標點改成空格
passagelist=passage.split(" ")#拆分成一個個單詞
pc=passagelist.()#復制一份
for i in range(len(pc)):
pi=pc[i]#這一個字元串
if pi.count(" ")==len(pi):#如果全是空格
passagelist.remove(pi)#刪除此項
worddict={}
for j in range(len(passagelist)):
pj=passagelist[j]#這一個單詞
if pj not in worddict:#如果未被統計到
worddict[pj]=1#增加單詞統計,次數設為1
else:#如果統計過了
worddict[pj]+=1#次數增加1
output=""#按照字母表順序,製表符
worddictlist=list(worddict.keys())#提取所有的單詞
worddictlist.sort()#排序(但大小寫會出現問題)
worddict2={}
for k in worddictlist:
worddict2[k]=worddict[k]#排序好的字典
print("單次 次數")
for m in worddict2:#遍歷輸出
tabs=(23-len(m))//8#根據單次長度輸入,如果復制到表格,請把此行改為tabs=2
print("%s%s%d"%(m," "*tabs,worddict[m]))
註:加粗部分是您要統計的短文,請修改。我這里的輸出效果是:
American 1
Americans 9
Center 2
China 10
China's 1
Chinese 1
Daly 2
Editor's 1
First 1
Gallup 1
His 1
Institute 1
It 1
Kissinger 1
Lipin 1
Looking 1
Many 1
Michael 1
Most 2
Note 1
Pew 1
Primary 1
Rather 1
Reporter 1
Research 1
Robert 1
S 2
Second 1
States 3
That 1
To 1
Two 1
U 2
United 3
VOA 2
VOA's 1
Washington-based1
We 1
What 1
Wilson 1
a 10
about 6
across 1
affairs 1
all 1
also 1
among 1
an 1
analysts 2
and 5
answers 1
appear 1
are 1
as 2
asked 1
assignment 1
at 2
attitudes 1
base 1
be 2
because 1
begins 1
beings 1
better 1
but 1
by 2
came 1
can 1
care 1
chief 1
clearer 1
common 1
considered 1
consume 1
country 1
cuts 1
director 1
distance 1
do 3
drives 1
economic 2
effort 1
events 1
expect 1
expense 1
factors 1
familiarity 1
feelings 1
foreign 1
from 1
future 1
have 4
helps 1
history 1
human 1
identified 1
in 5
interest 1
interviewed 1
is 1
lack 1
latest 1
letter 1
like 1
listener 1
listener's 1
mail 1
major 1
make 1
many 1
means 1
media 1
military 2
more 1
most 4
negative 1
never 1
not 2
of 10
on 2
one 1
opinion 1
opinions 1
or 1
others 1
otherwise 1
our 1
part 1
past 1
perceived 1
perceptions 2
picture 1
pose 1
present 2
providing 1
public 1
published 1
question 2
range 1
recent 1
regularly 1
reports 1
respondents 1
rival 1
say 2
says 2
series 1
short 1
should 1
simple 1
some 1
strong 1
survey 1
surveyed 1
surveys 1
take 1
than 2
that 2
the 16
their 2
them 1
they 1
think 2
those 2
threat 3
through 1
time 1
to 7
toward 2
traveled 1
understand 2
understanding 1
unfavorable 1
unfavorably 1
us 1
variety 1
view 2
views 1
we 2
what 2
who 1
whole 1
why 1
wide 1
widely-publicized1
with 1
world 1
worried 1
year 1
(應該是對齊的,到這就亂了)
註:目前難以解決的漏洞
1、大小寫問題,無法分辨哪些必須大寫哪些只是首字母大寫
2、's問題,目前如果含有隻能算為一個單詞里的
3、排序問題,很難做到按照出現次數排序
3. 如何用python分別提取出某個像素的rgb值並寫入一個一行三列的數組中。
可以使用 Python Image Library 做,load() 函數會返回一個對象,這個對象我們可以把它當作一個二維數組對待,而數組中存放的就是點的 RGB 值,可以很容易地訪問到任何像素點的 RGB 值:
fromPILimportImage
#可以支持很多種圖片格式.
im=Image.open("your_picture.jpg")
pix=im.load()
#獲得圖片的尺度,可以用於迭代
printim.size
#獲得某個像素點的RGB值,像素點坐標由[x,y]指定
printpix[x,y]
#設置[x,y]點的RGB的值為value
pix[x,y]=value
4. python兩個列表比較
直接遍歷即刻
for i in a:
if i in b:
c.append (i)
5. 如何入門 Python 爬蟲
http://wenku..com/link?url=hTf6rVqe3--z_,詳細信息可以參考這個,希望可以幫到你
6. Python作業題,要求定義一個函數輸入ABCDE一串字母,每個字母代表一個分數,計算平均分。
出現那個錯誤,是因為自定義函數zh沒有返回值,所以導致a=zh(cj[i]);a沒有類型(NoneType)
我幫你把Python程序改完了,你看看吧(注意程序的縮進,因為python程序依賴縮進判斷程序邏輯)
#!/usr/bin/python
#conding=utf-8
importstring
defzh(m):
iford(m)==ord("A"):
return95
iford(m)==ord("B"):
return85
iford(m)==ord("C"):
return75
iford(m)==ord("D"):
return65
iford(m)==ord("E"):
return40
cj=str(input("請輸入五門成績:"))
zcj=0;
foriinrange(0,5):
a=zh(cj[i]);
zcj=zcj+a;
pjcj=zcj*1.0/5;
print("平均成績為:{}".format(pjcj))
7. 如何在 Python 中模擬 post 表單來上傳文件
在機器上安裝了Python的setuptools工具,可以通過下面的命令來安裝 poster:
1
<a href="https://www..com/s?wd=easy_install&tn=44039180_cpr&fenlei=-4Bmy-bIi4WUvYETgN-" target="_blank" class="-highlight">easy_install</a> poster
裝完之後,安裝下面代碼就可以實現post表單上傳文件了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
# 在 urllib2 上注冊 http 流處理句柄
register_openers()
# 開始對文件 "DSC0001.jpg" 的 multiart/form-data 編碼
# "image1" 是參數的名字,一般通過 HTML 中的 <input> 標簽的 name 參數設置
# headers 包含必須的 Content-Type 和 Content-Length
# datagen 是一個生成器對象,返回編碼過後的參數
datagen, headers = multipart_encode({"image1": open("DSC0001.jpg", "rb")})
# 創建請求對象(localhost伺服器IP地址,5000<a href="https://www..com/s?wd=%E6%9C%8D%E5%8A%A1%E5%99%A8%E7%AB%AF%E5%8F%A3&tn=44039180_cpr&fenlei=-4Bmy-bIi4WUvYETgN-" target="_blank" class="-highlight">伺服器埠</a>)
request = urllib2.Request("http://localhost:5000/upload_image", datagen, headers)
# 實際執行請求並取得返回
print urllib2.urlopen(request).read()
8. 什麼是文件後綴
文件的後綴名,即文件的擴展名,是操作系統用來標志文件類型的一種機制,是一個類型的元數據。
舉例:「小說.txt」的文件名中,小說是主文件名,txt為擴展名(文本、外語全稱:Text),表示這個文件是一個純文本文件。
一個文件可以有或沒有擴展名。對於打開文件操作,沒有擴展名的文件需要選擇程序去打開它,有擴展名的文件會自動用設置好的程序去嘗試打開,文件擴展名是一個常規文件的構成部分,但一個文件並不一定需要一個擴展名。
(8)pjpython擴展閱讀
常用的文件擴展名
1、doc/docx
表示:Word文檔,用微軟的word等軟體打開。
2、wps
表示:Wps文字編輯系統文檔,用金山公司的wps軟體打開。
3、xls/xlsx
表示:Excel電子表格,用微軟的excel軟體打開。
4、ppt/pptx
表示:Powerpoint演示文稿,用微軟的powerpoint等軟體打開。
5、rar
表示:WinRAR壓縮文件,用WinRAR等打開 。
6、pdf
表示:可移植文檔格式,用用pdf閱讀器打開(比如Acrobat)、用pdf編輯器編輯
7、dwg
表示:CAD圖形文件,用AutoCAD等軟體打開。
8、exe
表示:可執行文件、可執行應用程序,是Windows視窗操作系統。