0

I have a list it contains dictionaries that hold string and float data eg. [{a:'DOB', b:'weight', c:height}, {a:12.2, b:12.5, c:11.5}, {a:'DOB', b:33.5, c:33.2}] as such:

I want to convert this to numpy and strip all keys and string values so only the float values pass into the numpy array then I want to use this to work out some stats. eg [[12.2,12.5,11.5], ['', 33.5, 33.2]] where the whole row is string it will be omitted but where the item in a row is string it should keep a null value.

I'm not sure how to achieve this.

4
  • 1
    What have you tried so far? Please provide some code that at least makes an attempt to do what you want. Look up the values method for dictionaries and see how far you get with that. Are you familiar with list comprehensions? These might be useful... Also, is your dictionary statement above valid? Have you even tried it? Commented Mar 22, 2021 at 3:34
  • np.array(alist, dtype=float) is the normal way to make an array. Just iterate through the dictionary, collecting values in a list. That's straight forward Python coding. What's the problem? Commented Mar 22, 2021 at 3:47
  • Keep an eye on the answers to the previous numpy question, stackoverflow.com/questions/66739758/… Commented Mar 22, 2021 at 3:49
  • What part are you unsure of? How to break down the problem? How to construct a numpy array? How to get values from a dictionary? How to decide if a list of values contains only strings? Are a, b, c, and height all variables, or does your example miss a number of quotes? Commented Mar 22, 2021 at 3:58

1 Answer 1

0

This answer combines all the suggestions in the comments. The procedure loops thru the initial list of dictionaries and does the following:

  1. Creates a new list is using list compression, saving each dictionary value as float, or None for non-floats.
  2. Counts # of float values in the new list and appends if there is at least 1 float.
  3. Creates an array from the new list using np.array().

I added missing quotes to starting code in original post.
Also, in the future posts you should at least make an effort to code something, then ask for help where you get stuck.

test_d = [{'a':'DOB', 'b':'weight', 'c':'height'}, 
          {'a':12.2, 'b':12.5, 'c':11.5}, 
          {'a':'DOB', 'b':33.5, 'c':33.2}] 

arr_list = []    
for d in test_d:
    d_list = [x if isinstance(x, float) else None for x in d.values()]
    
    check = sum(isinstance(x, float) for x in d_list)
    if check > 0:
        arr_list.append(d_list) 
    print (arr_list)
    
arr = np.array(arr_list)
print(arr)

For reference, here is the list compression converted to a standard loop with if/else logic:

for d in test_d:
    # List compression converted to a loop with if/else below:
    d_list = []
    for x in d.values():
        if isinstance(x, float):
            d_list.append(x)
        else:
            d_list.append(None)
Sign up to request clarification or add additional context in comments.

2 Comments

this is perfect my main issues was I didn't know I had to make the dictionary into a list and my list comprehension skills are not amazing. Thank you this works great generating none values when the data is string.
I get it -- I remember when I was learning dictionaries and lists (and iteration)...so many moving parts. Note: You don't have to use list comprehension; it just puts all the logic on 1 line. A standard loop with the same logic also works (but takes 6 lines). I added that way to my answer as so you can reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.