❶ 使用python畫出一個三維的函數圖像,數據來自於一個Excel表格
可以的。 python利用matplotlib這個庫,先定義一個空圖層,然後聲明x,y,z的值,x,y,z賦相應的列的值,最後建立標簽,標題即可。最後,excel安裝運行python的插件,運行python。
❷ 使用python編寫函數,實現以下功能
這不就是遍歷嗎= =,怎麼又是你
def fun(s):
ret = ''
for i,elem in enumerate(s):
ret += elem + ('*' if i % 2 == 0 and ord(elem) % 2 != 0 else '')
return ret使用
s = 'ABCDEFG12345'
print fun(s)
❸ 用python語言編寫一個m行n列得表格怎麼寫
defdrawTable(iRow,iColumn,iCellW=5,iCellH=3):
deffunc(x,y):
if(x%iCellW==0)and(y%iCellH==0):
print'+',
elifx%iCellW==0:
print'|',
elify%iCellH==0:
print'-',
else:
print'',
foryinrange(iColumn*iCellH+1):
forxinrange(iRow*iCellW):
func(x,y)
print'+'
drawTable(5,3)
上例為畫一個五行三列表格的代碼示例,在python 2.7版本上測試通過。
行列數目以及單元格寬度都可通過函數參數控制。
❹ python怎樣做html的表格
現要實現python製作html格式的表格,利用Python對字元串str.format()格式化操作進行處理,在日常對CVS格式文件處理過程當中,經常會將CVS格式文件進行轉換,在正式場合是程序讀取CVS文件進行轉換並輸出到html格式的文件當中,但現在只是實現一下轉換的過程,需要輸入以逗號分隔的數據。
在設計程式的時候,需要先定義一下整個代碼的框架,首先我們要定義一個主函數main(),雖然Python沒有規定入口函數,一般在正式的開發中都設計了一個main()函數作為程序的入口函數,或許這是一種規范吧。然後我們在定義一個列印表頭的方法print_head(),並在主函數里進行調用。再定義一個列印表尾的方法print_end(),也在主函數中進行調用。定義print_line()為列印表格行,定義extract_field()處理cvs行數據轉換為list集合數據。最後再定義一個處理特殊符號的方法escape_html(),因為在html代碼中為了避免與它的標簽沖突,特要進行特殊符號的轉換,如&-->&
還有就是對長度過長的數據要進行處理並用...代替
源代碼:
#Author Tandaly
#Date 2013-04-09
#File Csv2html.py
#主函數
def main():
print_head()
maxWidth = 100
count = 0
while True:
try:
line = str(input())
if count == 0:
color = "lightgreen"
elif count%2 == 0:
color = "white"
else:
color = "lightyellow"
print_line(line, color, maxWidth)
count += 1
except EOFError:
break
print_end()
#列印表格頭
def print_head():
print("")
#列印錶行
def print_line(line, color, maxWidth):
tr = "".format(color)
tds = ""
if line is not None and len(line) > 0:
fields = axtract_fields(line)
for filed in fields:
td = "{0}".format(filed if (len(str(filed)) <= maxWidth) else
(str(filed)[:100] + "..."))
tds += td
tr += "{0}
".format(tds)
print(tr)
#列印表格尾
def print_end():
print("")
#抽取行值
def axtract_fields(line):
line = escape_html(line)
fields = []
field = ""
quote = None
for c in line:
if c in "\"":
if quote is None:
quote = c
elif quote == c:
quote = None
continue
if quote is not None:
field += c
continue
if c in ",":
fields.append(field)
field = ""
else:
field += c
if len(field) > 0:
fields.append(field)
return fields
#處理特殊符號
def escape_html(text):
text = text.replace("&", "&")
text = text.replace(">", ">")
text = text.replace("<", "<")
return text
#程序入口
if __name__ == "__main__":
main()
運行結果:
>>>
"nihao","wo"
nihaowo
"sss","tandaly"
...tandaly
"lkkkkkkkkkkksdfssssssssssssss",
34
...34
❺ 用python 畫一個網格
print函數是輸出字元串,所以用 print輸出形成的 grid其實質是具有某特定形狀的字元串,而plt.grid() 繪制出的網格是 圖片。
字元串是可以用文字處理軟體(如 word、記事本等)直接編輯處理的,而圖片則只能用圖片編輯軟體(如畫筆)打開處理。
如問題所要求的最簡單的「編寫一個能畫出如下網格(grid)的函數」代碼,可以簡單的用 print函數就可以了:
❻ 如何使用Python函數寫一個萬曆表
你得先了解歷法是怎麼計算的,在這基礎上定義變數計算得出,最後包裝一下做做交互、圖形化什麼的。
❼ 用Python編寫一個函數,繪制如下表格
不能問點有用的問題嗎?這種課後自己好好看書,自己動腦啊。
最簡單:
print('''+----+----+
|||
|||
+----+----+
|||
|||
+----+----+''')
想要自己搞的復雜點?
deff(w=5,h=3,width=30,height=50)
deffunc(x,y):
ifx%w==0andy%h==0:
print('+',end='')
elifx%w==0:
print('|',end='')
elify%h==0:
print('-',end='')
else:
print('',end='')
foryinrange(height):
forxinrange(width):
func(x,y)
print()
f()
❽ python繪制函數圖像
raw_input獲取的輸入是字元串,不能直接用np.array,需要用split進行切分,然後強制轉化成數值類型,才能用plot函數
我把你的代碼稍微修改了一下,可能不太漂亮,不過能運行了
x=[1,2,3]
a = raw_input('function>>>')
a = a.split(' ')#依空格對字元串a進行切分,如果是用逗號分隔,則改成a.split(',')
b = []
for i in range(len(a)):#把切分好的字元強制轉化成int類型,如果是小數,將int改為float
b.append(int(a[i]))
plt.plot(x, b, label='x', color="green", linewidth=1)
❾ 請用python語言編寫一個四行四列的表格
print'''
+-----+-----+-----+-----+
|||||
|||||
+-----+-----+-----+-----+
|||||
|||||
+-----+-----+-----+-----+
|||||
|||||
+-----+-----+-----+-----+
|||||
|||||
+-----+-----+-----+-----+'''
❿ python怎麼編寫函數
在python中,定義一個函數要使用def語句,依次寫出函數名、括弧、括弧中的參數和冒號:,然後在縮進塊中編寫函數體,函數的返回值用return語句返回
def 函數名([參數1,參數2,...,參數n]):
函數體
函數代碼以def關鍵詞開頭,後接函數標識符名稱和圓括弧():
任何傳入參數和自變數必須放在圓括弧中間,圓括弧之間可以用於定義參數
函數的第一行語句可以選擇性的使用文檔字元串-用於存放函數說明
函數內容以冒號起始,並且縮進
return[表達式]結束函數,選擇性的返回一個值給調用方,不帶表達式的return相當於返回None