1

I'm using python and have the following dictionary:

{
    'Measurement1': {
        'point1': [['1.1', '1,2', '497.1937349917', '497.1937349917', '497.1937349917'],
                   ['3.1', '1,2', '497.6760940676', '497.6760940676', '497.6760940676'],
                   ['1.1', '3,4', '495.0455154634', '495.0455154634', '495.0455154634'],
                   ['3.1', '3,4', '497.003633083', '497.003633083', '497.003633083']]
    }
}

I am trying to get all the elements from data['Measurement1']['point1'][all_data_sets][2] to do a ','.join() for additional calculations later in the program. I'm hoping to get an output like:

'497.1937349917', '495.0455154634', '500.9453006597', '490.1952705428'

I'm currently looping through the array.

value_temp = []
for data_elem in data['Measurement1']['point1']:
    value_temp.append(data_elem[2])

output = ','.join(value_temp)   

Is there a way to grab these values without performing the loop?

3
  • you forgot to ask a question Commented Oct 11, 2017 at 1:18
  • This is actually more a topic for codereview.stackexchange.com Commented Oct 11, 2017 at 1:19
  • 1
    You might be able to see a slight speed increase with a list comprehension output=','.join([data_elem[2] for data_elem in data['Measurement1']['point1']]). You can't beat O(n) for this though (think of it this way: to write each element to the string, you have to look at it. There's no way to "skip elements") Commented Oct 11, 2017 at 1:19

4 Answers 4

1

If you want a more efficient way, you should use numpy for your whole project.

Just take your snippet as an example:

import numpy as np

# suppose your dict named "data"

data["Measurement1"]["point1"] = np.array(data["Measurement1"]["point1"])

output = ",".join(data["Measurement1"]["point1"][:, 2])

The indexing and calculation in numpy is done by C, so it is really much faster.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you are after list comprehension (which is still essentially looping through, but in a more pythonic way):

",".join([data_elem[2] for data_elem in data['Measurement1']['point1']])

although the output values don't match what is actually in your post. If this is not what you want, please clarify where the values should come from...

Comments

1

Given your original dict(dict(list(list()))),

x = {
  'Measurement1': {
    'point1': [['1.1', '1,2', '497.1937349917', '497.1937349917', '497.1937349917'],
               ['3.1', '1,2', '497.6760940676', '497.6760940676', '497.6760940676'],
               ['1.1', '3,4', '495.0455154634', '495.0455154634', '495.0455154634'],
               ['3.1', '3,4', '497.003633083', '497.003633083', '497.003633083']]
  }
}

Note that x['Measurement1']['point1'][2] is the ordinal 3rd (sub)array element. You can print this reference,

print (x['Measurement1']['point1'][2])

So, you could assign directly, use a list comprehension to reference, or simply join the elements of that (sub)array,

#use the (sub)array reference directly
','.join( x['Measurement1']['point1'][2] )
#or compose a new array using list comprehension
','.join( [ z for z in x['Measurement1']['point1'][2] ] )
#or assign array reference to variable
y = x['Measurement1']['point1'][2]
','.join( y )

You can also make a copy (to avoid accidental modification of original original,

#duplicate via list comprehension,
y = [ z for z in x['Measurement1']['point1'][2] ]
#copy the reference using list()
y = list( x['Measurement1']['point1'][2] )
#or copy.copy() #import copy
y = copy.copy( x['Measurement1']['point1'][2] )
#then
','.join( y )

Comments

0

Try using the map function.

output = ",".join(map(lambda arr: arr[2],data['Measurement1']['point1']))

Comments

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.