This question already has answers here:
2D array of lists in python
(6 answers)
Closed 6 years ago.
I have a list 2D
a = {}
a[0,0] = 1
a[0,1] = 2
a[1,0] = 3
a[1,1] = 4
a[1,2] = 5
...
a[1,n] = 6
Now, I want to access the element a[1,x] (x = is 0 to n)
How can I do?
You'll need a nested loop if I understand what your asking for.
somearray = [[0,1],[2,3],[4,5]]
for row in range(len(somearray)):
for col in range(len(somearray[row])):
print(somearray[row][col], end =" ")
will output:
0 1 2 3 4 5
Related
This question already has answers here:
Creation of numpy array from two arrays, such that alternate indices contain elements from different arrays [duplicate]
(2 answers)
Pythonic way to combine (interleave, interlace, intertwine) two lists in an alternating fashion?
(26 answers)
Closed 10 days ago.
With Matlab, I can run this easily :
a = [0 0 0 0 0]
b = [1 1 1 1]
a(1:1:end) = b
To obtain : a = [0 1 0 1 0 1 0 1 0]
How can I implement this using Python ?
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 2 years ago.
I am new to python and i was just writing a programm to get an input like :
1 2 3 4 5 6 7 8 9
I wanted to remove the integers which are bigger than 2 and this was my code:
a = input()
b =a.split()
for i in range(len(b)):
b[i] = int(b[i])
for i in b:
if i >=3:
b.remove(i)
print(b)
I expected an output like this :
[1,2]
but when I run it,it shows this :
[1, 2, 4, 6, 8]
Can someone tell me ,where I made mistake?
Check with below code:
#a = '1 2 3 4 5 6 7 8 9'
a = input()
b =a.split()
for i in range(len(b)):
b[i] = int(b[i])
tempB = []
for i in b:
if i < 3:
tempB.append(i)
print(tempB)
More optimized and shortcode:
b =list(map(int,input().split()))
tempB = [i for i in b if i < 3]
print(tempB)
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 4 years ago.
l = [1,2,3,4,5,6,7,8,9,10]
I have a list and I need to print 5 elements per line to get:
1 2 3 4 5
6 7 8 9 10
I tried everything but I'm stuck, help!
#Joaquin, try below code:
http://rextester.com/BLN7722
l = [1,2,3,4,5,6,7,8,9,10]
for index, item in enumerate(l):
if (index + 1) % 5 == 0:
print(item)
else:
print(item, end=" ")
"""
1 2 3 4 5
6 7 8 9 10
"""
This question already has answers here:
String to list in Python
(5 answers)
Closed 5 years ago.
Given:
['1 -1 1 1 1 1 1 1 1']
How can I convert it (efficiently) to be a vector of integers something like:
[1 -1 1 1 1 1 1 1 1]
Thank you.
return [int(n)
for n in s.split()]
This question already has answers here:
Count all values in a matrix less than a value
(6 answers)
Closed 5 years ago.
Data = matrix R
First I wanted to count elements of each row
countR = np.count_nonzero(R, axis=1)
Then, I could get matrix countR.
[25 2 1 2 2 55 1 2 1 2 1 1 2 2 1 1 1 1 2 2 1 2 14 1 3 ..
Second, I want to count elements in matrix
"if element>1 "
So what I did is here
countR1 = pd.value_counts(countR.values, sort>1)
But there was an error.
How can i count elements?
you can do it easily like this:
y=np.array(countR)
len(y[y>1])
if I understand correctly you want to count all the elements that are larger than 1 in the matrix R.
you can filter the data-frame by doing so(to dispose of the elements that are larger than 1):
biggerThanOne = R[R<1]
you can then get the size of the array and get the number of elements:
biggerThanOne.size
if you mean you want to count the elements of countR you can practically do the same thing