[英]What is the most efficient way of counting occurrences of a bunch of specific keywords in pandas?
[英]What's the most efficient way to run through keywords/functions?
我正在開發一個用python 3.7編寫的終端應用程序。 目前,當輸入命令時,它會通過一個如下所示的函數:
def execute(command):
if command is None or command.isspace() or command == "":
terminal()
command = command.split(" ")
command = list(command)
command[0] = command[0].lower()
var(command)
iftrue(command)
... etc
每個函數看起來像這樣:
def func(command):
if command[0] == "func":
function code blah blah blah
我沒有嘗試其他方法,因為我不確定要使用什么 - 我使用這種方法是因為我看到一段很久以前使用過的代碼。
什么是最好(最有效/最優化)的方法來做到這一點? 這似乎非常浪費和緩慢,並且功能更多,列表中較低的功能可能需要花費大量時間才能到達。
我會使用一個字典,其中命令字符串是鍵,函數是值。 Dictionary 將有 log(n) 搜索時間,並且應該保持樹結構平衡。 所以將d
作為dict
,定義類似於:
d = {'func1': myFunc1, 'func2': MyFunc2...}
而且當然:
def myFunc1(args..):
...
def myFunc2(args..):
...
我們最終得到:
if cmd in d:
d[cmd](args...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.