我有一個輸入檔案,其中包含以下日志條目。
200,John,/home,60ms
200,Sarah,/log,13ms
500,Jack,/home,40ms
輸出:
sarah
uj5u.com熱心網友回復:
我假設,您的資料在 txt 檔案中
file = "path/to/user_data.txt"
def find_lowest(file):
with open(file, 'r') as f:
# Create list that contains every latency
# Because you cannot know the lowest or the max latency before check them all
latencies = []
names = []
# Make a loop through users'data
for user_data in f.readlines():
data = user_data.strip('\n').split(",") # Convert a row (string) into list
latencies.append(int(data[3][:-2])) # [:-2] to remove "ms"
names.append(data[1])
return names[latencies.index(min(latencies))] # Return the first occurence
它給出一個具有最低延遲的用戶名,如果兩個相等,則僅回傳具有此延遲的第一個用戶
如果您想要一個包含所有用戶頻率最低的串列,只需將最后一行替換為:
return [names[i] for i, lat in enumerate(latencies) if lat == min(latencies)]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/475046.html
標籤:python-3.x
