min

finding a longest list within the lists in python

孤街浪徒 提交于 2019-12-02 08:57:44
问题 I'm just a new in python and I need to print the lists which has minimum and maximum items in a list. For example if i have: total_list = [[1, 2, 3], [1, 2, 3, 4], [1,2,3,4,5]] I need to return a list with a minimum and maximum length of itself. How can I do it simply in python way? I try to iterate over it but all I could receive is the only len of each item in a list Output must be: total_list[0] and total_list[2] Thanks in advance 回答1: max and min function in python accepts a key argument

Normalisation of a two column data using min and max values

谁说胖子不能爱 提交于 2019-12-02 06:12:55
问题 I am trying to find an R code for normalisation of my values using min and max value for a two column matrix. My matrix looks like this: Column one (C1) and C2 I.D not to be calculated, C3; Heading row 1 then 407 numbers and NA´s, C4;heading row 1 then numbers and NA´s. I was thinking of something like: Min value for C3 = x, Max value for same column x, If(x="","NA",(x-Min value)/(Max value-Min value)) This would give a column with values from 0 to 1. The same should be done for column 4

How to Determine the Max and Min Values Read in From a Text File in Java

让人想犯罪 __ 提交于 2019-12-02 03:44:00
I am doing a homework assignment for a class, and am looking for some helpful pointers, not full solutions. Basically, I have to write a Java program that reads in a text file and lists the information line by line, lists the line number, and finally, prints out the maximum and minimum value and the years that relates to each. The text file contains a year and the temperature for that year. So, it lists something like, "1900 50.9." I am not meant to use an array, or the scanner, this is part of the assignment. I've already been able to successfully get the program to print out each year and

Elegant way to extract a tuple from list of tuples with minimum value of element

旧巷老猫 提交于 2019-12-01 21:50:42
I am having list of tuple from which I want the tuple with the minimum value at index 1 . For example, if my list is like: a =[('a', 2), ('ee', 3), ('mm', 4), ('x', 1)] I want the returned tuple to be ('x', 1) . Currently I am using sorted function to get this result like: min=sorted(a, key=lambda t: t[1])[0] Is there better ways to do it? Maybe with min function? Moinuddin Quadri You may use min() function with key parameter in order to find the tuple with minimum value in the list. There is no need to sort the list. Hence, your min call should be like: >>> min(a, key=lambda t: t[1]) ('x', 1)

Index from accumarray with max/min

ぐ巨炮叔叔 提交于 2019-12-01 20:51:34
I have a vector and a cell array (with repeating strings) with the same size. The cell array defines the groups. I want to find min/max values in the vector for each group. For example: value = randperm(5) %# just an example, non-unique in general value = 4 1 2 3 5 group = {'a','b','a','c','b'}; [grnum, grname] = grp2idx(group); I use ACCUMARRAY function for this: grvalue = accumarray(grnum,value,[],@max); So I have new cell array with unique group name ( grname ) and new vector ( grvalue ). grname = 'a' 'b' 'c' grvalue = 4 5 3 But I also need to find location index of values from old vector

Keyword functions for Python min/max

ぃ、小莉子 提交于 2019-12-01 18:24:28
问题 I am trying to understand how this works: my_dict = {'a':2,'b':1} min(my_dict, key=my_dict.get) produces b Which is a really cool feature and one I want to understand better. Based on the documentation min(iterable[, key]) Return the smallest item in an iterable or the smallest of two or more arguments... The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, min(a,b,c,key=func))

Why does python max('a', 5) return the string value?

倾然丶 夕夏残阳落幕 提交于 2019-12-01 18:13:03
问题 Tracing back a ValueError: cannot convert float NaN to integer I found out that the line: max('a', 5) max(5, 'a') will return a instead of 5. In the above case I used the example string a but in my actual case the string is a NaN (the result of a fitting process that failed to converge). What is the rationale behind this behaviour? Why doesn't python recognize automatically that there's a string there and that it should return the number? Even more curious is that min() does work as expected

Why does python max('a', 5) return the string value?

▼魔方 西西 提交于 2019-12-01 17:42:43
Tracing back a ValueError: cannot convert float NaN to integer I found out that the line: max('a', 5) max(5, 'a') will return a instead of 5. In the above case I used the example string a but in my actual case the string is a NaN (the result of a fitting process that failed to converge). What is the rationale behind this behaviour? Why doesn't python recognize automatically that there's a string there and that it should return the number? Even more curious is that min() does work as expected since: min('a', 5) min(5, 'a') returns 5 . Martijn Pieters In Python 2, numeric values always sort

How can I get the minimum and the maximum element of a list in python

天涯浪子 提交于 2019-12-01 12:45:46
If a have a list like: l = [1,2,3,4,5] and I want to have at the end min = 1 max = 5 WITHOUT min(l) and max(l) . Mink The fastest approach I can think of would be to sort the original list and then pick the first and last elements. This avoids looping multiple times, but it does destroy the original structure of your list. This can be solved by simply copying the list and sorting only the copied list. I was curious if this was slower than just using max() and min() with this quick example script: import time l = [1,2,4,5,3] print "Run 1" t1 = time.time() print "Min =", min(l) print "Max =",

Given value in column A, find min/max in column B in EXCEL

不羁的心 提交于 2019-12-01 11:47:38
问题 I've got a column of bus stop ID numbers (Column A) and Column B are various timings of which each value is the time a person boards/alights the bus. In order to know how long the bus is stopped, I want to find the difference between the max/min in Column B given a certain value of A. 84009 9:17:38 AM 84009 9:17:40 AM 84009 9:17:41 AM 84009 9:17:43 AM 84009 9:17:50 AM 84009 9:17:55 AM 84029 9:22:47 AM 84029 9:22:47 AM 84029 9:22:49 AM 84029 9:22:50 AM 84019 9:23:49 AM 83049 9:28:19 AM 82069 9