I want to make the following combinations:
string.1 string.1
string.2 string.2 string.1
string.3 string.3 string.2 string.1
string.4 string.4 string.3 string.2
string.4 string.1
Each item should be combined with every other one, but the order does not matter, ie string.4 string.1
is the same as string.1 string.4
. Also, there should be a maximum of 4 combinations per line. Each item in the first column is combined with each one in the other columns. For example in line 2, there are the combinations string.2-string.2
& string.2-string.1
.
This is my code:
import itertools
BLOCKS=4
DB="string"
getiter = lambda : itertools.chain( range(1, BLOCKS+1))
for i in getiter():
for j in range(1, BLOCKS, 4):
emit = f"{DB}.{i}"
for k in range(4):
if j + k > BLOCKS:
break
emit += " {}.{}".format(DB, j+k)
print(emit)
And this is my current output:
string.1 string.1 string.2 string.3 string.4
string.2 string.1 string.2 string.3 string.4
string.3 string.1 string.2 string.3 string.4
string.4 string.1 string.2 string.3 string.4
Here, there are combinations that are superfluous. How do I get rid of those?
Maybe something like that:
import itertools
BLOCKS=4
DB="string"
getiter = lambda : itertools.chain( range(1, BLOCKS+1))
for i in getiter():
emit = f"{DB}.{i}"
for k in range(1, BLOCKS+1):
if k > BLOCKS: break
else if k == i: continue #<========== Added
emit += " {}.{}".format(DB, k)
print(emit)
(Note that the second loop (j) seems useless so I did remove it)
Output:
string.1 string.2 string.3 string.4
string.2 string.1 string.3 string.4
string.3 string.1 string.2 string.4
string.4 string.1 string.2 string.3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.