I have arrays with different length and I want to save them inside 1D array using python,
a new array is generated after some tests this is why I have different sizes of arrays,
here is a smple of what I have:
array1=[1,3,5]
array2=[10,12,13,14]
array3=[12,14,14,15,15] #etc
The desired result:
myArray=[[1,3,5],[10,12,13,14],[12,14,14,15,15]]
I tried to use this code
myArray=[]
myArray.append(array1)
myArray.append(array2) #etc
when I print myArray I get:
[[array([1,3,5])], [array([10,12,13,14])], [array([12,14,14,15,15])]]
so when I try to get the second array, for example, I have to use this code
temp = myArray[1]
result = temp[0]
this was working for me but it looks like it has a limitation and it stopped working after a while when I'm retrieving results using some loops.
The currently accepted answer makes little sense, so here's what's actually going on: array_1, array_2, etc. are not plain Python lists, they're almost certainly NumPy arrays. my_array, however, is just a Python list.
Here is a simple program which should allow you to reproduce and understand the difference, at least in how it relates to your program:
import numpy as np
plain_list = [1, 2, 3]
numpy_array = np.array([1, 2, 3])
result_list = [plain_list, numpy_array]
print(plain_list) # [1, 2, 3]
print(numpy_array) # [1 2 3]
print(result_list) # [[1, 2, 3], array([1, 2, 3])]
Now, it isn't exactly clear what's happening to your program, since you just write this was working for me but it looks like it has a limitation and it stopped working after a while when I'm retrieving results using some loops.
Depending on what the rest of the program is doing, numpy arrays may or may not be the appropriate data structure. In any case, please share the entirety of your code as well as an explanation of the program.
First thing first there is no array data structure in python.
Instead List and tuples are used.
In your case variable array1, array2 & array3 are lists.
array1=[1,3,5]
array2=[10,12,13,14]
array3=[12,14,14,15,15]
# to get the desired result as myArray=[[1,3,5],[10,12,13,14],[12,14,14,15,15]]
myArray = [array1, array2, array3]
Check python documentation to know more about lists
Related
I would like to create a 2d array of integers using array module. I know that, I can easily create a 2d array using list but I would like to explore array module since they are compact.
from array import array
a = array('i', [1, 2, 3, 4]) # working fine
a = array('i', [[1, 2], [3, 4]]) # throws error
From: https://docs.python.org/3/library/array.html
"This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained."
Basically, it seems that you cannot store lists inside.
I would recommend other modules like numpy for 2+dimensional matrices.
We can create multi-dimensional arrays in python by using nested list, such as:
A = [[1,2,3],
[2,1,3]]
etc.
In this case, it is simple nRows= len(A) and nCols=len(A[0]). However, when I have more than three dimensions it would become complicated.
A = [[[1,1,[1,2,3,4]],2,[3,[2,[3,4]]]],
[2,1,3]]
etc.
These lists are legal in Python. And the number of dimensions is not a priori.
In this case, how to determine the number of dimensions and the number of elements in each dimension.
I'm looking for an algorithm and if possible implementation. I believe it has something similar to DFS. Any suggestions?
P.S.: I'm not looking for any existing packages, though I would like to know about them.
I believe to have solve the problem my self.
It is just a simple DFS.
For the example given above: A = [[[1,1,[1,2,3,4]],2,[3,[2,[3,4]]]],
[2,1,3]]
the answer is as follows:
[[3, 2, 2, 2, 3, 4], [3]]
The total number of dimensions is the 7.
I guess I was overthinking... thanks anyway...!
I have a code that generates a 1-D numpy array in each iteration. I want the arrays to get appended to the end of a CSV file so that I can read all data from Excel. I am currently trying the following method:
for loop in range(0,10):
# The following part generates the array
Array1 = numpy.array([4.3])
Array2 = numpy.array([10.2])
Array3 = numpy.concatenate((Array1,Array2),axis=0)
# The following part tries to generate a CSV writable array. But it fails :S
if (loop == 0):
ArrayMain = Array3
else:
# Trying to append the new array with the previous array
ArrayMain = numpy.asarray(ArrayMain,Array3)
# Trying to write the array into a .txt. file in .csv format
numpy.savetxt("ArrayMain.csv", ArrayMain, delimiter=",",fmt='%.3f')
This code is giving errors. Any idea how I can rectify it?
I think you need to use append function to append new array with previous array, asarray function converts input to array.
else:
# Trying to append the new array with the previous array
ArrayMain = numpy.append(ArrayMain,Array3)
You should really keep in mind that ndarrays are not really designed to be appended like that.
If you write your CSV file at once outside of the loop, you should consider using a list of arrays instead.
base = []
for i in range(10):
base.append(...)
np.savetxt("ArrayMain.csv", base, ...)
That'll work provided the arrays you append to base have all the same size (np.savext will transform the list of arrays into an array itself).
Alternatively, you could open the file you want to write first, then use its handle in np.savetxt to update it at each iteration. In that case, don't forget to close it at the end...
Mostafar is right. numpy.asarray does not append lists
numpy.asarray([1,2]) -> array([1,2])
i.e. what it actually does is that it converts a list to an array.
You can use the append function as he has defined. Or my personal favourite
numpy.r_
Usage:
numpy.r_[array([1,2]), array([3,4])] -> array([1,2,3,4])
also:
numpy.r_[array([1,2]), 3, [4,5], 6, array([7,8])] -> array([1, 2, 3, 4, 5, 6, 7, 8])
as you can see it can be used to concatenate various lists, arrays and single elements at one go.
I would like to read a 2d array of integers from stdin (or from a file) in Python.
Non-working code:
from StringIO import StringIO
from array import array
# fake stdin
stdin = StringIO("""1 2
3 4
5 6""")
a = array('i')
a.fromstring(stdin.read())
This gives me an error: a.fromstring(stdin.read())
ValueError: string length not a multiple of item size
Several approaches to accomplish this are available. Below are a few of the possibilities.
Using an array
From a list
Replace the last line of code in the question with the following.
a.fromlist([int(val) for val in stdin.read().split()])
Now:
>>> a
array('i', [1, 2, 3, 4, 5, 6])
Con: does not preserve 2d structure (see comments).
From a generator
Note: this option is incorporated from comments by eryksun.
A more efficient way to do this is to use a generator instead of the list. Replace the last two lines of the code in the question with:
a = array('i', (int(val) for row in stdin for val in row.split()))
This produces the same result as the option above, but avoids creating the intermediate list.
Using a NumPy array
If you want the preserve the 2d structure, you could use a NumPy array. Here's the whole example:
from StringIO import StringIO
import numpy as np
# fake stdin
stdin = StringIO("""1 2
3 4
5 6""")
a = np.loadtxt(stdin, dtype=np.int)
Now:
>>> a
array([[1, 2],
[3, 4],
[5, 6]])
Using standard lists
It is not clear from the question if a Python list is acceptable. If it is, one way to accomplish the goal is replace the last two lines of the code in the question with the following.
a = [map(int, row.split()) for row in stdin]
After running this, we have:
>>> a
[[1, 2], [3, 4], [5, 6]]
I've never used array.array, so I had to do some digging around.
The answer is in the error message -
ValueError: string length not a multiple of item size
How do you determine the item size? Well it depends on the type you initialized it with. In your case you initialized it with i which is a signed int. Now, how big is an int? Ask your python interpreter..
>>> a.itemsize
4
The value above provides insight into the problem. Your string is only 11 bytes wide. 11 isn't a multiple of 4. But increasing the length of the string will not give you an array of {1,2,3,4,5,6}... I'm not sure what it would give you. Why the uncertainty? Well, read the docstring below... (It's late, so I highlighted the important part, in case you're getting sleepy, like me!)
array.fromfile(f, n)
Read n items (as machine values) from the file object f and append them to the end of the array. If less than n items are available, EOFError is raised, but the items that were available are still inserted into the array. f must be a real built-in file object; something else with a read() method won’t do.
array.fromstring reads data in the same manner as array.fromfile. Notice the bold above. "as machine values" means "reads as binary". So, to do what you want to do, you need to use the struct module. Check out the code below.
import struct
a = array.array('i')
binary_string = struct.pack('iiii', 1, 2, 3, 4)
a.fromstring(binary_string)
The code snippet above loads the array with tlhe values 1, 2, 3, 4; like we expect.
Hope it helps.
arr = []
arr = raw_input()
If you want to split the input by spaces:
arr = []
arr = raw_input().split()
What's the best way to create 2D arrays in Python?
What I want is want is to store values like this:
X , Y , Z
so that I access data like X[2],Y[2],Z[2] or X[n],Y[n],Z[n] where n is variable.
I don't know in the beginning how big n would be so I would like to append values at the end.
>>> a = []
>>> for i in xrange(3):
... a.append([])
... for j in xrange(3):
... a[i].append(i+j)
...
>>> a
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
>>>
Depending what you're doing, you may not really have a 2-D array.
80% of the time you have simple list of "row-like objects", which might be proper sequences.
myArray = [ ('pi',3.14159,'r',2), ('e',2.71828,'theta',.5) ]
myArray[0][1] == 3.14159
myArray[1][1] == 2.71828
More often, they're instances of a class or a dictionary or a set or something more interesting that you didn't have in your previous languages.
myArray = [ {'pi':3.1415925,'r':2}, {'e':2.71828,'theta':.5} ]
20% of the time you have a dictionary, keyed by a pair
myArray = { (2009,'aug'):(some,tuple,of,values), (2009,'sep'):(some,other,tuple) }
Rarely, will you actually need a matrix.
You have a large, large number of collection classes in Python. Odds are good that you have something more interesting than a matrix.
In Python one would usually use lists for this purpose. Lists can be nested arbitrarily, thus allowing the creation of a 2D array. Not every sublist needs to be the same size, so that solves your other problem. Have a look at the examples I linked to.
If you want to do some serious work with arrays then you should use the numpy library. This will allow you for example to do vector addition and matrix multiplication, and for large arrays it is much faster than Python lists.
However, numpy requires that the size is predefined. Of course you can also store numpy arrays in a list, like:
import numpy as np
vec_list = [np.zeros((3,)) for _ in range(10)]
vec_list.append(np.array([1,2,3]))
vec_sum = vec_list[0] + vec_list[1] # possible because we use numpy
print vec_list[10][2] # prints 3
But since your numpy arrays are pretty small I guess there is some overhead compared to using a tuple. It all depends on your priorities.
See also this other question, which is pretty similar (apart from the variable size).
I would suggest that you use a dictionary like so:
arr = {}
arr[1] = (1, 2, 4)
arr[18] = (3, 4, 5)
print(arr[1])
>>> (1, 2, 4)
If you're not sure an entry is defined in the dictionary, you'll need a validation mechanism when calling "arr[x]", e.g. try-except.
If you are concerned about memory footprint, the Python standard library contains the array module; these arrays contain elements of the same type.
Please consider the follwing codes:
from numpy import zeros
scores = zeros((len(chain1),len(chain2)), float)
x=list()
def enter(n):
y=list()
for i in range(0,n):
y.append(int(input("Enter ")))
return y
for i in range(0,2):
x.insert(i,enter(2))
print (x)
here i made function to create 1-D array and inserted into another array as a array member. multiple 1-d array inside a an array, as the value of n and i changes u create multi dimensional arrays