简体   繁体   中英

Convert a list to a numpy array

I'm currently iterating through a dictionary and creating a list of strings, each one composed of a key / value from the dictionary, using this:

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

This gives me something like this:

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

I need to be able to reference a particular string in the list by index number or possibly by the first few letters in the string. I think what I want to do is use an array instead of a list. Not only will I be referencing multiple strings, from multiple arrays, I'll need to update certain strings with new strings. Would it be better if I used numpy for this?

Could you please suggest the best way to use an array instead of a list for this?

This method seems fine to me if you are having trouble identifying the index for each one you can use enumerate your lists and return the two indexes for the item you are locating.

Although I would recommend staying with the dictionaries, which most likely should be able to accomplish your task

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 

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