same way you did the fill in, but reverse the indexes:
>>> for j in range(columns):
... for i in range(rows):
... print mylist[i][j],
...
0,0 1,0 2,0 0,1 1,1 2,1
>>>
Answer from Iliyan Bobev on Stack Overflowsame way you did the fill in, but reverse the indexes:
>>> for j in range(columns):
... for i in range(rows):
... print mylist[i][j],
...
0,0 1,0 2,0 0,1 1,1 2,1
>>>
This is the correct way.
>>> x = [ ['0,0', '0,1'], ['1,0', '1,1'], ['2,0', '2,1'] ]
>>> for i in range(len(x)):
for j in range(len(x[i])):
print(x[i][j])
0,0
0,1
1,0
1,1
2,0
2,1
>>>
Iterating through a two dimensional array in Python? - Stack Overflow
Nested for Loops in Python- creating a two dimensional array/list of lists - Stack Overflow
create a dynamic two dimensional array in python (loop) - Stack Overflow
python 3.x - Two-dimensional arrays and for-loops - Stack Overflow
Videos
You need to tell us something about this: dataset = datas.values
It's probably a 2d array, since it derives from a load of a csv. But what shape and dtype? Maybe even a sample of the array.
Is that the data argument in the function?
What are blackKings and values? You treat them like lists (with append).
for i in data:
if data[i][39] == 1:
This doesn't make sense. for i in data, if data is 2d, i is the the first row, then the second row, etc. If you want i to in an index, you use something like
for i in range(data.shape[0]):
2d array indexing is normally done with data[i,39].
But in your case data[i][39] is probably an array.
Anytime you use an array in a if statement, you'll get this ValueError, because there are multiple values.
If i were proper indexes, then data[i,39] would be a single value.
To illustrate:
In [41]: data=np.random.randint(0,4,(4,4))
In [42]: data
Out[42]:
array([[0, 3, 3, 2],
[2, 1, 0, 2],
[3, 2, 3, 1],
[1, 3, 3, 3]])
In [43]: for i in data:
...: print('i',i)
...: print('data[i]',data[i].shape)
...:
i [0 3 3 2] # 1st row
data[i] (4, 4)
i [2 1 0 2] # a 4d array
data[i] (4, 4)
...
Here i is a 4 element array; using that to index data[i] actually produces a 4 dimensional array; it isn't selecting one value, but rather many values.
Instead you need to iterate in one of these ways:
In [46]: for row in data:
...: if row[3]==1:
...: print(row)
[3 2 3 1]
In [47]: for i in range(data.shape[0]):
...: if data[i,3]==1:
...: print(data[i])
[3 2 3 1]
To debug a problem like this you need to look at intermediate values, and especially their shapes. Don't just assume. Check!
I'm going to attempt to rewrite your function
def model_building(data):
global blackKings
blackKings.append(data[0, 1])
# Your nested if statements were performing an xor
# This is vectorized version of the same thing
values = np.logical_xor(*(data.T[[39, 40]] == 1)) * -2 + 1
# not sure where `values` is defined. If you really wanted to
# append to it, you can do
# values = np.append(values, np.logical_xor(*(data.T[[39, 40]] == 1)) * -2 + 1)
# Your blackKings / flag logic can be reduced
mask = (blackKings[:, None] != data[:, 1]).all(1)
blackKings = np.append(blackKings, data[:, 1][mask])
This may not be perfect because it is difficult to parse your logic considering you are missing some pieces. But hopefully you can adopt some of what I've included here and improve your code.
How can iterate through only the outermost objects of a 2d array? If I had a 5x5 array of integers all set to 0, how can iterate through and change the outermost objects to a different value using for loops?
So if I had an array that looked like:
00000 00000 00000 00000 00000
How can I change it to look like:
11111 10001 10001 10001 11111
I'm looking for a more conceptual answer but an example in a language like java or python would also be helpful.
Basically, it's nested for loop. You can use list comprehension to do that.
matches = [(A.index(a),B.index(b)) for a in A for b in B if len(set(a).intersection(set(b)))]
Since your question is pseudo-code, I'll respond the same way. ;-)
for all rows in list1:
for all rows in list2:
%comparison of rows..
matches = set(current row of list1) & set(current row of list2)
Basically, this SO question/answer should cover your problem. Possibly, there is a more efficient way to the solution. I'm a beginner with python myself.
List items are iterable, no need to get length as they will assign themselves to the first variable with for..in.. loops;
for item in numbers:
print "In first list: ", item
for num in item:
print " Getting number: ", num
outputs
In first list: [0, 1, 2, 3, 4]
Getting number: 0
Getting number: 1
Getting number: 2
Getting number: 3
Getting number: 4
In first list: [0, 1, 2, 3, 4]
Getting number: 0
Getting number: 1
...
for i in range(5):
for j in range(5):
print numbers[i][j]
Very early into my newest hobby, learning to code so forgive my poor etiquette
Trying to figure out when iterating through a 2d list with a for loop
This structure doesn't preserve the 2d list
new_list = []
for temp_var1 in two_d_lst:
for temp_var2 in temp_var1:
new_list.append(temp_var2.method())But this structure does
new_list = []
for temp_var1 in two_d_lst:
unpacking_lst = []
for temp_var2 in temp_var1:
unpacking_lst.append(temp_var2.method())
new_list.append(unpacking_lst)I'm not sure I understand why...
Greatly appreciate sarcastic remarks, especially if it helps me understand this.