在一个项目中,我有几个具有多边形值的列表,如下所示:[143788.505, 523767.385, 143788.519, 523767.065, 143791.926 523767.213, 143792.216 523760.528, 143804.22 523761.099, 143803.924 523766.718, 143800.893 523766.573]列表中的第一个元素是X值,第二个是Y,第三个X,第四个Y等。如果我使用这样的函数,它将获得完整列表的最小值。如何获得x和y的最小值?def my_min(sequence): """return the minimum element of sequence""" low = sequence[0] # need to start with some value for i in sequence: if i < low: low = i return low最小X将是143788.505最小Y将是:523760.528 最佳答案 设定L = [143788.505, 523767.385, 143788.519, 523767.065, 143791.926, 523767.213, 143792.216, 523760.528, 143804.22, 523761.099, 143803.924, 523766.718, 143800.893, 523766.573]如果您对整体坐标感兴趣,则可以调整列表的形状,将相应的X和Y值分组为元组。您可以使用zip完成此操作:coords = list(zip(L[::2], L[1::2]))[(143788.505, 523767.385), (143788.519, 523767.065), (143791.926, 523767.213), (143792.216, 523760.528), (143804.22, 523761.099), (143803.924, 523766.718), (143800.893, 523766.573)]现在,要分别找到具有最小X和Y值的坐标,请使用min键,并将该键设置为您感兴趣的元组的元素(对于X为0,对于):>>> min(coords, key=lambda x: x[0])(143788.505, 523767.385) # coordinate with smallest x>>> min(coords, key=lambda x: x[1])(143792.216, 523760.528) # coordinate with smallest y如果您不关心坐标,仅关心值,请在列表的切片上调用Y:>>> min(L[::2])143788.505>>> min(L[1::2])523760.528关于python - 获取具有多边形结构的列表的最小值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52762714/
10-09 12:42