I have two arrays:
import numpy as np
MC=np.array([1, 2, 3])
oo=np.array([4,5,6])
for i in range(0, len(MC)):
for j in range(0, len(oo)):
H[i,j]=(MC[i],oo[j])
print(oo)
print(H)
I want an output matrix like this and I get an error (list indices must be integers or slices, not tuple):
H=[[(1,4),(1,5),(1,6)],[(2,4),(2,5),(2,6)],[(3,4),(3,5),(3,6)]]
"I get an error" is quite unspecific. So you should provide more details. But I assume that you get a NameError since H is not defined anywhere before using it. But looking at you implementation I can't see how you would get the desired result anyway. If you generally don't know the length of the given arrays in advance you could try
H = []
for i in range(0, len(MC)):
H.append([])
for j in range(0, len(oo)):
H[-1].append((MC[i], oo[j]))
That should give you the desired output. Or in my opinion even better, particulary in terms of readability:
H = []
for mc_el in MC:
H.append([])
for oo_el in oo:
H[-1].append((mc_el, oo_el))
Or (inspired by nikoros' answer):
H = [[(MC_el, oo_el) for oo_el in oo] for MC_el in MC]
Instead of manually iterating the arrays, you can combine groupby and product from itertools, you can create the required list of list of tuples in List-Comprehension
>>> from itertools import groupby, product
>>> [list(product([k], oo)) for k,v in groupby(MC)]
[[(1, 4), (1, 5), (1, 6)], [(2, 4), (2, 5), (2, 6)], [(3, 4), (3, 5), (3, 6)]]
Related
I'm committing a crime in python on purpose. This is a bad way of doing this.
The GOAL here is cursed code. All on one line.
I have basically whats below
with open("file") as f:
[int(x) for x in [y for y in f.read().split()]
I cannot use
with open("file") as f:
a = f.read().split()
[x for x in [(a[i-1, e]) for i, e in enumerate(a) if i > 0] ...]
because the goal is to have this in one line (aside from the with open)
I would like to return from the original object the current element and either the previous one or the next one.
To illustrate it clearly.
a = [1, 2, 3, 4, 5]
After the illegal code would return
[(1, 2), (2, 3), (3, 4), (4, 5), (5, ?)]
So again the focus here is not production code. This is purely to see how much we can abuse the language.
So far I've found https://code.activestate.com/recipes/204297/ which references the use of local in python2, after mucking around with it I found that the interface for it is a little different.
I've been able to get the object in memory but I dont know how to actually use this object now that I have it.
local()['.0']
Most attributes seem to be missing, no __self__ to call.
Please share your most cursed ideas for this.
Normally, I would use tee and islice on the generator for something like this:
from itertools import tee, islice
with open("file") as f:
a, b = tee(f.read().split())
b = islice(b, 1, None)
list(zip(a, b))
You can convert this into a one-liner using (abusing) the walrus operator (:=):
list(zip((k := tee(f.read().split()))[0], islice(k[1], 1, None)))
The result is
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
If you want the last element to be padded, use zip_longest instead of zip:
from itertools import tee, islice, zip_longest
...
list(zip_longest((k := tee(f.read().split()))[0], islice(k[1], 1, None), fillvalue='?'))
The result in this case is
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, '?')]
The nice thing about using iterators rather than lists this way is that while f.read().split() is a sequence of known length, the tee and islice will work on any iterable, even if the length is unknown.
I'd like to make a recursive algorithm generating all permutations of a list of integers taken k things at a time.
To be specific, what I want to do is create a recursive function Perm(list, k) from scratch returning a output satisfying the following condition.
from itertools import permutations
li = [1,2,3,4]
set(permutations(li, 2)) == set(Perm(li, 2))
I tried it by referring to the Shailaja's codes (Perm(lst,n)) from this link: Recursive Algorithm to generate all permutations of length k of a list in Python
Since the function returns a nested list, I've tried to convert the nested list to a set of tuples. However, I was unable to find any solutions because the function is a recursive algorithm. Could anyone help me out to change the Perm function to get the following format of outputs? Thanks a lot.
# the output of set(Perm(li, 2))
{(1, 2),
(1, 3),
(1, 4),
(2, 1),
(2, 3),
(2, 4),
(3, 1),
(3, 2),
(3, 4),
(4, 1),
(4, 2),
(4, 3)}
Since the function returns a nested list, I've tried to convert the nested list to a set of tuples.
Yes, that is indeed what is needed. So Perm should yield tuples.
Here is a possible recursive implementation for Perm:
def Perm(lst, size):
if size <= 0:
yield () # empty tuple
else:
for i, val in enumerate(lst):
for p in Perm(lst[:i] + lst[i+1:], size-1):
yield (val, *p)
This passes the test given in the question.
I want to combine all elements of a list into sublists (not tuples) with a specified length.
The itertools.combinations_with_replacement generator does nearly what I want to achieve:
>>> list(itertools.combinations_with_replacement([1,2],2))
[(1, 1), (1, 2), (2, 2)]
There are only two things I dislike: It creates tuples instead of sublists (what I could change with a map), and it misses the element (2,1) in the example above.
Is there any builtin module in Python 2 that does what I want? If not, is there any simple way to at least get combinations_with_replacements (or any other module function) to generate the missing element in the provided example?
maybe:
>>> from itertools import product
>>> list(product([1, 2], repeat=2))
[(1, 1), (1, 2), (2, 1), (2, 2)]
probably basic, but couldn't find it in any other question.
I tried:
print ["".join(seq) for seq in itertools.permutations("00011")]
but got lots of duplications, seems like itertools doesn't understand all zeroes and all ones are the same...
what am I missing?
EDIT:
oops. Thanks to Gareth I've found out this question is a dup of: permutations with unique values.
Not closing it as I think my phrasing of the question is clearer.
list(itertools.combinations(range(5), 2))
returns a list of 10 positions where the two ones can be within the five-digits (others are zero):
[(0, 1),
(0, 2),
(0, 3),
(0, 4),
(1, 2),
(1, 3),
(1, 4),
(2, 3),
(2, 4),
(3, 4)]
For your case with 2 ones and 13 zeros, use this:
list(itertools.combinations(range(5), 2))
which returns a list of 105 positions. And it is much faster than your original solution.
Now the function:
def combiner(zeros=3, ones=2):
for indices in itertools.combinations(range(zeros+ones), ones):
item = ['0'] * (zeros+ones)
for index in indices:
item[index] = '1'
yield ''.join(item)
print list(combiner(3, 2))
['11000',
'01100',
'01010',
'01001',
'00101',
'00110',
'10001',
'10010',
'00011',
'10100']
and this needs 14.4µs.
list(combiner(13, 2))
returning 105 elements needs 134µs.
set("".join(seq) for seq in itertools.permutations("00011"))
I'd like to make an iteration to calculate all the possibilities of a given formula. I need to write down nested iteration but couldn't make it right. I am not good at algorithm :(
For calculating all the possibilities(%0-%100) 3 constant{z1,z2,z3} values, I prepared:
a=frange(0,1.0,0.01)
for z1 in a:
for z2 in a:
for z3 in a:
calculate(z1,z2,z3)
and works properly as I expected.
If z is a list which consists of n values(n can be 2-30 in my case), Which algorithm do you suggest to me to handle this? How can I create nested iteration?
The easiest way is to use itertools.product():
a=frange(0,1.0,0.01)
for z in itertools.product(a, repeat=n):
calculate(*z)
If n really would be 30, this would iterate over 100**30 = 10**60 values. Be prepared to wait.
itertools.product will do what you want (and more). Unfortunately it wants the lists whose products it computes in separate arguments, like this:
>>> list(itertools.product([1,2,3],[1,2,3]))
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
so on the face of it you need to do something like this:
a=frange(0,1.0,0.01)
for (z1,z2,z3) in itertools.product(a,a,a): calculate(z1,z2,z3)
but if you want to use the exact code for different numbers of products you can say
a=frange(0,1.0,0.01)
for (z1,z2,z3) in itertools.product(*(3*[a])): calculate(z1,z2,z3)
or
a=frange(0,1.0,0.01)
for (z1,z2,z3) in apply(itertools.product, 3*[a]): calculate(z1,z2,z3)