Get the minimum and maximum values ​​from the "for in" loop

The first post, and I'm probably not doing it here, but coming here ...

How to find the maximum and minimum values ​​from the output of the "for in" loop?

I tried min () and max () and got the following error ...

TypeError: 'int' object is not iterable

here is my code ...

import urllib2
import json

def printResults(data):
  # Use the json module to load the string data into a dictionary
  theJSON = json.loads(data)

  # test bed for accessing the data
  for i in theJSON["features"]:
   t = i["properties"]["time"]
   print t

def main():
  # define a variable to hold the source URL
  urlData = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson"

  # Open the URL and read the data
  webUrl = urllib2.urlopen(urlData)
  #print webUrl.getcode()
  if (webUrl.getcode() == 200):
    data = webUrl.read()
    # print out our customized results
    printResults(data)
  else:
    print "Received an error from server, cannot retrieve results " +  str(webUrl.getcode())

if __name__ == "__main__":
   main()

Any pointers would be greatly appreciated!

+3
source share
3 answers

You can use minand maxin iterables. As you go through theJSON["features"], you can use:

print min(e["properties"]["time"] for e in theJSON["features"])
print max(e["properties"]["time"] for e in theJSON["features"])

You can also save the result in a variable, so you can use it later:

my_min = min(...)
my_max = max(...)

As @Sabyasachi commented, you can also use:

print min(theJSON["features"], key = lambda x:x["properties"]["time"])
+2
source

, .

minVal = 0
maxVal = 0
for i in yourJsonThingy:
    if i < minVal:
        minVal = i
    if i > maxVal:
        maxVal = i

:

for i in yourJsonThingy:
    maxVal = max(i)

max

int

maxVal = max(yourJsonThingy)
minVal = min(yourJsonThingy)
+1

, (, , , , , , max min , , . ):

def max_min(iterable, key=None):
    ''' 
    returns a tuple of the max, min of iterable, optional function key 
    tuple items are None if iterable is of length 0
    '''
    it = iter(iterable)
    _max = _min = next(it, None)
    if key is None:
        for i in it:
            if i > _max:
                _max = i
            elif i < _min:
                _min = i
    else:
        _max_key = _min_key = key(_max)
        for i in it:
            key_i = key(i)
            if key_i > _max_key:
                _max, _max_key = i, key_i
            elif key_i < _min_key:
                _min, _min_key = i, key_i
    return _max, _min

:

>>> max_min(range(100))
(99, 0)
>>> max_min(range(100), key=lambda x: -x)
(0, 99)

:

>>> timeit.timeit('max(range(1000)), min(range(1000))', setup=setup)
70.95577674100059
>>> timeit.timeit('max_min(range(1000))', setup=setup)
65.00369232000958

9% - , max min, , . :

>>> timeit.timeit('max(range(1000), key=lambda x: -x),min(range(1000), key=lambda x: -x)', setup=setup)
294.17539755300095
>>> timeit.timeit('max_min(range(1000), key=lambda x: -x)', setup=setup)
208.95339999899443

40% lambdas.

+1

Source: https://habr.com/ru/post/1530895/


All Articles