Related
Working in Python.
Take k and n positive integers.
I have a function F with input a list L of positive integers where len(L) = k, max(L) < n+1. F returns an integer.
I want to return sum([F(L) for L a list of positive integers with len(L) = k, max(L)<n+1])
The naive approach is to have k nested for loops.
value = 0
for jj1 in range(1,n+1):
for jj2 in range(1,n+1):
...
for jjk in range(1,n+1)
value = value + F([jj1,jj2,...,jjk])
return value
This is not satisfactory because I want to take this sum in a loop over various values of k (so sometimes k =1, sometimes k=10).
I'm hoping there is some standard trick to do this?
You want itertools.product.
>>> from itertools import product
>>> n, k = 4, 3
>>> list(product(range(1, n+1), repeat=k))
[(1, 1, 1), (1, 1, 2), (1, 1, 3), (1, 1, 4), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 2, 4), (1, 3, 1), (1, 3, 2), (1, 3, 3), (1, 3, 4), (1, 4, 1), (1, 4, 2), (1, 4, 3), (1, 4, 4), (2, 1, 1), (2, 1, 2), (2, 1, 3), (2, 1, 4), (2, 2, 1), (2, 2, 2), (2, 2, 3), (2, 2, 4), (2, 3, 1), (2, 3, 2), (2, 3, 3), (2, 3, 4), (2, 4, 1), (2, 4, 2), (2, 4, 3), (2, 4, 4), (3, 1, 1), (3, 1, 2), (3, 1, 3), (3, 1, 4), (3, 2, 1), (3, 2, 2), (3, 2, 3), (3, 2, 4), (3, 3, 1), (3, 3, 2), (3, 3, 3), (3, 3, 4), (3, 4, 1), (3, 4, 2), (3, 4, 3), (3, 4, 4), (4, 1, 1), (4, 1, 2), (4, 1, 3), (4, 1, 4), (4, 2, 1), (4, 2, 2), (4, 2, 3), (4, 2, 4), (4, 3, 1), (4, 3, 2), (4, 3, 3), (4, 3, 4), (4, 4, 1), (4, 4, 2), (4, 4, 3), (4, 4, 4)]
Given F, n, and k, you can therefore do:
sum(F(list(L)) for L in product(range(1, n+1), repeat=k))
(or leave off the list call if it's fine for L to be a tuple instead of a list)
Input list
[(1, 0, 3), '21:40:44', (1, 3, 9), (1, 4, 6), '.273']
[(1, 0, 3), '21:43:37', (1, 3, 9), (1, 4, 6), '.060']
[(1, 0, 3), '21:45:27', (1, 3, 9), (1, 4, 6), '.410']
And the result should be like:
[(1, 0, 3), ['21:40:44', '21:43:37', '21:45:27'], (1, 3, 9), (1, 4, 6), ['.273', '.060', '.410']
The goal is to reduce the duplicates, the unique elements in a column should be stored in a list.
How about using zip. It will merge the 3 list and give you output with duplicates. later use set to remove duplicates. and since you don not want list of tuple for single element only, use list(set(x))[0] if len(set(x))==1 else list(set(x))
a=[(1, 0, 3), '21:40:44', (1, 3, 9), (1, 4, 6), '.273']
b=[(1, 0, 3), '21:43:37', (1, 3, 9), (1, 4, 6), '.060']
c=[(1, 0, 3), '21:45:27', (1, 3, 9), (1, 4, 6), '.410']
l=[list(set(x))[0] if len(set(x))==1 else list(set(x)) for x in list(zip(a,b,c))]
print(l)
>>>[(1, 0, 3), ['21:45:27', '21:40:44', '21:43:37'], (1, 3, 9), (1, 4, 6), ['.060', '.273', '.410']]
the simplest way is this:
in_data = [
[(1, 0, 3), '21:40:44', (1, 3, 9), (1, 4, 6), '.273'],
[(1, 0, 3), '21:43:37', (1, 3, 9), (1, 4, 6), '.060'],
[(1, 0, 3), '21:45:27', (1, 3, 9), (1, 4, 6), '.410']
]
res = []
for i in range(len(in_data[0])):
data = list(set([x[i] for x in in_data ]))
data = data[0] if len(data) == 1 else data
res.append(data)
print(res)
>>> [(1, 0, 3), ['21:43:37', '21:40:44', '21:45:27'], (1, 3, 9), (1, 4, 6), ['.273', '.060', '.410']]
If I have a Numpy array like X=np.array([1,2,3,4,5])
I want to put all of the combinations of these numbers into an array.
How would I go about getting an array with each row being a combination of X and the length is how ever many combinations there are?
Assuming from your question you are actually looking for permutations:
from itertools import permutations
import numpy as np
x = np.array([1, 2, 3, 4, 5])
perm_array = np.asarray([p for p in permutations(x)])
print(perm_array.shape)
# prints(120, 5)
Each row of perm_array will contain a permutation of your input.
Watch out that the number of permutations grows extremely quickly!
Available in itertools:
from itertools import combinations
X=np.array([1,2,3,4,5])
X_combinations = {} # dictionary key r: combinations of rank r.
for r in range(len(X) + 1):
X_combinations[r] = list(combinations(X, r))
from pprint import pprint
pprint(X_combinations)
output:
{0: [()],
1: [(1,), (2,), (3,), (4,), (5,)],
2: [(1, 2),
(1, 3),
(1, 4),
(1, 5),
(2, 3),
(2, 4),
(2, 5),
(3, 4),
(3, 5),
(4, 5)],
3: [(1, 2, 3),
(1, 2, 4),
(1, 2, 5),
(1, 3, 4),
(1, 3, 5),
(1, 4, 5),
(2, 3, 4),
(2, 3, 5),
(2, 4, 5),
(3, 4, 5)],
4: [(1, 2, 3, 4), (1, 2, 3, 5), (1, 2, 4, 5), (1, 3, 4, 5), (2, 3, 4, 5)],
5: [(1, 2, 3, 4, 5)]}
let's say I have a list of numbers:
my_list = range(0,1001,50)
I want to make every single possible combination of threes (threes = [a,b,c]) based on my_list, for example: [0,50,100], [0,50,150], ..., [0,50,1000], [0,100,150], ...
Threes don't have to be stored in a list, it's just an example of data structure.
And after that I want to put every single threes' values (a,b,c) to some sort of formula.
How could I make that? I'm kinda beginner in Python and haven't experienced with more complicated mathematical structures yet. Or is it possible to do on some kind of loop...?
Any help would be appreciated.
You can use combinations :
my_list = range(0,1000,50)
from itertools import combinations
combinations(my_list,3)
From the doc :
combinations() p, r r-length tuples, in sorted order, no repeated
elements
It creates an iterable. Converted to a list, it looks like :
[(0, 50, 100), (0, 50, 150), (0, 50, 200), (0, 50, 250), (0, 50, 300), ...
What you want is the wonderful itertools module, that has combinations() in it:
>>> import itertools
>>> list(itertools.combinations(range(10), 3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 1, 5), (0, 1, 6), (0, 1, 7), (0, 1, 8), (0
, 1, 9), (0, 2, 3), (0, 2, 4), (0, 2, 5), (0, 2, 6), (0, 2, 7), (0, 2, 8), (0, 2
, 9), (0, 3, 4), (0, 3, 5), (0, 3, 6), (0, 3, 7), (0, 3, 8), (0, 3, 9), (0, 4, 5
), (0, 4, 6), (0, 4, 7), (0, 4, 8), (0, 4, 9), (0, 5, 6), (0, 5, 7), (0, 5, 8),
(0, 5, 9), (0, 6, 7), (0, 6, 8), (0, 6, 9), (0, 7, 8), (0, 7, 9), (0, 8, 9), (1,
2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 2, 7), (1, 2, 8), (1, 2, 9), (1, 3,
4), (1, 3, 5), (1, 3, 6), (1, 3, 7), (1, 3, 8), (1, 3, 9), (1, 4, 5), (1, 4, 6)
, (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 6), (1, 5, 7), (1, 5, 8), (1, 5, 9), (
1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7, 8), (1, 7, 9), (1, 8, 9), (2, 3, 4), (2,
3, 5), (2, 3, 6), (2, 3, 7), (2, 3, 8), (2, 3, 9), (2, 4, 5), (2, 4, 6), (2, 4,
7), (2, 4, 8), (2, 4, 9), (2, 5, 6), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7),
(2, 6, 8), (2, 6, 9), (2, 7, 8), (2, 7, 9), (2, 8, 9), (3, 4, 5), (3, 4, 6), (3
, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 6), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6
, 7), (3, 6, 8), (3, 6, 9), (3, 7, 8), (3, 7, 9), (3, 8, 9), (4, 5, 6), (4, 5, 7
), (4, 5, 8), (4, 5, 9), (4, 6, 7), (4, 6, 8), (4, 6, 9), (4, 7, 8), (4, 7, 9),
(4, 8, 9), (5, 6, 7), (5, 6, 8), (5, 6, 9), (5, 7, 8), (5, 7, 9), (5, 8, 9), (6,
7, 8), (6, 7, 9), (6, 8, 9), (7, 8, 9)]
Then you can feed that to your function:
def f(a,b,c):
return a * b * c
print [f(*x) for x in itertools.combinations(range(10), 3)]
Imagine I have a list of tuples in this format:
(1, 2, 3)
(1, 0, 2)
(3, 9 , 11)
(0, 2, 8)
(2, 3, 4)
(2, 4, 5)
(2, 7, 8)
....
How could I sort the list by the first element of the tuples, and then by the second? I'd like to get to this list:
(0, 2, 8)
(1, 0, 2)
(1, 2, 3)
(2, 3, 4)
(2, 4, 5)
(2, 7, 8)
(3, 9 , 11)
I was thinking to do a sort for the first element, and then go through the list, and build a hash with subarrays. I will probably overcomplicate things :), and this is why I asked for other ways of doing this sort.
Why not simply let python sort the list for you ?
my_list = [
(1, 2, 3),
(1, 0, 2),
(3, 9 , 11),
(0, 2, 8),
(2, 3, 4),
(2, 4, 5),
(2, 7, 8),
]
print sorted(my_list)
>>>[(0, 2, 8), (1, 0, 2), (1, 2, 3), (2, 3, 4), (2, 4, 5), (2, 7, 8), (3, 9, 11)]
Python automagically does the right thing:
>>> a = [(1, 2, 3), (1, 0, 2), (3, 9, 11), (0, 2, 8), (2, 3, 4), (2, 4, 5), (2, 7, 8)]
>>> a.sort()
>>> a
[(0, 2, 8), (1, 0, 2), (1, 2, 3), (2, 3, 4), (2, 4, 5), (2, 7, 8), (3, 9, 11)]
Tuples are already sorted that way.
Try this:
#!/usr/bin/python2
l = [
(1, 2, 3),
(1, 0, 2),
(3, 9 , 11),
(0, 2, 8),
(2, 3, 4),
(2, 4, 5),
(2, 7, 8),
]
l.sort()
print l
If you don't mind sorting by all three elements, this is really trivial:
>>> l = [(1, 2, 3), (1, 0, 2), (3, 9, 11), (0, 2, 8), (2, 3, 4), (2, 4, 5), (2, 7, 8)]
>>> l.sort()
>>> l
[(0, 2, 8), (1, 0, 2), (1, 2, 3), (2, 3, 4), (2, 4, 5), (2, 7, 8), (3, 9, 11)]
>>> x = [
... (1, 2, 3),
... (1, 0, 2),
... (3, 9 , 11),
... (0, 2, 8),
... (2, 3, 4),
... (2, 4, 5),
... (2, 7, 8),
... ]
>>> x.sort()
>>> x
[(0, 2, 8), (1, 0, 2), (1, 2, 3), (2, 3, 4), (2, 4, 5), (2, 7, 8), (3, 9, 11)]