JSONArray into 2d numpy array [closed] - python
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a json array in this format in PYTHON. How to make it to a 2d numpy array?
[{"z":1,"y":0.10000000149011612,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.10000000149011612,"x":0.20000000298023224},{"z":0.5,"y":0.10000000149011612,"x":0.30000001192092896},{"z":0.6666666865348816,"y":0.10000000149011612,"x":0.4000000059604645},{"z":0.3333333432674408,"y":0.10000000149011612,"x":0.5},{"z":0.1666666716337204,"y":0.10000000149011612,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.10000000149011612,"x":0.699999988079071},{"z":0.5,"y":0.10000000149011612,"x":0.800000011920929},{"z":0.5,"y":0.25999999046325684,"x":0.10000000149011612},{"z":0.5,"y":0.25999999046325684,"x":0.20000000298023224},{"z":1,"y":0.25999999046325684,"x":0.30000001192092896},{"z":0.1666666716337204,"y":0.25999999046325684,"x":0.4000000059604645},{"z":0.1666666716337204,"y":0.25999999046325684,"x":0.5},{"z":0.5,"y":0.25999999046325684,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.25999999046325684,"x":0.699999988079071},{"z":0.6666666865348816,"y":0.25999999046325684,"x":0.800000011920929},{"z":0.5,"y":0.41999998688697815,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.41999998688697815,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.41999998688697815,"x":0.30000001192092896},{"z":0.6666666865348816,"y":0.41999998688697815,"x":0.4000000059604645},{"z":0.5,"y":0.41999998688697815,"x":0.5},{"z":0.8333333134651184,"y":0.41999998688697815,"x":0.6000000238418579},{"z":0.8333333134651184,"y":0.41999998688697815,"x":0.699999988079071},{"z":0.5,"y":0.41999998688697815,"x":0.800000011920929},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.10000000149011612},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.30000001192092896},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.4000000059604645},{"z":0.8333333134651184,"y":0.5799999833106995,"x":0.5},{"z":0.5,"y":0.5799999833106995,"x":0.6000000238418579},{"z":0.6666666865348816,"y":0.5799999833106995,"x":0.699999988079071},{"z":0.1666666716337204,"y":0.5799999833106995,"x":0.800000011920929},{"z":0.3333333432674408,"y":0.7400000095367432,"x":0.10000000149011612},{"z":1,"y":0.7400000095367432,"x":0.20000000298023224},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.30000001192092896},{"z":0.3333333432674408,"y":0.7400000095367432,"x":0.4000000059604645},{"z":1,"y":0.7400000095367432,"x":0.5},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.6000000238418579},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.699999988079071},{"z":0.1666666716337204,"y":0.7400000095367432,"x":0.800000011920929}]
The array look like: ([[1,2,3],[4,5,6]]) Every json object is a []
As I understand, json_array is a list of dictionaries
m = np.zeros((len(json_array), 3))
for i, elem in enumerate(json_array):
m[i,0] = elem['x']
m[i,1] = elem['y']
m[i,2] = elem['z']
Related
"while True" intermediate solutions [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself. Closed 8 years ago. Improve this question I'm working with big multidimensional arrays. I want to know after x loops the values S,S2,E,E2 I want to find all the solutions of a loop. I have a code that iterates with a greedy. My output is a list of numers. I want to save the arrays that generate those numbers. My code: while True: if f2<f: f,S,S2=f2,E,E2 # f is a value and S and E are arrays ...... print f2-f else: break If i run this code I have a list of f2-f but I need also to save the f,S,S2,f2,E,E2 created in each passage of the while.. Thank you
results = [] while True: if f2<f: f,S,S2=f2,E,E2 # f is a value and S and E are arrays ...... results.append((f,S,S2,E,E2)) print f2-f print "intermediate result:", results[-1] else: break
Check if part of text is in tuple [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist Closed 9 years ago. Improve this question How to check if part of text is in tuple? For example: my_data = ((1234L,), (23456L,), (3333L,)) And we need to find if 123 or 1234 is in tuple. I didn't worked lot with tuples before. In array we use: if variable in array But its not working for tuples like my_data PS. 1st answer solved problem.
def findIt(data, num): num = str(num) return any(num in str(i) for item in data for i in item) data = ((1234L,), (23456L,), (3333L,)) print findIt(data, 123) Output True
python parsing string using regex [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist Closed 9 years ago. Improve this question I need to parse a string from this: CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt To this: ES1-HER / ES1-Seura Any easy way to do this with regex?
>>> import re >>> s = 'CN=ERT234,OU=Computers,OU=ES1-HER,OU=ES1-Seura,OU=RES-ES1,DC=resu,DC=kt,DC=elt' >>> re.findall('OU=([^,]+)', s) ['Computers', 'ES1-HER', 'ES1-Seura', 'RES-ES1'] >>> re.findall('OU=([^,]+)', s)[1:3] ['ES1-HER', 'ES1-Seura'] >>> ' / '.join(re.findall('OU=([^,]+)', s)[1:3]) 'ES1-HER / ES1-Seura' Don't use str as a variable name. It shadows builtin function str.
Python List sorting by columns [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist Closed 9 years ago. Improve this question If I had a list ['xxxx', 'oooo', 'xxxx', 'oooo'......etc] which looks like (xxxx) (oooo) (xxxx) (oooo) and the list could be as long as the user inputs, how would I make a new list sorted by each column which would look like: ['xoxo', 'xoxo', 'xoxo', 'xoxo'] which would be (xoxo) (xoxo) (xoxo) (xoxo)
myList=['xxxx', 'oooo', 'xxxx', 'oooo'] print [''.join(element) for element in zip(*myList)] Output ['xoxo', 'xoxo', 'xoxo', 'xoxo'] What you are looking for, is called, transposing an array. That can be achieved with zip function.
how to convert timecode into seconds:fractions_of_a_second? [closed]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist Closed 9 years ago. Improve this question Given timecode in this format 00:00:00,0 What is the best way to convert this into seconds.fractions_of_a_second?
Here's a link for some python code to handle SMPTE timecodes http://code.google.com/p/pytimecode/ Hope it helps...
The datetime module is your friend in this case import datetime time_string = "17:48:12,98" t = datetime.datetime.strptime(time_string, "%H:%M:%S,%f") seconds = 60 * t.minute * t.hour print (seconds, t.microsecond)