How to turn this Matrix into a list of integers? - python

I am trying to extract a list of integers from a input that looks like this:
[[matrix([[0.57863575]])], [matrix([[0.57170157]])], [matrix([[0.44320711]])], [matrix([[0.37195535]])]]
I am trying to get an output like so:
[0.57863575,0.57170157,0.44320711,0.37195535]
What are my options?

You can use a loop comprehension:
from numpy import matrix
l = [[matrix([[0.57863575]])], [matrix([[0.57170157]])], [matrix([[0.44320711]])], [matrix([[0.37195535]])]]
[e[0].flat[0] for e in l]
output: [0.57863575, 0.57170157, 0.44320711, 0.37195535]
The real question is, how did you get this format in the first place? It might be better to fix it there.

Related

array with pairs

How to make a pair of an array with numbers?
array = []
subjects = raw_input("Subject: ")
array.append(subjects)
When I do this I will get an array like this:
["PSP","PMT","PMF"]
I wanted my output to be like this
[("PSP",1),("PMT",2),("PMF",3)]
How can I make it like that?
Try using p.write("\n".join(entires)).
Alternatively, if you do not wish to hold the entire string in memory, you may as well loop over it like so:
for line in entries:
p.write(line)

Python - convert a list in an array

I got a list of values and i would like to convert it in an array in order to extract easily columns, but i m embarassed with " which doesn t allow to use : " x = np.array(a, dtype=float)"
['"442116.503118","442116.251106"',
'"442141.502863","442141.247462"',
...
The message obtained is :
"could not convert string to float: "442116.503118","442116.251106""
Answering based on the VERY limited information given, but if that is your list it looks like a list of nested strings, not floats. Try
x = np.array([float(i.replace("\"","")) for i in a], dtype=float)"
This is just wrong... This does the trick for me though:
import numpy as np
wtf = ['"442116.503118","442116.251106"',
'"442141.502863","442141.247462"']
to_list = []
for nest1 in wtf:
nest2 = nest1.split(',')
for each in nest2:
to_list.append(float(each.strip('"')))
to_array = np.asarray(to_list)
Not exactly elegant. You need to deal with each level of nesting in your input data. I'd recommend you reconsider the way you're formatting the data you're inputting.

python list modification to list of lists

I am trying to learn python (just finished Learn Python the Hard Way book!), but I seem to be struggling a bit with lists. Specifically speaking, I have a list like so:
x = ["/2.ext", "/4.ext", "/5.ext", "/1.ext"]
I would like to operate on this above list, so that it returns a list (somehow!) like so:
y = [ ["/1.ext", "/2.ext"], ["/1.ext", "/2.ext", "/3.ext, "/4.ext"], ["/1.ext", "/2.ext", "/3.ext", "/4.ext", "/5.ext"], ["/1.ext"] ]
So, essentially, each element in x is now turned to a list of lists. I could probably loop over x, store all the sequence lists in another list and then merge then together - but it just seems like there must be a better way to do it.
Would be grateful if someone could point me in the right direction to solve this problem.
EDIT (taking into account Martijn's comments):
Specifically, I want to generate the intermediary filenames in a sequence, ending at the number for each x list element
You can do it as follows:
x = ["/2.ext", "/4.ext", "/5.ext", "/1.ext"]
print [['/{}.ext'.format(j) for j in range(1,int(i[1])+1)] for i in x]
[OUTPUT]
[['/1.ext', '/2.ext'], ['/1.ext', '/2.ext', '/3.ext', '/4.ext'], ['/1.ext', '/2.ext', '/3.ext', '/4.ext', '/5.ext'], ['/1.ext']]
This only works for digits upto 9. I'll post update for more general solutions
HERE is the more general solution. Works for any numbers:
import re
x = ["/2.ext", "/4.ext", "/5.ext", "/1.ext"]
print [['/{}.ext'.format(j) for j in range(1,int(re.search(r'\d+',i).group(0))+1)] for i in x]

Python dot-multiply lists on list of lists, without using numpy

I am quite new to python and getting my head around arrays as such, and I am struck on a rather simple problem. I have a list of list, like so:
a = [[1,0,1,0,1],[0,0,1,0,1],[0,0,1,0,1],[1,1,1,0,1],[1,0,0,0,0]]
and I would like to multiply elements of each list with each other. Something like:
a_dot = [1,0,1,0,1]*[0,0,1,0,1]*[0,0,1,0,1]*[1,1,1,0,1]*[1,0,1,0,0]
=[0,0,1,0,0]
Was wondering if I can do the above without using numpy/scipy.
Thanks.
import operator
a_dot = [reduce(operator.mul, col, 1) for col in zip(*a)]
But if all your data is 0s and 1s:
a_dot = [all(col) for col in zip(*a)]
Did you try the reduce function? You call it with a function (see it as your operator) and a list and it applies it the way you described.
You can solve by below code,
def multiply(list_a,list_b):
c = []
for x,y in zip(list_a,list_b):
c.append(x*y)
return c
reduce(lambda list_a,list_b: multiply(list_a,list_b), a)
Happy coding!!!!

2D Arrays in Python?

How would I do a "for every" command for a list, so I want to loop through it for every item in the list!
Ask a short question, get a short answer:
a = []
for x in abc:
a.append(x)
Note that typically when people say "array" they mean the flat data-structure that C has: a block of adjacent cells in memory. In particular, you can't append to an array. Python's list type is a cross between an array and a list in that you can append and pop but also index. (I believe it's a dynamically resizing array.)
The answer to your revised question is:
for elt in mylist:
do_something(elt)
Is this all you were looking for?
Try something like this for 2d array's
http://www.stev.org/post/2012/02/22/Python-2d-Arrays-dont-work.aspx

Categories

Resources