This question already has answers here:
How to get all possible combinations of a list’s elements?
(32 answers)
Get all combinations of any length without sub-combinations [duplicate]
(1 answer)
Closed 8 months ago.
I have a list which contain values input = [1, 2, 3] and would need all the possible combination of this values i.e. Output = [[], [1], [2], [3], [1,2], [1,3], [2, 3], [1,2,3]].
I have used below code but it is not showing exact output values.
output = permutations([1, 2, 3], 2)
for i in output:
print(list(i))
Can someone help me with this?
From the output you're giving, it seems you want the combinations and not the permutations.
You can iterate over all possible valid lengths (0 to 3) and create a sequence like that.
import itertools as it
list(it.chain.from_iterable(it.combinations([1, 2, 3], i) for i in range(4)))
will output:
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
Related
This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 1 year ago.
For instance, I could have this list:
l = [1, [2, 3], [4, 5, [6, 7], 8]]
and I want to flatten the list like this:
l = [1, 2, 3, 4, 5, 6, 7, 8]
I found some methods online for doing this, but they assume that the list doesn't contain any values outside of sub-lists, or have side effects like reversing the order of elements. What's the best way to do this?
[Edit] I got an existing question recommended, but I couldn't understand what "yield from" does in Python, as I haven't seen it before in that context.
# initializing the data and empty list
data = [1, [2, 3, [4, 5]], 6, [[7], [8, 9]]]
flat_list = []
# function
def flatten_list(data):
# iterating over the data
for element in data:
# checking for list
if type(element) == list:
# calling the same function with current element as new argument
flatten_list(element)
else:
flat_list.append(element)
# flattening the given list
flatten_list(data)
# printing the flat_list
print(flat_list)
(See more at https://geekflare.com/flatten-list-python/)
This question already has answers here:
What is an intuitive explanation of np.unravel_index?
(6 answers)
Closed 3 years ago.
Given an nth place in a 2D static numpy array, how should I calculate that point's position within said array? For an example, given the number 5 representing an nth place, and an array shape of (4, 2), I want to receive the position in the array where that nth place is located, which is position (0, 1). 5th place in an array shape of 10, 1 corresponds to position (5, 0) and so on.
Is there a numpy function that I can use?
Not clear what you mean, try numpy.argwhere(...).
From numpy docs:
>>> x = np.arange(6).reshape(2,3)
>>> x
array([[0, 1, 2],
[3, 4, 5]])
>>> np.argwhere(x>1)
array([[0, 2],
[1, 0],
[1, 1],
[1, 2]])
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 5 years ago.
This is what I came up with but is there a shorter way of doing this? Trying to make a list out of tuple pairs.
x = [[1, 2], [3, 4], [5, 6]]
list1, list2 = zip(*x)
list1=list1+list2
list1=list(list1)
list1.sort()
outcome is [1, 2, 3, 4, 5, 6]
Is there a concept of range in Haskell or other functional programming languages? Suppose I want to compute I_AMAX from BLAS (to find the smallest index of the largest element in vector x). A naive method would be something like
idamax x idx = foldr(imax) 0 (zip x idx)
where idx is the index vector and imax returns the tuple with the smallest index and largest value.
For example, some vector (say, [1, 7, 3, 5]), zipped with its index vector ([0, 1, 2, 3], which makes [(1, 0), (7, 1), (3, 2), (5, 2)]), then reduced with the maximum function (so (7, 1), or 7).
But the index knowledge should be implicit, since in a loop the index is self-evident. Is there a way to write this in Haskell?
tl;dr, what's the best way to write I_AMAX in Haskell?
Here's a version:
idamax :: Ord a => [a] -> Int
idamax = negate . snd . maximum . flip zip [0, -1..]
Explanation
First note that zipping to lists yields a list with length of the shortest input list. So we can use [0..] to attach natural indices to list elements:
zip [0..] [1, 7, 3, 5] = [(0, 1), (1, 7), (2, 3), (3, 5)]
So what we do is:
attach an (negated) index to each element in a list
find a pair with max value and least index (which is max here because of previous negation)
extract index from resulting pair
negate it back
For example, starting with [1, 4, 2, 4]:
attach index: [(1, 0), (4, -1), (2, -2), (4, -3)]
find maximum: (4, -1) (note that (4, -1) > (4, -3))
extract index: -1
negate: 1
Examples
>>> idamax [1..10]
9
>>> idamax [1, 3, 4, 2, 4]
2
>>> idamax "oh hello"
0
I can't figure out how to get permutations to return the actual permutation and not
I tried a lot of different things to no avail. The code I used was from itertools import permutations and then permutations([1,2,3]). Thanks!
This may not be answering your question (it appears to be missing the part after 'and not'), but from your code, what you are likely seeing is the repr of the itertools.permutations iterator. You can iterate through this object just as you would a normal list in order to access all of the items. If you want to convert it to a list, you can wrap it in list:
>>> from itertools import permutations
>>> permutations([1, 2, 3])
<itertools.permutations object at 0x1e67890>
>>> list(permutations([1, 2, 3]))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
However as mentioned above, the iterator can be iterated over just like you would a normal list (the benefit of returning an iterator is that the entire sequence is not loaded into memory right away - it is instead loaded 'as needed'):
>>> for perm in permutations([1, 2, 3]):
... print(perm)
...
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
itertools.permutations is a generator, which means you have to retrieve results from it by using it like this:
for permutation in itertools.permutations([1,2,3]):
do_stuff_with(permutation)
or alternatively put all of them in a list:
list(itertools.permutations([1,2,3]))
or, less conveniently:
generator = itertools.permutations([1,2,3])
generator.__next__()
from itertools import permutations
#iteration
for p in permutations([1,2,3]):
print(p)
This should work perfectly.