本文目錄一覽:
- 1、一個基礎的PYTHON代碼問題
- 2、求簡潔優美的python代碼例子、片段、參考資料
- 3、python 讀取多個csv文件中某一列,並生成一個新csv文件
- 4、【Python】有沒有大佬懂下面這段代碼思路?
一個基礎的PYTHON代碼問題
你這個原始報錯還是代碼的縮進問題,不知道你用什麼ide 來編輯運行代碼的。
#百度知道這裡的編輯是提供python代碼格式的,如下:
def findMinAndMax(L):
#max = None
#min = None #will be error: TypeError: ” not supported between instances of ‘NoneType’ and ‘int’
#max = 0 # 如果都初始為0 最小值會不正確
#min = 0
max = L[0]
min = L[0]
for n in L:
if min n:
min = n
if max n:
max = n
return(min,max)
L = [4,5,10,9,7,12,21]
a,b = findMinAndMax(L)
print(a,b)
你的原始代碼我跑成功了,另外,我修改了一些bug,min max 不能初始為None ,否則會報錯。你可以測試下 :) ,, 有其他問題再交流。
求簡潔優美的python代碼例子、片段、參考資料
建議你去看一本書:《計算機程序的構造與解釋》。裡面用的語言是Scheme,一種Lisp的方言。通過這本書學習程序的抽象、封裝,以及重要的函數式編程思想。等看完這本書以後,你在來寫寫Python代碼,就知道如何讓其簡潔直觀而又不失其可讀性了。
同時,要讓代碼寫得簡潔,你也得熟悉Python本身,充分挖掘其能力。Python內建的幾個高階函數:map,reduce,filter,enumerate等等,lambda表達式,zip函數,以及標準庫里強大的itertools、functools模塊,都是函數式編程的利器。此外Python本身提供了許多非常好的語法糖衣,例如裝飾器、生成器、*args和**kwargs參數、列表推導等等,也是簡化代碼的有效手段。還有,Python有著強大的庫。多參考官方的文檔了解其原理和細節,我相信你也能寫出高效簡潔的代碼的。
其實代碼的簡潔沒有什麼捷徑,它要求你了解你要解決的問題,所使用的語言和工具,相關的演算法或流程。這些都得靠你自己不斷地練習和持續改進代碼,不斷地專研問題和學習知識。加油吧,少年!
樓下讓你參考PEP 20,其實不用去查,標準庫里的this模塊就是它(試試import this):The Zen of Python(Python之禪)。它就是一段話:
s=”’
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!
”’
讓我們來做個小遊戲吧:統計上面這段話的單詞總數目,以及各個單詞的數量(不區分大小寫),然後按字典順序輸出每個單詞出現的次數。要求,例如it’s和you’re等要拆分成it is和you are。你會怎麼寫代碼呢?如何保持簡潔呢?
下面是我的參考答案,爭取比我寫的更簡潔吧~
import re
p = re.compile(“(\w+)(‘s|’re|n’t)?”)
wc = {}
tail_map = { “‘s” : ‘is’, “‘re” : ‘are’, “n’t”: ‘not’}
for m in re.finditer(p, s):
word = m.group(1).lower() # Get the word in lower case
wc[word] = wc.get(word, 0) + 1 # Increase word count
tail = m.group(2) # Get the word tail
if tail is not None: # If a word tail exists,
tail = tail_map[tail] # map it to its full form
wc[tail] = wc.get(tail, 0)+1 # Increase word count
print (‘Total word count: %d’%sum(wc.values())) # Output the total count
max_len = max(map(len, wc.keys())) # Calculate the max length of words for pretty printing
for w in sorted(wc.keys()): # Sort the words
print (‘%*s = %d’%(max_len, w, wc[w])) # Output
python 讀取多個csv文件中某一列,並生成一個新csv文件
csv文件應該是用逗號分隔得才對,否則怎麼算作是csv文件。樓主你開玩笑吧。否則你這只是一個普通的文本文件。如果是真正的csv文件,我只說一點,python裡面有csv模塊,專門處理csv文件。如果是空格分割應該也可以,建議你,看一下python的csv模塊的API,蠻簡單的代碼,其實如果不用的話自己寫也可以。不是很複雜。代碼片段如下:
def deal_file(file_in, file_out)
with open(file_in, ‘r’) as f_in:
with open(file_out, ‘w’) as f_out:
for line in f_in:
f_out.write(line.split(‘ ‘)[2] + ‘\n’)
之後你可以將所有的輸入文件放到一個列表裡面,進行迭代調用這個函數就可以了。
【Python】有沒有大佬懂下面這段代碼思路?
這段代碼的思路是先將所有字元按其ASCII值升序排序(list.sort(),關鍵步驟)
這樣同樣的字元就會排列在一起,再從頭開始統計每段連續出現的字元及其個數
其中a=list[0]表示從頭開始統計,a代表上一段連續出現的字元
第一個print()在else: 之後,表示遇到不一樣的字元,上一段連續出現的字元終止
那麼先print()上一段連續出現的字元及其個數,再重置a為新一段連續出現的字元
第二個print()在for循環之外,表示列印最後一段連續出現的字元及其個數
添加了注釋的代碼和運行結果如下:
關鍵在於先排序,再統計每段連續出現的字元,注意不要忘記最後一段~望採納~
原創文章,作者:HGXH,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/150041.html