Skip to content

Instantly share code, notes, and snippets.

@JarenGlover
Last active August 29, 2015 14:12
Show Gist options
  • Save JarenGlover/68180ffb5b7f45c9c1c2 to your computer and use it in GitHub Desktop.
Save JarenGlover/68180ffb5b7f45c9c1c2 to your computer and use it in GitHub Desktop.
JSON --> Iterable object (List)
Python 2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> numbers = '[20.0, 30.0, 40.0, 20.0, 30.0, 40.0, 35.0, 10.0, 0.0, 15.0, 25.0, 30.0, 10.0, 35.0, 30.0, 30.0, 30.0, 40.0, 25.0, 30.0, 35.0, 15.0, 65.0, 20.0]'
>>> type(numbers)
<type 'str'>
>>> numbers.strip(r'[|]')
'20.0, 30.0, 40.0, 20.0, 30.0, 40.0, 35.0, 10.0, 0.0, 15.0, 25.0, 30.0, 10.0, 35.0, 30.0, 30.0, 30.0, 40.0, 25.0, 30.0, 35.0, 15.0, 65.0, 20.0'
>>> numbers_strip = numbers.strip(r'[]')
>>> map(float,numberes_strip.split(','))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'numberes_strip' is not defined
>>> map(float,numbers_strip.split(','))
[20.0, 30.0, 40.0, 20.0, 30.0, 40.0, 35.0, 10.0, 0.0, 15.0, 25.0, 30.0, 10.0, 35.0, 30.0, 30.0, 30.0, 40.0, 25.0, 30.0, 35.0, 15.0, 65.0, 20.0]
>>> list_float = map(float,numbers_strip.split(',')) # list comprehesion --> [ float(s) for s in list_float.split(',')]
>>> type(list_float)
<type 'list'>
>>> list_float[4]
30.0
>>> float(list_float[4])
30.0
>>> type(list_float[4])
<type 'float'>
>>> list_float
[20.0, 30.0, 40.0, 20.0, 30.0, 40.0, 35.0, 10.0, 0.0, 15.0, 25.0, 30.0, 10.0, 35.0, 30.0, 30.0, 30.0, 40.0, 25.0, 30.0, 35.0, 15.0, 65.0, 20.0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment