获取具有多边形结构的列表的最小值

时间:2018-10-11 14:33:46

标签: python

在一个项目中,我有几个具有如下多边形值的列表:

[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

1 个答案:

答案 0 :(得分:3)

设置

const testDiv = document.getElementById('test');
const resultDiv = document.getElementById('result');

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

testDiv.addEventListener('mouseup', function(e) {
    const selectedText = getSelectionText();
    resultDiv.textContent = selectedText;
})

如果您对整体坐标感兴趣,则可以调整列表的形状,以将相应的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为0,X为1):

Y

如果您不关心坐标,而只关心值,请在列表的切片上调用>>> 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

min