numpy /弄平列表

发布于 2025-02-11 15:37:14 字数 779 浏览 0 评论 0原文

我创建了这个字符

list1 = [['20']*3,['35']*2,['40']*4,['10']*2,['15']*3]

结果的此:

[['20', '20', '20'], ['35', '35'], ['40', '40', '40', '40'], ['10', '10'], ['15', '15', '15']]

来将其转换为单个列表

charlist = [x for sublist in list1 for x in sublist]
print(charlist)
['20', '20', '20', '35', '35', '40', '40', '40', '40', '10', '10', '15', '15', '15']

我可以使用列表理解我想知道如何使用numpy

listNP=np.array(list1)

给出的输出

array([list(['20', '20', '20']), list(['35', '35']),
       list(['40', '40', '40', '40']), list(['10', '10']),
       list(['15', '15', '15'])], dtype=object)

:事实是 listnp.flatten()给出输出相同的结果。可能我错过了将列表转换为Numpy数组的步骤

I have create this of character

list1 = [['20']*3,['35']*2,['40']*4,['10']*2,['15']*3]

result :

[['20', '20', '20'], ['35', '35'], ['40', '40', '40', '40'], ['10', '10'], ['15', '15', '15']]

I can convert it into a single list using list comprehension

charlist = [x for sublist in list1 for x in sublist]
print(charlist)
['20', '20', '20', '35', '35', '40', '40', '40', '40', '10', '10', '15', '15', '15']

I was wondering how to do that with numpy

listNP=np.array(list1)

gives as output :

array([list(['20', '20', '20']), list(['35', '35']),
       list(['40', '40', '40', '40']), list(['10', '10']),
       list(['15', '15', '15'])], dtype=object)

The fact is that listNP.flatten() gives as an output the same result. Probably I missed a step when converting the list into an numpy array

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尾戒 2025-02-18 15:37:14

您可以绕过所有额外的操作,并使用np.repeat

>>> np.repeat(['20', '35', '40', '10', '15'], [3, 2, 4, 2, 3])
array(['20', '20', '20', '35', '35', '40', '40', '40', '40',
       '10', '10', '15', '15', '15'], dtype='<U2')

如果您需要dtype = object,请首先将第一个参数放入数组:

arr1 = np.array(['20', '35', '40', '10', '15'], dtype=object)
np.repeat(arr1, [3, 2, 4, 2, 3])

You can bypass all the extra operations and use np.repeat:

>>> np.repeat(['20', '35', '40', '10', '15'], [3, 2, 4, 2, 3])
array(['20', '20', '20', '35', '35', '40', '40', '40', '40',
       '10', '10', '15', '15', '15'], dtype='<U2')

If you need dtype=object, make the first argument into an array first:

arr1 = np.array(['20', '35', '40', '10', '15'], dtype=object)
np.repeat(arr1, [3, 2, 4, 2, 3])
半﹌身腐败 2025-02-18 15:37:14

使用 hstack()

import numpy as np
list1 = [['20']*3,['35']*2,['40']*4,['10']*2,['15']*3]
flatlist = np.hstack(list1)

print(flatlist)

['20''20'20''35'35''30'40'40'40''40''10'10''10'15'15'15'15']

试图构建您的< code> listnp 带有np.array与您在OP中所做的那样,我对锯齿阵列有一个警告,不得不使用dtype = object,但是让<代码> hstack 直接构建它不会引起警告(感谢评论中的@michael delgado)

Use hstack()

import numpy as np
list1 = [['20']*3,['35']*2,['40']*4,['10']*2,['15']*3]
flatlist = np.hstack(list1)

print(flatlist)

['20' '20' '20' '35' '35' '40' '40' '40' '40' '10' '10' '15' '15' '15']

In trying to construct your ListNP with np.array as you do in the OP, I got a warning about jagged arrays and having to use dtype=object, but letting hstack construct it directly doesn't evoke a warning (thanks @Michael Delgado in the comments)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文