簡體   English   中英

列表之間有什么區別

[英]What is the difference between the lists

我用同樣的方式獲得了兩個列表,只有第一個是直接從列表中讀取的,第二個是從 postgresql 中卸載的:

列表1

>>> 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'>

列表2

>>> 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'>

我如何看到元素的差異? 如何從 list2 中獲取<class 'numpy.float64'>項目?

如果類型 list1 是numpy ,為什么它是一個類“列表”?

list1是一個list ,其中包含1種元素,其為numpy.array包含多個floats64

list2list ,其中包含1個元素是一個list包含多個strings (碰巧看起來很像floats )。

您可以像這樣轉換它們:

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]))

輸出:

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

正如帕特里克·阿特納( Patrick Artner)所寫。 如果 list2 包含多個數組,則可以使用:

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

   converted_list = list(map(string_list_to_int_list, list2))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM