My dict is like,  {'A':4,'B':10,'C':0,'D':87}   I want to find max value with its key and min value with its key.  Output will be like ,  max : 87 , key is D  min : 0  , key is C  I know how to get min and max values from dict. Is there is any way to get value and key in one statement?  max([i for i in dic.values()])  min([i for i in dic.values()])
2532
I have a dictionary: keys are strings, values are integers.  Example:   stats = {'a':1000, 'b':3000, 'c': 100}   I'd like to get 'b' as an answer, since it's the key with a higher value.  I did the following, using an intermediate list with reversed key-value tuples:  inverse = [(value, key) for key, value in stats.items()] print max(inverse)[1]   Is that one the better (or even more elegant) approach?
45224
What is the difference between max, min and sup, inf?
121693
To do the equivalent of Python list comprehensions, I'm doing the following:  some_array.select{|x| x % 2 == 0 }.collect{|x| x * 3}   Is there a better way to do this...perhaps with one method call?
41227
A lot of times in Perl, I'll do something like this:  $myhash{foo}{bar}{baz} = 1   How would I translate this to Python? So far I have:  if not 'foo' in myhash:     myhash['foo'] = {} if not 'bar' in myhash['foo']:     myhash['foo']['bar'] = {} myhash['foo']['bar']['baz'] = 1   Is there a better way?
5316
I have an NSDictionary (stored in a plist) that I'm basically using as an associative array (strings as keys and values).  I want to use the array of keys as part of my application, but I'd like them to be in a specific order (not really an order that I can write an algorithm to sort them into).  I could always store a separate array of the keys, but that seems kind of kludgey because I'd always have to update the keys of the dictionary as well as the values of the array, and make sure they always correspond.  Currently I just use [myDictionary allKeys], but obviously this returns them in an arbitrary, non-guaranteed order.  Is there a data structure in Objective-C that I'm missing?  Does anyone have any suggestions on how to more elegantly do this?
18448
Let's say I have an Array of numbers: [2,3,3,4,2,2,5,6,7,2]  What is the best way to find the minimum or maximum value in that Array?  Right now, to get the maximum, I am looping through the Array, and resetting a variable to the value if it is greater than the existing value:  var myArray:Array /* of Number */ = [2,3,3,4,2,2,5,6,7,2];  var maxValue:Number = 0;  for each (var num:Number in myArray) {     if (num &gt; maxValue)         maxValue = num; }   This just doesn't seem like the best performing way to do this (I try to avoid loops whenever possible).
61610
I have a query which selects a max value over all the values of in a datetime column (dtimesent).  select dtimesent, max(ResponseTime) AS maxtime FROM allrequests  where Request like '%Requestsa%' group by dtimesent   I need to get the value of the request colmn for each max response time in each group by. Is there any easy way to do this?  There can be multiple rows per dtimesent value so I can't just include the request column in the group by,  Many thanks,
31075
I am trying to parse the following JSON format into a Object.  {   "info1": "blah blah",   "info2": "blah blah2",   "info3": 1391673663,   "info4": "blah4",   "info5": {     "AAA": 0.1,     "BBB": 0.3, .....   } }   But the problem is under [info5] list, name like "AAA", "BBB" and their related values are dynamic and always changing.   How should I make my Object to satisfy this kind of structure? I want to put those values into a Map if possible.  So that I can easily get "AAA" and its Related values.  Any input will be so helpful for me.
112859
Is there a better way to do this?  string[] s = {"zero", "one", "two", "three", "four", "five"};  var x =  s .Select((a,i) =&gt; new {Value = a, Index = i}) .Where(b =&gt; b.Value.StartsWith("t")) .Select(c =&gt; c.Index);   i.e. I'm looking for a more efficient or more elegant way to get the positions of the items matching the criteria.
121261
How do I get for each  point an ID in the database (DB).  So far I splited the line (each 10m) but it returns only one DB entry for each original line.   v.db.select line cat|Id|Name 1|1|Line 1 2|2|Line 2  # split the lines every 10m v.to.points in=line out=line_points dmax=10  # check the result v.db.select line_points cat|Id|Name 1|1|Line 1 2|2|Line 2   Each point should have its own DB entry. The result should be:  v.db.select line_points cat|Id|Name 1|1|Line 1 2|1|Line 1 3|2|Line 2 4|2|Line 2     Workaround: So far my workaround is, to export (v.out.ogr) the points as shape and import (v.in.ogr) it again. Then each point has his own ID. But there must be a smarter way to do that!
72069
Given [1,2,3,4,5], how can I do something like   1/1, 1/2, 1/3,1/4,1/5, ...., 3/1,3/2,3/3,3/4,3/5,.... 5/1,5/2,5/3,5/4,5/5   I would like to store all the results, find the minimum, and return the two numbers used to find the minimum. So in the case I've described above I would like to return (1,5).  So basically I would like to do something like  for each element i in the list map some function across all elements in the list, taking i and j as parameters store the result in a master list, find the minimum value in the master list, and return the arguments i, jused to calculate this minimum value.  In my real problem I have a list objects/coordinates, and the function I am using takes two coordinates and calculates the euclidean distance. I'm trying to find minimum euclidean distance between any two points but I don't need a fancy algorithm.
2818
I know in JavaScript, objects double as hashes, but I have been unable to find a built-in function to get the keys: var h = {a:'b', c:'d'};  I want something like var k = h.keys() ; // k = ['a', 'c'];  It is simple to write a function myself to iterate over the items and add the keys to an array that I return, but is there a standard cleaner way to do that? I keep feeling it must be a simple built in function that I missed but I can't find it!
48764
Does anyone know how the built in dictionary type for python is implemented?  My understanding is that it is some sort of hash table, but I haven't been able to find any sort of definitive answer.
34147
I'm trying to identify null/0 values in each of my raster bands. How can I loop through all the values of each band? I've tried this code, straight from the gdal/python cookbook website but this just gives stats for each band as a whole and for some reason it actually doesn't print a minimum value of 0 even though my raster does have 0 values, it shows a minimum value of 1.   I want to print every single cell value. Is there a way to do this?   from osgeo import gdal import sys  src_ds = gdal.Open( "INPUT.tif" )  print "[ RASTER BAND COUNT ]: ", src_ds.RasterCount for band in range( src_ds.RasterCount ):     band += 1     print "[ GETTING BAND ]: ", band     srcband = src_ds.GetRasterBand(band)      stats = srcband.GetStatistics( True, True )      print "[ STATS ] =  Minimum=%.3f, Maximum=%.3f, Mean=%.3f, StdDev=%.3f" % ( \             stats[0], stats[1], stats[2], stats[3] )
12971
I have about 10million values that I need to put in some type of look up table, so I was wondering which would be more efficient a list or dict?  I know you can do something like this for both:  if something in dict_of_stuff:     pass   and  if something in list_of_stuff:     pass   My thought is the dict will be faster and more efficient.  Thanks for your help.  EDIT 1  Little more info on what I'm trying to do.  .  I'm making a look up table to see if a value calculated has all ready been calculated.   EDIT 2  Efficiency for look up.  EDIT 3  There are no values assosiated with the value...so would a set be better?
44649
What is the best way to get the Max value from a LINQ query that may return no rows? If I just do  Dim x = (From y In context.MyTable _          Where y.MyField = value _          Select y.MyCounter).Max   I get an error when the query returns no rows. I could do  Dim x = (From y In context.MyTable _          Where y.MyField = value _          Select y.MyCounter _          Order By MyCounter Descending).FirstOrDefault   but that feels a little obtuse for such a simple request. Am I missing a better way to do it?  UPDATE: Here's the back story: I'm trying to retrieve the next eligibility counter from a child table (legacy system, don't get me started...). The first eligibility row for each patient is always 1, the second is 2, etc. (obviously this is not the primary key of the child table). So, I'm selecting the max existing counter value for a patient, and then adding 1 to it to create a new row. When there are no existing child values, I need the query to return 0 (so adding 1 will give me a counter value of 1). Note that I don't want to rely on the raw count of child rows, in case the legacy app introduces gaps in the counter values (possible). My bad for trying to make the question too generic.
27892
I would like to shift data found in a column to a row by Key field. I have a table, within a geodatabase, set up similar to what's below.  KEY_    Value 123     Yellow 123     Blue 456     Red 456     Yellow 789     Green 246     Green 246     Orange   I want to create a table that moves the data in the Value field to rows based on the KEY_ field. The result will look like what's below.  KEY_    Value001    Value002 123     Yellow      Blue 456     Red         Yellow 789     Green 246     Green       Orange   Does anyone have an idea of how this can be done? I tried using the Transpose tool Esri offers but it didn't produce the result I'm looking for. I have basic understanding of Python.