我有一个输入文件,它有以下日志(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)]

Python-3.x相关问答推荐

Python gpsd客户端

pandas查找另一列中是否存在ID

如何将项目添加到Python中具有固定大小的列表列表中

在特定条件下从 DataFrame 中提取特定组

通过匹配第一列的行值,逐个单元格地添加两个Pandas 数据框中的浮点数

asyncio.as_completed() 应该接受 `Iterable`,但如果输入是 `Generator` 就会崩溃?

Pandas 在每组两个条件之间获得时间增量

如何查找 tensorflow.python.data.ops.dataset_ops.MapDataset 对象的大小或形状,make_csv_dataset 的输出

Dask worker post-processing

将字符串表示与使用整数值的枚举相关联?

PySpark python 问题:Py4JJavaError: An error occurred while calling o48.showString

tensorflow 中 numpy.newaxis 的替代方案是什么?

发送Electron邮件时的 MIMEText UTF-8 编码问题

django - 值更改后自动更新日期

如何在 Selenium 和 Python 中使用类型查找元素

将字符串拆分为最大长度 X 的片段 - 仅在空格处拆分

如何强制 Sphinx 使用 Python 3.x 解释器

连接 dict 值,它们是列表

注册 Celery 基于类的任务

Beautifulsoup 的单元测试失败