使用dict和array的Pythonic快速方法

2024-10-02 02:44:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理非常大的数据量。我的代码中有一个计算需要花费很多时间。代码如下:

chester_output_connections = ['0', '5', '0', '1', '0', '2', '0', '3', '0', '4', '1', '9', '1', '2', '1', '6', '1', '8', '2', '9', '2', '3', '2', '8', '3', '9', '3', '7', '3', '8']

dic = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, '2fit': 7}

for index,item in enumerate(chester_output_connections):
    for k,v in dic.items():
        if str(v)==str(item):
            chester_output_connections[index]=str(k)

请为更好的运行时间提出建议。你知道吗


Tags: 代码inforoutputindex时间itemsitem
1条回答
网友
1楼 · 发布于 2024-10-02 02:44:54

如果不将比较转换为字符串,可能会有帮助

if str(v)==str(item):

可能(因为项已经是字符串)

if str(v)==item:

可能会加速一点??你知道吗

相关问题 更多 >

    热门问题