简体   繁体   中英

What is the difference between the lists

I have two lists obtained in the same way, only the first is read directly from the list, and the second is unloaded from postgresql:

List1

>>> print(type(list1))
... <class 'list'>
>>> print(list1)
... [array([-0.11152368,  0.1186936 ,  0.00150046, -0.0174517 , -0.14383622,
            0.04046987, -0.07069934, -0.09602138,  0.18125986, -0.14305925])]
>>> print(type(list1[0][0]))
... <class 'numpy.float64'>

List2

>>> print(type(list2))
... <class 'tuple'>
>>> print(list2)
... (['-0.03803351', '0.07370875', '0.03514577', '-0.07568369', '-0.07438357'])
>>> list2 = list(list2)
>>> print(type(list2))
... <class 'list'>
>>> print(list2)
... [['-0.03803351', '0.07370875', '0.03514577', '-0.07568369', '-0.07438357']]
>>> print(type(list2[0][0]))
... <class 'str'>

How do I see the difference in the elements? How can I get items like <class 'numpy.float64'> from list2?

And why is the type list1 a class 'list' if it's numpy ?

list1 is a list that contains 1 element that is an numpy.array that contains multiple floats64 .

list2 is list that contains 1 element that is a list that contains multiple strings (that happen to look a lot like floats ).

You can convert them like so:

import numpy as np

# list of list of strings that look like floats
list2 = [['-0.03803351', '0.07370875', '0.03514577', '-0.07568369', '-0.07438357']]

# list of np.arrays that contain float64's
data = list([np.array(list(map(np.float64, list2[0])))])  # python 3.x

print(data)
print(type(data))
print(type(data[0]))
print(type(data[0][0]))

Output:

[array([-0.03803351,  0.07370875,  0.03514577, -0.07568369, -0.07438357])]
<type 'list'>
<type 'numpy.ndarray'>
<type 'numpy.float64'>

As Patrick Artner wrote. If list2 contains multiple arrays, you can use:

   def string_list_to_int_list(l):
       return l.astype(float)

   converted_list = list(map(string_list_to_int_list, list2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM