Alternative to nesting for loops in Python

后端 未结 3 1754
小蘑菇
小蘑菇 2020-12-18 10:22

I\'ve read that one of the key beliefs of Python is that flat > nested. However, if I have several variables counting up, what is the alternative to multiple for loops? My c

3条回答
  •  春和景丽
    2020-12-18 10:58

    You can use a dictionary to optimize performance significantly

    This is another example:

    locations = {}
    for i in range(len(airports)):
        locations[airports["abb"][i][1:-1]] = (airports["height"][i], airports["width"][i])
    
    for i in range(len(uniqueData)):
        h, w = locations[uniqueData["dept_apt"][i]]
        uniqueData["dept_apt_height"][i] = h
        uniqueData["dept_apt_width"][i] = w
    

提交回复
热议问题