我有一个输入文件,它有以下日志(log)实体.
200,John,/home,60ms
200,Sarah,/log,13ms
500,Jack,/home,40ms
输出:
sarah
我有一个输入文件,它有以下日志(log)实体.
200,John,/home,60ms
200,Sarah,/log,13ms
500,Jack,/home,40ms
输出:
sarah
我猜,你的数据是在一个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)]