繁体   English   中英

将列表转换为numpy数组

[英]Convert a list to a numpy array

我目前正在遍历字典并创建一个字符串列表,每个字符串都由字典中的键/值组成,使用此命令:

listA.append(attributesA + " " + thisObjectA.attributes[attributesA])

这给了我这样的东西:

['model taurus', 'make ford', 'color white']

我需要能够通过索引号或字符串中的前几个字母来引用列表中的特定字符串。 我想我想做的是使用数组而不是列表。 我不仅要引用来自多个数组的多个字符串,还需要用新的字符串更新某些字符串。 如果我使用numpy会更好吗?

您能否建议使用数组而不是列表的最佳方法?

如果您无法确定每个索引的索引,可以使用此方法enumerate您的列表,然后为要查找的项目返回两个索引,则该方法对我来说似乎不错。

尽管我建议您留在字典中,但字典最有可能完成您的任务

lista = ['model taurus', 'make ford', 'color white']

for index, item in enumerate(lista):
    print(f"{item}: {index}")
 (xenial)vash@localhost:~/python/stack_overflow$ python3.7 yodish.py model taurus: 0 make ford: 1 color white: 2 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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