I am working on Project Euler #35, and I need to find the circular permutations of a number. Using itertools
, I can easily get the permutations of a number. However, I want to do it with a list comprehension (as it seems more Pythonic; I am also trying to get familiar with list comprehensions).
I found that all circular primes can only contain the digits 1, 3, 7, and 9 (this excludes 2 and 5, which are circular primes by definition). If any other digit was in the number (0, 2, 4, 5, 6, or 8) one of the permutations would not be a prime (as that digit would be last in at least one of the permutations).
Thus, I tried doing this:
from itertools import permutations
l = [x for x in list(permutations('1397', y)) for y in range(7)]
I needed to use y for y in range(7)
so that I get varying lengths of permutations.
However, this gave me a TypeError
:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
l = [x for x in list(permutations('1397', y)) for y in range(7)]
TypeError: an integer is required
This works, but it isn't using two variables in one list comprehension:
l = []
for y in range(7):
l.append([x for x in list(permutations('1379', y))])
How can I do a double-variable list comprehension? Thanks!
2 List Comprehension. This has two drawbacks: You can only look for one value at a time. It only returns the index of the first occurrence of a value; if there are duplicates, you won't know about them.
As it turns out, you can nest list comprehensions within another list comprehension to further reduce your code and make it easier to read still. As a matter of fact, there's no limit to the number of comprehensions you can nest within each other, which makes it possible to write very complex code in a single line.
Map function is faster than list comprehension when the formula is already defined as a function earlier. So, that map function is used without lambda expression.
The list comprehension method is slightly faster. This is, as we expected, from saving time not calling the append function. The map and filter function do not show a significant speed increase compared to the pure Python loop.
The for y in range(7)
part should come before the permutation loop.:
l = [x for y in range(7) for x in list(permutations('1397', y))]
The above list comprehension is equivalent to :
In [93]: l = []
In [94]: for y in range(7):
...: l.extend(list(permutations('1397', y)))
For example:
In [76]: l = [x for y in range(3) for x in list(permutations('1397', y))]
In [77]: l
Out[77]:
[(),
('1',),
('3',),
('9',),
('7',),
('1', '3'),
('1', '9'),
('1', '7'),
('3', '1'),
('3', '9'),
('3', '7'),
('9', '1'),
('9', '3'),
('9', '7'),
('7', '1'),
('7', '3'),
('7', '9')]
And the list-comprehension
version for your working example,
l = []
for y in range(7):
l.append(list(permutations('1397', y)))
is:
In [85]: l = [list(permutations('1397', y)) for y in range(3)]
In [86]: l
Out[86]:
[[()],
[('1',), ('3',), ('9',), ('7',)],
[('1', '3'),
('1', '9'),
('1', '7'),
('3', '1'),
('3', '9'),
('3', '7'),
('9', '1'),
('9', '3'),
('9', '7'),
('7', '1'),
('7', '3'),
('7', '9')]]
[list(permutations('1397',x)) for x in range(7)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With