获取包含具有最大值的列表的列表

时间:2017-10-14 15:43:55

标签: python list

我有一个包含坐标和其他参数的列表。我必须创建一个新列表或编辑具有最大参数的重复坐标的初始列表。 例如:

list = [

[(34.5464,58.3435),35]
[(34.5464,58.3435),17]
[(34.5464,58.3435),23]
[(36.6575,62.6545),89]
[(31.2101,45.9789),2]
[(31.2101,45.9789),12]
[(37.5462,48.7521),78]

]

我需要这个:

list_dub_max = [

[(34.5464,58.3435),35]
[(31.2101,45.9789),12]

]

2 个答案:

答案 0 :(得分:2)

使用StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { @Override public void onResponse(String response) { loading.dismiss(); showJSON(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getViolation.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); } }); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); } private void showJSON(String response) { try { String lnumber; String violation; JSONObject jsonObject = new JSONObject(response); JSONArray result = jsonObject.getJSONArray(getConfig.JSON_ARRAY); JSONObject violationData = result.getJSONObject(0); lnumber = violationData.getString(getConfig.KEY_LNUMBER); violation = violationData.getString(getConfig.KEY_VIOLATION); textViewResult.setText("License Number:\t"+lnumber+"\nViolation:\t"+violation); } 之类的:

dict

如果您需要>>> data = [[(34.5464, 58.3435), 35], [(34.5464, 58.3435), 17], [(34.5464, 58.3435), 23], [(36.6575, 62.6545), 89], [(31.2101, 45.9789), 2], [(31.2101, 45.9789), 12], [(37.5462, 48.7521), 78]] >>> result={} >>> for t,v in data: x = result.get(t) if x: if x<v: result[t]=v else: result[t] = v >>> result = result.items() >>> result [((37.5462, 48.7521), 78), ((31.2101, 45.9789), 12), ((36.6575, 62.6545), 89), ((34.5464, 58.3435), 35)] list代替list list(如上所述),您可以这样做(就像其他答案一样):

tuple

答案 1 :(得分:1)

这应该做:

def getFilteredList(l):
    list_dub_max = {}
    for val in l:
        if (val[0] in list_dub_max and list_dub_max[val[0]] < val[1]) or val[0] not in list_dub_max:
             list_dub_max[val[0]] = val[1]
    return [[k,v] for k,v in list_dub_max.iteritems()]

其中llist

l = [
[(34.5464,58.3435),35],
[(34.5464,58.3435),17],
[(34.5464,58.3435),23],
[(36.6575,62.6545),89],
[(31.2101,45.9789),2],
[(31.2101,45.9789),12],
[(37.5462,48.7521),78],
]