Tagged Questions
2
votes
7answers
98 views
How to parse a string and return a nested array?
I want a Python function that takes a string, and returns an array, where each item in the array is either a character, or another array of this kind. Nested arrays are marked in the input string by ...
3
votes
1answer
71 views
Numpy: outer product of n vectors
I'm trying to do something simple in numpy, and I'm sure there should be an easy way of doing it.
Basically, I have a list of n vectors with various lengths. If v1[i] is the i'th entry of the first ...
1
vote
2answers
67 views
How to insert elements into array without using append, Python?
I'm a beginner with Python and am having a bit of trouble inserting elements into an array without using the append() function.
This is part of my code which I hope illustrates enough but feel free ...
4
votes
3answers
59 views
Python split text and place into array
I dont really know how to explain it using english but:
inputText = "John Smith 5"
I want to split it and insert that to nameArray and make 5(string) into an integer.
nameArray = ["John", "Doe", ...
0
votes
2answers
25 views
PHP max function with key as php function
I have a function in php:
function cmp_key($lst){
$itersect_size = count(array_intersect($zset, $lst)); //zset is a list which i have
return $intersect_size,-count($lst)
}
and then this ...
0
votes
4answers
68 views
Dealing Cards in Python? [closed]
I'm making a program that deals cards and assigns 5 random cards to each player, and it works up until I try to print the hands of each player (showHand function). I am trying to print the cards the ...
22
votes
1answer
267 views
Why is numpy.any so slow over large arrays?
I'm looking for the most efficient way to determine whether a large array
contains at least one nonzero value. At first glance np.any seems like the
obvious tool for the job, but it seems unexpectedly ...
3
votes
2answers
36 views
Numpy array get the subset/slice of an array which is not NaN
I have an array of size: (50, 50). Within this array there is a slice of size (20,10).
Only this slice contains data, the remainder is all set to nan.
How do I cut this slice out of my large array?
...
1
vote
1answer
31 views
Applying a function to windows in an array (like a filter)
Suppose I have an image loaded into Python as a Numpy array.
I would like to run a function over say a 5x5 window, like a filter kernel but it's not really a standard convolution. What is the most ...
4
votes
2answers
45 views
Mapping element-wise a NumPy array into an array of more dimensions
I want map a numpy.array from NxM to NxMx3, where a vector of three elements is a function of the original entry:
lambda x: [f1(x), f2(x), f3(x)]
However, things like numpy.vectorize do not allow ...
0
votes
2answers
71 views
For loop in Python?
I am trying to list every card in a deck of cards (along with a number assigned to the card) using this code:
suitName = ("hearts", "diamonds", "spades", "clubs")
rankName = ("Ace", "Two", "Three", ...
0
votes
3answers
51 views
Cannot convert string to int in Python
What's wrong with this python snippet:
for zhszam in pontok.keys():
s = 0
for p in pontok[zhszam]:
if p.isdigit():
s += int(p)
print s
pontok[zhszam] = s
...
-2
votes
1answer
47 views
Python Random number generator from array [closed]
I'm trying to write a random number generator from scratch, but I need it to follow these rules.
it needs randomly select 3 numbers from an array producing every possible outcome of groups of three ...
2
votes
1answer
56 views
Iterating through a numpy array and then indexing a value in another array
I am struggling to get this code to work I want to iterate through an numpy array and based on the result, index to a value in another numpy array and then save that in a new position based on that ...
3
votes
0answers
67 views
Python and Numba for vectorized functions
Good day, I'm writing a Python module for some numeric work. Since there's a lot of stuff going on, I've been spending the last few days optimizing code to improve calculations times.
However, I have ...
0
votes
1answer
29 views
python ctypes array of structs
I have the followng code and it ends up to segmentation fault.
import ctypes
from random import randint
class STRUCT_2(ctypes.Structure):
#_pack_=2
_fields_ = [('field_1', ctypes.c_short),
...
1
vote
1answer
58 views
PyPy and efficient arrays
My project currently uses NumPy, only for memory-efficient arrays (of bool_, uint8, uint16, uint32).
I'd like to get it running on PyPy which doesn't support NumPy. (failed to install it, at any ...
0
votes
1answer
27 views
How do I join results of looping script into a single variable?
I have looping script returning different filtered results, I can make this data return as an array for each of the different filter classes. However I am unsure of the best method to join all of ...
2
votes
2answers
32 views
Numpy chain comparison with two predicates
In Numpy, I can generate a boolean array like this:
>>> arr = np.array([1, 2, 1, 2, 3, 6, 9])
>>> arr > 2
array([False, False, False, False, True, True, True], dtype=bool)
...
0
votes
0answers
20 views
What is the best way to convert a SymPy matrix to a numpy array/matrix
I am not sure if the approach I've been using in sympy to convert a MutableDenseMatrix to a numpy.array or numpy.matrix is a good current practice.
I have a symbolic matrix like:
g = sympy.Matrix( ...
0
votes
2answers
45 views
How do you write several arrays into an excel file?
If I had several arrays I want to write into an excel file using Python, what would be the best way to go about it? I've tried several ways and cant figure it out.... this is an example of one way I ...
1
vote
1answer
44 views
Inversing a twodimensional array in python
I have an array a, which is twodimensional. A contains objects which also contain objects.
I want to make sure that a[1,1] becomes a[n,n], a[2,1] becomes a[n-1,n], a[2,2] becomes a[n-1][n-1] etc.
I ...
0
votes
1answer
57 views
Python multiplication Matrix with Matrix transpose with array
[1]: https://upload.wikimedia.org/math/3/0/a/30aed0153521807d5a314ea76f37e723.png [1].
I want to write the above equation in Python using numpy functions:
b = b - INV(J'*J) * J' * r(b)
J is matrix ...
1
vote
2answers
35 views
replace zeroes in numpy array with the median value
I have a numpy array like this:
foo_array = [38,26,14,55,31,0,15,8,0,0,0,18,40,27,3,19,0,49,29,21,5,38,29,17,16]
I want to replace all the zeros with the median value of the whole array (where ...
3
votes
2answers
55 views
Funky behavior with numpy arrays
Am hoping someone can explain to me the following behavior I observe with a numpy array:
>>> import numpy as np
>>> data_block=np.zeros((26,480,1000))
>>> ...
0
votes
2answers
38 views
python - parse string (which is an array) returned from a web service as array/list
I am using
httplib.HTTPConnection(self._myurl)
conn.request("GET", "/")
data = conn.getresponse().read()
now this URL returns an python type arrays similar to the below:
[1,"apple",23,"good"]
...
2
votes
1answer
33 views
How to calculate numpy arrays on galois field?
I want to use numpy array on galois field (GF4).
so, I set GF4 class to array elements.
It works on array + integer calculation but it dosen't works on array + array calculation.
import numpy
class ...
2
votes
1answer
34 views
Python: change numpy array with NaNs to array with numbers and '--'
I have a numpy array with some floats and some nans:
a = [ 8.08970226 nan nan 8.30043545 nan nan nan nan]
I want to convert it to an array (for printing in Latex) to the mixed form:
a = ...
0
votes
1answer
61 views
Python: Reversing lists [duplicate]
Ok so i've searched multiple threads and came across helpful discussion on reversing lists such as this and this; What i did get from those is to use
S = ("hello")
print(S[::-1])
or
S = ("hello")
...
1
vote
2answers
36 views
Importing Large Tab Delimited .txt file Into Python
I have a tab delimited .txt file that I'm trying to import into a matrix array in python of the same format as the text file is as shown below:
123088 266 248 244 266 ...
1
vote
1answer
38 views
lexsort zeros before negatives?
I have some data that I want to sort, but this method using numpy.lexsort()
data = np.zeros(shape=(n,6))
# some routine that partially populates the table
index = np.lexsort((data[:,0],data[:,1]))
...
0
votes
1answer
56 views
(Pseudo-)random array in Bash used as seed in Python simulations
A Bash script controls a Python script ("main.py") which needs a random seed to run.
Aiming at getting several realizations of the simulation, I would like to have a random random seed for each of ...
1
vote
4answers
36 views
Python, Appending string on each item on the list
I have a code that concatenates a string 'lst' on each item of the list.
i = 0
lim = len(lst)
while i < lim:
lst[i] = 'lst%s' % (lst[i])
i += 1
Is there a faster way ...
0
votes
0answers
21 views
covert the pairwise data to a matrix
I have a similar question to the following poster:
In Python: create a table to visualize pairwise data
but my data missed lots of pairwise information.
so if I use a similar implementation as in ...
2
votes
1answer
55 views
Creating sublist from a give list of items
I would say first that the following question is not for homework purpose even because i've finish software engineer a few months ago. Anyway today I was working and one friend ask to me this strange ...
1
vote
1answer
27 views
Populate predefined numpy array with arrays as columns
Something I can't figure out by reading the Python documentation and stackoverflow. Probably I'm thinking in the wrong direction..
Let's say I've a predefined 2D Numpy array as follow:
a = ...
2
votes
1answer
27 views
Grouping array to nested structure with numpy
Say I've got a numpy array like this (larger and with different number of repetitions per date):
data = np.array([ \
["2011-01-01", 24, 554, 66], \
["2011-01-01", 44, 524, 62], \
...
3
votes
2answers
38 views
NumPy array, change the values that are NOT in a list of indices
I have a numpy array like:
a = np.arange(30)
I know that I can replace the values located at positions indices=[2,3,4] using for instance fancy indexing:
a[indices] = 999
But how to replace the ...
3
votes
1answer
56 views
Merging two arrays under numpy
Using Numpy, I would like to achieve the result below, given b and c. I have looked into stacking functions, but I cannot get it to work. Could someone please help?
import numpy as np
...
-5
votes
1answer
48 views
helping with this quicksort python [closed]
I've written this code but I am stuck into it .
please help to fix it.
thanks.
def QuickSort(A,p,r):
if p<r:
q=Partition(A,p,r)
QuickSort(A,p,q-1)
...
0
votes
1answer
40 views
How do I avoid recursively adding arrays when I need to merge two arrays in a while-loop?
Working with pygame, I am trying to create a larger list out of two smaller lists. This needs to be done during the game loop because those lists contain pygame.Rect objects which determine collision ...
-4
votes
2answers
65 views
Using arrays in an if statement condition
How do I use arrays in the condition part of an if statement? I want the program to check every element's absolute value, and return the appropriate part. This gave some hope: Function of Numpy Array ...
4
votes
2answers
57 views
Pythonic way to print a multidimensional complex numpy array to a string
I have a 3D complex numpy array defined like this:
> import numpy as np
> a = np.random.rand(2,3,4) + np.random.rand(2,3,4) * 1j
> a
array([[[ 0.40506245+0.68587874j, ...
2
votes
5answers
78 views
I have a list of words. I want to add a counter variable associated with each word. How do I do this?
I have a list of words, let's say it's
['a', 'b', 'c', 'd']
I have a document where I've already pre-processed a text file into a matrix, and it goes like this:
a,b,c,d
0,1,1,0
1,1,0,0
1,1,1,1
...
1
vote
3answers
79 views
How to group this python list?
I have the following array:
[1 , 2]
[1 , 3]
[1 , 4]
[2 , 3]
[2 , 4]
[5 , 1]
I want to print an output like the following:
"Items related to 1:"
2, 3, 4, 5 *note this last one was on ...
1
vote
2answers
57 views
Remove elements from the array
I have two arrays, that I am trying to combine using concatenate:
a = np.array(([1,2], [5,6], [9,10]))
b = np.array(([3,4], [7,8], [11,12], [13,14], [17,18]))
c = np.concatenate((a,b), 1)
This wont ...
0
votes
1answer
55 views
Numpy, how to convert a 2D array to 3D (by grouping cols to 2 list)
E.g.
Before converting
array([[0, 0, 0],
[0, 1, 1],
[1, 0, 1],
[1, 1, 0]])
After converting
array([[[0, 0], [0]],
[[0, 1], [1]],
[[1, 0], [1]],
[[1, 1], ...
10
votes
5answers
146 views
Creating list of individual list items multiplied n times
I'm fairly new to Python, and think this should be a fairly common problem, but can't find a solution. I've already looked at this page and found it helpful for one item, but I'm struggling to extend ...
0
votes
3answers
64 views
How to find the index of n largest elements in a list or np.array, Python
Is there a built-in function or a very simple way of finding the index of n largest elements in a list or a numpy array?
K = [1,2,2,4,5,5,6,10]
Find the index of the largest 5 elements?
I count ...
0
votes
2answers
59 views
reading C array in python
Can python take in a C array (from a file) and does some processing on it? For example, say I have a C header containing:
static char bin[] = {...}; // whole bunch of hex values
Can I write a ...