Tagged Questions
0
votes
1answer
29 views
Python numpy array strange return
I am relatively new to python. I have a numpy array that has 3 dimensions. I know we can display only few elements using :.
It seems to work just fine while I'm doing it starting from a small value, ...
0
votes
1answer
26 views
Python multiple search in arrays
idtopick is an array of ids
idtopick=array([50,48,12,125,3458,155,299,6,7,84,58,63,0,8,-1])
idtolook is another array containing the ids I'm interested in
idtolook=array([0,8,12,50])
...
1
vote
2answers
40 views
Python: Acessing index numbers within an if statement nested within a for loop
I have an array of integers derived from a gradient of a line. The array is called sign_slope and looks like this:
sign_slope = array([-1, 1, -1, ..., -1, -1, -1])
I am searching for the cases ...
1
vote
2answers
53 views
Read values from txt with rows of different sizes into a single numpy array
I need to read a file that has a structure like:
1 2 3 4 5
6 7 8 9 10
11 22
13 14 15 16 17
18 19 20 21 22
23 24
I need to read this file in a single array = [ 1,2,3, ... , 23, 24]
How to do ...
3
votes
2answers
82 views
In Python: Given an original array of numbers, how would I create a new array that contains the values from the original array within a certain range?
import os
import pyfits as ps
import lomb
import numpy as np
import matplotlib.pyplot as plt
hdulist = ps.open('filename')
tbdata = hdulist[1].data
PDCFlux = tbdata.field(7)
PDCFlux = ...
2
votes
2answers
68 views
Interleave rows of two numpy arrays in Python
I wanted to interleave the rows of two numpy arrays of the same size.
I came up with this solution.
# A and B are same-shaped arrays
A = numpy.ones((4,3))
B = numpy.zeros_like(A)
C = ...
0
votes
2answers
80 views
Python adding records to an array
I have an array defined as -
import numpy as np
A = np.recarray((3,),dtype=[('x',float), ('y', float), ('z',float)])
Plus another array B which is read from a CSV file as -
>>> print B
...
1
vote
1answer
58 views
Python declare empty record array
I want to declare an empty record array like this:
kneePointsOnAxis = np.recarray((3,), dtype=[(int, int, int)])
However, it gives me and error:
TypeError: data type not understood
At the end I ...
1
vote
4answers
173 views
Can this algorithm be improved without recursion
The question is to print all possible interleavings of two given strings. So I wrote a working code in Python which runs like this:
def inter(arr1,arr2,p1,p2,arr):
thisarr = copy(arr)
if p1 ...
2
votes
1answer
57 views
Accessing C struct array to Python with SWIG
I attempting to call into existing C code from Python. The C code defines a struct B that contains an struct array of As. The C code also defines a function that puts values into the structure when ...
1
vote
4answers
72 views
Python - getting values from 2d arrays
I have a 2d array:
[[], ['shotgun', 'weapon'], ['pistol', 'weapon'], ['cheesecake', 'food'], []]
How do I call a value from it? For example I want to print (name + " " + type) and get
shotgun ...
2
votes
2answers
36 views
Slicing python matrix into quadrants
Suppose I have the following matrix in python:
[[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]]
I want to slice it into the following matrices (or quadrants/corners):
[[1,2], [5,6]]
[[3,4], ...
1
vote
2answers
59 views
How can I add different elements to a list in a list? Python 3
f = open("sonad.txt",encoding="utf-8")
c = f.readlines()
blokk = [[]] * 15
for read in c:
length = len(read.strip())
blokk[length].append(read.strip())
sonad.txt has just some random words ...
0
votes
3answers
52 views
how can i make a 2d array out of a text file? [closed]
Possible Duplicate:
How can i make a 2D array with existing lists?
I have a text file 'mapview.txt' and its content is:
1234
5678
9012
i want the result to be:
...
0
votes
2answers
40 views
How can i make a 2D array with existing lists?
for instance, i have a txt data called 'mazeline' like this:
abcd
cdae
korp
So i first made 3 lists:
mazeline = readmaze.split()
mline0 = list(mazeline[0])
mline1 = list(mazeline[1])
mline2 = ...
1
vote
1answer
63 views
Changing a 3D array to a 2D array that includes a list in Python/Pulp
I am using Solverstudio (with the Pulp solver) and i am trying to get a 2D output (onto a spreadsheet) from a 3D variable that has been found, for certain values of that variable.
Ive tried:
for ...
1
vote
1answer
67 views
Matplotlib: Two datasets on one image
I am trying to plot two datasets on a single figure. Essentially this is for tracking fluids in a 2 dimensional porespace.
Both data sets are 250x250 numpy array, the first with data ranging from 0 ...
0
votes
1answer
31 views
Python: mask a numpy.ndarray with another numpy.ndarray with elegant solution
I have two numpy.ndarray and i found a not elegant solution (using more than 4 lines code) to mask the data2 with data1. I am asking an elegant solution, saving line to do:
example.
data1 = ...
3
votes
2answers
47 views
formatted string of series of numpy array elements
It seems quit trivial to me but I'm still missing an efficient and "clean" way to insert a series of element belonging to numpy array (as aa[:,:]) in a formatted string to be printed/written.
In fact ...
2
votes
2answers
64 views
What is the equivalent of “zip()” in Python's numpy?
I am trying to do the following but with numpy arrays:
x = [(0.1, 1.), (0.1, 2.), (0.1, 3.), (0.1, 4.), (0.1, 5.)]
normal_result = zip(*x)
This should give a result of:
normal_result = [(0.1, 0.1, ...
3
votes
3answers
73 views
Unpacking arguments: only named arguments may follow *expression
The following works beautifully in Python:
def f(x,y,z): return [x,y,z]
a=[1,2]
f(3,*a)
The elements of a get unpacked as if you had called it like f(3,1,2) and it returns [3,1,2]. Wonderful!
...
0
votes
0answers
41 views
Using mutable sequence types as default attribute values together with @property in Python
I get stuck on the following. I am trying to initialize some class attributes with default values, where the default values are mutable sequence types (list and numpy array). Because I want to have ...
0
votes
2answers
35 views
Django - List index out of range
I have a ManyToManyField in my model. I need to get the third item for every query as below.
class Staff(models.Model):
status = models.BooleanField(default=True)
person = ...
1
vote
2answers
33 views
Finding range of a numpy array elements
This is a very simple question:
I have a numpy array of 94 x 155:
a = [1 2 20 68 210 290..
2 33 34 55 230 340..
.. .. ... .. ... .....]
I want to calculate the range of each ...
1
vote
4answers
102 views
Python: method to remove all duplicate points from a X,Y,Z file that have identical x and y coordinates
I am looking a method to removes all duplicate points from a a X,Y,Z file. What i wish to code is remove points that have identical x and y coordinates. The first point survives, all subsequent ...
3
votes
2answers
103 views
Numpy slicing x,y,z array for variable z
I have a 3d array of position data, which I'd like to take 2-d slices from. However the slices vary in the z depth with x (and y eventually).
E.g.
An array 100x100x100, and I want the first slice to ...
0
votes
4answers
53 views
Python Script to find file name and size of all files in a directory and save them to a text file for excel import
I am pretty new to python and I am trying to:
Within a user defined directory (1000 images)
Find the file name (Finished)
Find the file size (Finished)
Store to an array
Store array to text file
...
0
votes
5answers
58 views
python array values changing mysteriously
I am thoroughly perplexed over the following behavior:
gap_in_y[i][j] = max((scoreMatrix[i-1][j] - dy), (gap_in_y[i-1][j] - ey))
if i == 3 and j == 1:
print gap_in_y
...
0
votes
1answer
27 views
convert sparse format to array numpy
Suppose you have a generator that returns the rows of a table. Something you could use like this:
for (labels, value) in rows:
pass
"labels" is length n and say it's all strings for simplicity. ...
-1
votes
1answer
34 views
Arrays of strings into numpy.amax
In the Python's standard max function (I also can pass in a key parameter):
s = numpy.array(['one','two','three'])
max(s) # 'two' (lexicographically last)
max(s, key=len) # 'three' (longest string)
...
1
vote
1answer
28 views
how can I make a numpy function that accepts a numpy array, an iterable, or a scalar?
Suppose I have this:
def incrementElements(x):
return x+1
but I want to modify it so that it can take either a numpy array, an iterable, or a scalar, and promote the argument to a numpy array ...
0
votes
4answers
92 views
Python array manipulation
I have array [1,2,1,2,3,4,3,4,1,2]
I want to loop it x times, each times moving array every element 1 position forward:
So next loop will be:
2.[2,1,2,3,4,3,4,1,2,1]
3.
[1,2,3,4,3,4,1,2,1,2]
...
0
votes
4answers
64 views
Merging two dictionaries with nested arrays
I have 2 dictionaries
a = {'I': [1,2], 'II': [1,2], 'III': [1,2]}
b = {'I': [3,4], 'II': [3,4], 'IV': [3,4]}
how can i merge them such that i get the following result
c = merge_dicts(a,b)
where ...
0
votes
4answers
57 views
Removing last character from each element in array (Python)
I have an array find_words which is
[u'Duration$', u'Noun$', u'Adjective$']
I would like to remove all the '$' so it looks like
[u'Duration', u'Noun', u'Adjective']
How do I go about this? ...
0
votes
4answers
67 views
checking if the elements of an array are present in another array in python
I have two arrays
a = array([1,2,3])
b = array([2,7])
Now I want to check if elements of a are in b
and the returning answer should be (False, True, False). Is there some simple way to do this ...
4
votes
2answers
86 views
Counting of adjacent cells in a numpy array
Past midnight and maybe someone has an idea how to tackle a problem of mine. I want to count the number of adjacent cells (which means the number of array fields with other values eg. zeroes in the ...
6
votes
5answers
193 views
dealing with arrays: how to avoid a “for” statement
I have a 100000000x2 array named "a", with an index in the first column and a related value in the second column. I need to get the median values of the numbers in the second column for each index. ...
5
votes
3answers
100 views
Python - Slicing numpy array with another array
I've got a large one-dimensional array of integers I need to take slices off. That's trivial, I'd just do a[start:end]. The problem is that I need more of these slices. a[start:end] does not work if ...
2
votes
1answer
60 views
‘TypeError: ufunc …’ when using += on numpy arrays
If I run the following code:
import numpy as np
b = np.zeros(1)
c = np.zeros(1)
c = c/2**63
print b, c
b += c
I get this error message:
TypeError: ufunc 'add' output (typecode 'O') could not be ...
1
vote
2answers
42 views
How do you display a 2D numpy array in glade-3 ?
I'm making live video GUI using Python and Glade-3, but I'm finding it hard to convert the Numpy array that I have into something that can be displayed in Glade. The images are in black and white with ...
1
vote
1answer
44 views
Interpolation in PyLab
I have two arrays x, y with coordinates of a function.
Example:
x=[0,1,2,3,4,5]
y=[0,1,5,20,30,32]
pylab.plot(x,y) shows me a smooth function for this.
Is there a way to get the x-Value for y=3?
...
1
vote
4answers
84 views
Squaring all elements in a list
I am told to
Write a function, square(a), that takes an array, a, of numbers and returns an array containing each of the values of a squared.
At first, I had
def square(a):
for i in a: print ...
0
votes
5answers
54 views
Filter from list - Python
I was wondering if someone can help me with a homework problem.
Write a function, func(a,x), that takes an array, a, x being both numbers, and returns an array containing only the values of a that ...
0
votes
1answer
37 views
python - please help cant figure out how to add up user input of an array
how do I Write using python a function, sum(a), that takes an array, a, of numbers and returns their sum?
I tried this but i am unable to figure out how get the user input of the array of number this ...
1
vote
3answers
99 views
What does '[0]' mean in Python?
I'm familiar with programming but new to python:
mem = [0] * memloadsize
what does the '[0]' represent?
Is it a built-in array?
3
votes
7answers
109 views
Numpy Indexing: Return the rest
A simply example of numpy indexing:
In: a = numpy.arange(10)
In: sel_id = numpy.arange(5)
In: a[sel_id]
Out: array([0,1,2,3,4])
How do I return the rest of the array that are not indexed by sel_id? ...
2
votes
3answers
90 views
Python-numpy test for ndarray using ndim
I'm working on a project in Python requiring a lot of numerical array calculations. Unfortunately (or fortunately, depending on your POV), I'm very new to Python, but have been doing MATLAB and ...
1
vote
3answers
62 views
Passing subset reference of array/list as an argument in Python
I'm kind of new (1 day) to Python so maybe my question is stupid. I've already looked here but I can't find my answer.
I need to modify the content of an array at a random offset with a random size.
...
1
vote
4answers
50 views
Merging ND array into single array and list
What I have is:
x = array([a, a, a, a, a, a],
[b, b, b, b, b, b],
[................],
[z,z,z,z,z,z,z,z,])
I am looking for a way about:
i. How to covert this array ...
-3
votes
0answers
79 views
C and python print in hex sin() [closed]
I have written a python code: sin-cos.py
import math
#main()
if __name__ == "__main__":
for i in range(0, 64):
print i, hex(int(math.floor(abs(math.sin(i+1) * 4294967296)))) , ...