Related
This code generates a pascal triangle:
import pprint
def nextRow(cRow):
cRow.append(0)
return [cRow[m - 1] + cRow[m + 1] for m in range(len(cRow) - 1)]
def Pascal(n):
row = [0, 0, 0, 0, 1, 0, 0, 0, 0]
l = []
for h in range(n):
l.append(row)
row = nextRow(row)
return l
pprint.pprint(Pascal(5))
I am trying to remove the extra zeros without just removing them in the end of the code:
Output:
[[0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 2, 0, 1, 0, 0, 0],
[0, 1, 0, 3, 0, 3, 0, 1, 0, 0],
[1, 0, 4, 0, 6, 0, 4, 0, 1, 0]]
Desired Output:
[[0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0],
[0, 0, 1, 0, 2, 0, 1, 0, 0],
[0, 1, 0, 3, 0, 3, 0, 1, 0],
[1, 0, 4, 0, 6, 0, 4, 0, 1]]
You can save in l the row calculated without the last element with l.append(row[:-1]) instead of l.append(row) in the Pascal function.
import pprint
def nextRow(cRow):
cRow.append(0)
return [cRow[m - 1] + cRow[m + 1] for m in range(len(cRow) - 1)]
def Pascal(n):
row = [0, 0, 0, 0, 1, 0, 0, 0, 0]
l = []
for h in range(n):
l.append(row[:-1])
row = nextRow(row)
return l
pprint.pprint(Pascal(5))
I'm trying to find an elegant way to find the max value in a two-dimensional array.
for example for this array:
[0, 0, 1, 0, 0, 1] [0, 1, 0, 2, 0, 0][0, 0, 2, 0, 0, 1][0, 1, 0, 3, 0, 0][0, 0, 0, 0, 4, 0]
I would like to extract the value '4'.
I thought of doing a max within max but I'm struggling in executing it.
Another way to solve this problem is by using function numpy.amax()
>>> import numpy as np
>>> arr = [0, 0, 1, 0, 0, 1] , [0, 1, 0, 2, 0, 0] , [0, 0, 2, 0, 0, 1] , [0, 1, 0, 3, 0, 0] , [0, 0, 0, 0, 4, 0]
>>> np.amax(arr)
Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers)) # max numbers from each sublist
[1, 2, 2, 3, 4]
>>> max(map(max, numbers)) # max of those max-numbers
4
Not quite as short as falsetru's answer but this is probably what you had in mind:
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
>>> max(max(x) for x in numbers)
4
How about this?
import numpy as np
numbers = np.array([[0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]])
print(numbers.max())
4
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
You may add key parameter to max as below to find Max value in a 2-D Array/List
>>> max(max(numbers, key=max))
4
One very easy solution to get both the index of your maximum and your maximum could be :
numbers = np.array([[0,0,1,0,0,1],[0,1,0,2,0,0],[0,0,2,0,0,1],[0,1,0,3,0,0],[0,0,0,0,4,0]])
ind = np.argwhere(numbers == numbers.max()) # In this case you can also get the index of your max
numbers[ind[0,0],ind[0,1]]
This approach is not as intuitive as others but here goes,
numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
maximum = -9999
for i in numbers:
maximum = max(maximum,max(i))
return maximum"
I am attempting to write the corresponding list comprehension for the following code snippet.
# Initialize data.
queryRelDict = {'1': [1, 2, 3],
'2': [4, 5, 6],
'3': [11, 13, 14]}
related_docs_indices = [1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14]
relOrNot = [0] * k
for item in queryRelDict.keys():
for i in range(len(related_docs_indices)):
if related_docs_indices[i] + 1 in queryRelDict[item]:
relOrNot[i] = 1
Basically I have a dictionary, where each key has a list as its value. Now my list relOrNot[i] needs to be 1, if ith element of related_docs_indices is in either of the lists in the dictionary.
The desired Output is:
[1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
I tried the following two variations, but is not able to get the desired output.
relOrNot2 = [1 for item in queryRelDict.keys() for i in range(len(related_docs_indices)) if related_docs_indices[i] + 1 in queryRelDict[item]]
but the output is
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
I also tried
relOrNot2 = [1 if related_docs_indices[i] + 1 in queryRelDict[item] else 0 for item in queryRelDict.keys() for i in range(len(related_docs_indices))]
Corresponding Output:
[0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
What modification is necessary to get the desired output?
Here if you want an one-liner :)
relOrNot = [1 if v in set().union(*queryRelDict.values()) else 0 for v in related_docs_indices]
If your desired output is a list relOrNot, where relOrNot[i] is 1, if ith element of related_docs_indices is in either of the lists in the dictionary queryRelDict (then it must have the same length as related_docs_indices), then you can do the following:
# first create one flat list with all elements of the sublists in the dictionary
flatlist = [i for sublist in queryRelDict.itervalues() for i in sublist]
relOrNot = [1 if i in flatlist else 0 for i in related_docs_indices]
# [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
For each key, you are iterating through the related_doc_indices and checking if there is a matching value within the set of values for that key. For key '1', it would look like this:
key 1 values = [1, 2, 3]
related_docs_indices = [
1, # 1 (match)
2, # 1 (match)
3, # 1 (match)
4, # 0 (no match)
5, # 0 (no match)
6, # 0 (no match)
7, # 0 (no match)
8, # 0 (no match)
12, # 0 (no match)
13, # 0 (no match)
14] # 0 (no match)
The desired output for this key should thus be:
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
One issue that you have is that keys are unordered in dictionaries, so that results of the longer list can vary depending on the random order of the keys. For example:
>>> queryRelDict.keys()
['1', '3', '2']
Let's say you first sort the keys, then I believe the desired output should look like this:
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, # key '1'
0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, # key '2'
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] # key '3'
keys = queryRelDict.keys()
keys.sort()
>>> [1 if i in queryRelDict.get(item) else 0
for item in keys for i in related_docs_indices]
#[1, 2, 3, 4, 5, 6, 7, 8, 12, 13, 14] related_doc_indices
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, # key '1' values: [1, 2, 3]
0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, # key '2' values: [4, 5, 6]
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] # key '3' values: [11, 13, 14] (note 11 is not in related_doc_indices)
Create a set with all keys and all values and in your loop just look if the required value is in the set.
s = set()
for (k,v) in queryRelDict.items():
s.add(int(k))# because your keys are string
s = s | set(v)
map(lambda x:1 if x in s else 0, related_docs_indices)
=>[1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1]
I am making a Draughts game in python, I made an array 10 by 10 and I need to append values within the entire row so that is eventually looks like this;
(
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
[2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
[2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
)
Here is my attempt at it so far, I know it's incorrect;
__author__ = 'Matt'
import array
Board_Array = array(10, 10)
pieces = ['Empty', 'White_Piece', 'Black_Piece', 'Upgraded_White_Piece', 'Upgraded_Black_Piece']
list(enumerate(pieces))
if Board_Array.array_equals == [1, 0]:
for i in range(10):
if (i%2) == 0:
array.pop([i])
array.insert(i,1)
You could use a nested list comprehension:
In [173]: [[((i+j) % 2)*k for i in range(10)] for k in (1,1,0,2,2)
for j in (0,1)]
Out[173]:
[[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
[2, 0, 2, 0, 2, 0, 2, 0, 2, 0],
[0, 2, 0, 2, 0, 2, 0, 2, 0, 2],
[2, 0, 2, 0, 2, 0, 2, 0, 2, 0]]
This is equivalent to
result = []
for k in (1,1,0,2,2):
for j in (0,1):
row = []
for i in range(10):
row.append(((i+j) % 2)*k)
result.append(row)
I'm going to do the following operation of a list or numpy array:
[0, 0, 0, 1, 0, 0, 4, 2, 0, 7, 0, 0, 0]
move all non-zeros to the right side:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 2, 7]
How can I do this efficiently?
Thanks
============
Sorry I didn't make it clear, I need the order of non-zeros elements remains.
You could sort the list by their boolean value. All falsy values (just zero for numbers) will get pushed to the front of the list. Python's builtin sort appears stable, so other values will keep their relative position.
Example:
>>> a = [0, 0, 0, 1, 0, 0, 5, 2, 0, 7, 0, 0, 0]
>>> sorted(a, key=bool)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 5, 2, 7]
Using NumPy:
>>> a = np.array([0, 0, 0, 1, 0, 0, 4, 2, 0, 7, 0, 0, 0])
>>> np.concatenate((a[a==0], a[a!=0]))
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 2, 7])
You can do this in O(N) time in Python as well by using a simple for-loop. But will take some extra memory which we can prevent in #grc's solution by using a.sort(key=bool):
>>> from collections import deque
#Using a deque
>>> def solve_deque(lst):
d = deque()
append_l = d.appendleft
append_r = d.append
for x in lst:
if x:
append_r(x)
else:
append_l(x)
return list(d) #Convert to list if you want O(1) indexing.
...
#Using simple list
>>> def solve_list(lst):
left = []
right = []
left_a = left.append
right_a = right.append
for x in lst:
if x:
right_a(x)
else:
left_a(x)
left.extend(right)
return left
>>> solve_list([0, 0, 0, 1, 0, 0, 4, 2, 0, 7, 0, 0, 0])
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 2, 7]
>>> solve_deque([0, 0, 0, 1, 0, 0, 4, 2, 0, 7, 0, 0, 0])
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 2, 7]