Generating random lists in Python query - list

I want my program to take numbers from 1 to X and randomly distribute those numbers between X/2 numbers of lists Y times. I don't want a number to be repeated during one shuffle, also I don't want the lists to repeat at all. So if there is list [1,2] there shouldn't be another list containing 1 or 2 in same shuffle and there shouldn't be another [1,2] or [2,1] in the whole result.
This is what I came up with, however, it keeps on repeating numbers. Any adice?
import random
def Shuffler():
amount = int(raw_input("Numbers up to: "))
times = int(raw_input("Number of shuffles: "))
numberslist = range(1,amount+1)
twos = []
thisshuffle = []
final = []
while len(final) < (amount/2)*times:
twos = []
thisshuffle = []
while len(twos) < 2:
if len(numberslist)!=0:
randomnumber = random.choice(numberslist)
if (randomnumber in twos) or (randomnumber in thisshuffle):
numberslist.remove(randomnumber)
else:
twos.append(randomnumber)
thisshuffle.append(randomnumber)
numberslist.remove(randomnumber)
else:
numberslist = range(1,amount+1)
if (twos or list(reversed(twos))) not in final:
final.append(twos)
k=0
for i in range(times): #this shit prints shit
print "%s:" % (i+1), final[k:k+amount/2]
print
k = k + amount/2
Shuffler()
Shuffler()

As ccf pointed out, your requirements aren't trivial. A few more steps and you'd have a sudoku generator :)
I tried a few solutions but they either didn't produce random output or were rather inefficient. Ccf's solution is clearly well written but it seems to have the same problem; it produces ordered output (e.g. [1, 2], [1, 3], [1, 4], [1, 5], [1, 6]).
#cff - wouldn't it be better to use itertools.combinations rather than itertools.permutations to avoid generating repetitions?
Here's a "solution" that's quite similar to ccf's (doesn't produce random output either):
import itertools
def Shuffler():
amount = int(raw_input("Numbers up to: "))
times = int(raw_input("Number of shuffles: "))
rng = range(1, amount+1)
perms = list(itertools.combinations(rng, 2))
lst_single = []
lst_all = []
for p in perms:
if len(lst_all) >= times:
for i, lst in enumerate(lst_all):
print str(i+1) + ": ", lst
break
if len(lst_single) == amount/2:
lst_all.append(lst_single)
lst_single = []
elif p[0] < p[1]:
p = list(p)
lst_single.append(p)
Shuffler()
Output
Numbers up to: 6
Number of shuffles: 3
1: [[1, 2], [1, 3], [1, 4]]
2: [[1, 6], [2, 3], [2, 4]]
3: [[2, 6], [3, 4], [3, 5]]
And here's a slightly hackish solution that seems to produce the output you want but in an inefficient way. It relies on a set for filtering out unwanted combinations but still wastes resources producing them in the first place.
import random
def Shuffler():
amount = int(raw_input("Numbers up to: "))
times = int(raw_input("Number of shuffles: "))
rng = range(1, amount+1)
final = []
lst_len = amount/2
combos_unique = set()
while len(combos_unique) < lst_len*times:
combo_rand = random.sample(rng, 2)
if combo_rand[0] < combo_rand[1]:
combos_unique.add(tuple(combo_rand))
tmp = []
for combo in combos_unique:
tmp.append(list(combo))
if len(tmp) >= lst_len:
final.append(tmp)
tmp = []
for i, lst in enumerate(final):
print str(i+1) + ": ", lst
Shuffler()
Output
Numbers up to: 6
Number of shuffles: 3
1: [[2, 6], [4, 6], [5, 6]]
2: [[4, 5], [1, 3], [1, 6]]
3: [[3, 4], [2, 4], [3, 5]]

You don't want any repeated numbers in one shuffle, and any repeated list ... and so on. This is not an easy task. Plus another fact is that, the unique sets of numbers are fixed, which cannot be set too high. For example, if you set "Numbers up to:" 5, and "Number of shuffles: " 20, for sure you will get repeated numbers.
The issue with your code, I see, is in this if statement:
if (twos or list(reversed(twos))) not in final:
final.append(twos)
(twos or list(reversed(twos))) is logical OR, the result is twos, because twos is not empty. I suggest you change the if statement to:
if (twos not in final) and (list(reversed(twos)) not in final):
final.append(twos)
The following code (python 2.7x) uses permutations and shuffle to generate numbers list. Next, make the list unique (e.g., no [1,2] and [2,1] in same list). then, divide them into groups based on number of shuffles specified by the user. Press any letter, script will exit. Hope it helps:
from itertools import permutations
from random import shuffle
def Shuffler():
try:
amount = input("Numbers up to: ")
p = list(permutations(range(1, amount + 1), 2))
p_uniq = [list(x) for x in p if x[::-1] in p and x[0]<=x[1]]
shuf_max = len(p_uniq) /(amount / 2)
times = shuf_max + 1 # set a higher value to trigger prompt
while times > shuf_max:
shuffle(p_uniq) # shuffle the unique list in place
times = input("Number of shuffles (MAX %s): " % (shuf_max))
else:
for i, group in enumerate(list(zip(*[iter(p_uniq[: (amount /2) * times + 1])]* (amount/2)))):
print "%i: " % (i + 1), list(group)
Shuffler()
except:
print 'quitting...'
Shuffler()
Output:
Numbers up to: 10
Number of shuffles (MAX 9): 8
1: [[6, 7], [1, 9], [2, 5], [5, 9], [9, 10]]
2: [[1, 10], [3, 8], [4, 10], [8, 10], [1, 5]]
3: [[1, 4], [6, 8], [3, 6], [2, 4], [4, 7]]
4: [[2, 10], [5, 8], [3, 9], [1, 7], [4, 9]]
5: [[1, 2], [7, 9], [1, 3], [6, 9], [1, 6]]
6: [[2, 9], [4, 8], [3, 5], [8, 9], [7, 10]]
7: [[2, 7], [2, 3], [7, 8], [3, 7], [3, 10]]
8: [[3, 4], [2, 6], [5, 6], [5, 7], [4, 6]]
Numbers up to:

Related

Classification of same rows in 2D numpy

Hi i want to classify indexes of same rows in 2D numpy array. Is there any function to do it ?
Something like this :
a= [[1,2,3] , [2,3,4] , [5,6,7] ,[1,2,3] ,[1,2,3] , [2,3,4]]
then f(a) returns same row indexes [[0,3,4],[1,5],[2]]
I would appreciate for your solutions
Here's one to output list of arrays of row indices -
def classify_rows(a):
sidx = np.lexsort(a.T)
b = a[sidx]
m = ~(b[1:] == b[:-1]).all(1)
return np.split(sidx, np.flatnonzero(m)+1)
If you need a list of lists as output -
def classify_rows_list(a):
sidx = np.lexsort(a.T)
b = a[sidx]
m = np.concatenate(( [True], ~(b[1:] == b[:-1]).all(1), [True]))
l = sidx.tolist()
idx = np.flatnonzero(m)
return [l[i:j] for i,j in zip(idx[:-1],idx[1:])]
Sample run -
In [78]: a
Out[78]:
array([[1, 2, 3],
[2, 3, 4],
[5, 6, 7],
[1, 2, 3],
[1, 2, 3],
[2, 3, 4]])
In [79]: classify_rows(a)
Out[79]: [array([0, 3, 4]), array([1, 5]), array([2])]
In [80]: classify_rows_list(a)
Out[80]: [[0, 3, 4], [1, 5], [2]]

Zipp lists in python by iterating through a list of lists

I have two lists of lists as follows. To merge them, I usually do the following:
>>>from itertools import imap, ilist
>>>a = [1,2,3]
>>>b = [4,5,6]
>>> c = list(imap(list,izip(a,b)))
>>> c
[[1, 4]], [2, 5], [3, 6]]
However, now I have a list of list as follows:
[[1,2,3],
[4,5,6],
[7,8,9],
]
How do I iterate through each list and pass it to the izip function to obtain the following output:
[[1,4,7],[2,5,8],[3,6,9]]
Answer for edited Question:
>>> input_list=[[1,2,3],
[4,5,6],
[7,8,9],
]
Using map and zip:
>>> map(list,zip(*input_list))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Using imap and izip:
>>> list(imap(list,list(izip(*input_list))))
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Answer for previous question:
By using list comprehension and two for loops:
input_list =[[[1],[2],[3]],
[[4],[5],[6]],
[[7],[8],[9]],
]
out_list = [[] for i in range(len(input_list))]
for each_row in input_list:
for i in range(len(each_row)):
out_list[i].extend(each_row[i])
print out_list
Output:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
I think u need something like this:
input_list =[[1,2,3],
[4,5,6],
[7,8,9],
]
result = []
for i in range(len(input_list)):
temp = []
for list in input_list:
temp.append(list[i])
result.append(temp)
print result
result will be:
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

How can I correct my list format?

first I write a test code:
a = [-0.2364, 2.2524, 8.4896,'a']
l = []
for i,each in enumerate(a,start=1):
l.append(["{}, {}".format(i,each)])
lst = str(l).translate(None, "'")
print lst
and the output is ok
[[1, -0.2364], [2, 2.2524], [3, 8.4896], [4, a]]
This is what I want like add serial number for each item in a list. And then I try use httplib read data and replace test list a.
a = response.read()
l = []
for i,each in enumerate(a,start=1):
l.append(["{}, {}".format(i,each)])
lst = str(l).translate(None, "'")
print a
print lst
But output is changed like :
[-0.035308122832456316]
[[1, [], [2, -], [3, 0], [4, .], [5, 0], [6, 3], [7, 5], [8, 3], [9, 0], [10, 8], [11, 1], [12, 2], [13, 2], [14, 8], [15, 3], [16, 2], [17, 4], [18, 5], [19, 6], [20, 3], [21, 1], [22, 6], [23, ]]]
lista seems ok. but outputlst is not what I expected.For this case I trying to get output like:
[-0.035308122832456316]
[[1,-0.035308122832456316]]
how can I fix this?Thanks!
As I mentioned above if a is a string '[-0.035308122832456316]' or even with multiple list values like this a = '[-0.035308122832456316,89427873479875]' simply use below code and it will work fine for you.
import ast
a = response.read()
a = ast.literal_eval(a) #Convert String representation of list to list
l = []
for i,each in enumerate(a,start=1):
l.append(["{}, {}".format(i,each)])
lst = str(l).translate(None, "'")
print a
print lst
Try working with a dictionary instead of a list:
a = [-0.2364, 2.2524, 8.4896, 'a']
l = {}
for i, each in enumerate(a, start=1):
l[i] = each
print l
To simulate your indication that a is a list with 1 item:
a = [-0.035308122832456316]
l = {}
for i, each in enumerate(a, start=1):
l[i] = each
print l
If, as others have indicated, that a is a string with value "[-0.035308122832456316]", then convert it to a list:
a = "[-0.035308122832456316]"
if isinstance(a, str):
a = [a[1:-1]]
l = {}
for i, each in enumerate(a, start=1):
l[i] = each
print l

Generate Successive Number Pairs - Prolog

I have some code that takes a given list of pairs of numbers and solves for chains of 7. However, it takes an obnoxious amount of time to solve for even one (well, i haven't solved for 1 yet and it has been a large amount of time). I was wondering if there was a better/more efficient way of coding this.
Here's what I did, with out the numbers in the list "L". (the list looks like such: L= [[1,2],[2,3],...])
length(L,LEN),
interval(N1,1,LEN),
interval(N2,1,LEN),
interval(N3,1,LEN),
interval(N4,1,LEN),
interval(N5,1,LEN),
interval(N6,1,LEN),
interval(N7,1,LEN),
nth1(N1,L,A),
nth1(N2,L,B),
nth1(N3,L,C),
nth1(N4,L,D),
nth1(N5,L,E),
nth1(N6,L,F),
nth1(N7,L,G),
nth1(2,A,A2),
nth1(1,B,B1),
A2 = B1,
nth1(2,B,B2),
nth1(1,C,C1),
B2 = C1,
nth1(2,C,C2),
nth1(1,D,D1),
C2 = D1,
nth1(2,D,D2),
nth1(1,E,E1),
D2 = E1,
nth1(2,E,E2),
nth1(1,F,F1),
E2 = F1,
nth1(2,F,F2),
nth1(1,G,G1),
F2 = G1,
nth1(2,G,G2),
nth1(1,A,A1),
G2 = A1,
R = (A,B,C,D,E,F,G).
If I understand your intention correctly, you can write this shorter as
use_module(library(clpfd)).
q(L,R) :-
[A,B,C,D,E,F,G] ins 1 .. 7,
R = [[A,B],[B,C],[C,D],[D,E],[E,F],[F,G],[G,A]],
permutation(L, R),
label([A,B,C,D,E,F,G]).
Example:
3 ?- q([[1,7],[2,3],[5,4],[3,1],[7,6],[6,5],[4,2]],X).
X = [[1, 7], [7, 6], [6, 5], [5, 4], [4, 2], [2, 3], [3, 1]] ;
X = [[2, 3], [3, 1], [1, 7], [7, 6], [6, 5], [5, 4], [4, 2]] ;
X = [[5, 4], [4, 2], [2, 3], [3, 1], [1, 7], [7, 6], [6, 5]] ;
X = [[3, 1], [1, 7], [7, 6], [6, 5], [5, 4], [4, 2], [2, 3]] ;
X = [[7, 6], [6, 5], [5, 4], [4, 2], [2, 3], [3, 1], [1, 7]] ;
X = [[6, 5], [5, 4], [4, 2], [2, 3], [3, 1], [1, 7], [7, 6]] ;
X = [[4, 2], [2, 3], [3, 1], [1, 7], [7, 6], [6, 5], [5, 4]] ;
false.
But your question is really unclear.
update: We can create the kind of lists we use above, of any length, with
vars(N, Vars):-
length(Vars, N).
pairs(Vars, N, Pairs):- % assuming vars(N, Vars)
N #> 0,
append(Vars,[A],[A|B]), % |B| = N
maplist( pair, Vars, B, Pairs).
pair( A, B, [A,B]).
Such that q/2 can be generalized as
gen_q(L,R) :-
length( L, N),
vars( N, Vars),
Vars ins 1 .. N,
pairs( Vars, N, R),
permutation(L, R),
label(Vars).
But computational feasibility of this for larger inputs is another matter entirely. The brute force of permutation/2 may have to be replaced with something more specific.
Also, the N results produced comprise a clear pattern; there's no need to re-enter the search to produce them all after the first one is found.

sum of items in a 2d list

Im trying ti implement a function evenrow() that takes a two dimensional list of integers and returns True if each row of the table sums up to an even number and False otherwise (i.e.., if some row sums up to an odd number)
usage
>>> evenrow([[1, 3], [2, 4], [0, 6]])
True
>>> evenrow([[1, 3], [3, 4], [0, 5]])
False
This is what I got so far:
def evenrow(lst):
for i in range(len(lst)-1):
if sum(lst[i])%2==0: # here is the problem, it only iterates over the first item in the lst [1, 3] - i cant figure this out - range problem?
return True
else:
False
How do I get the loop to iterate over every item [1, 3], [2, 4], [0, 6] in the list and not just the first?
well I have gotten this far now:
def evenrow(lst):
for i in range(len(lst)-1):
if sum(lst[i]) %2 >0:
return False
else:
return True
and i get the following answer when executing different lists:
>>> evenrow([[1, 3], [2, 4], [0, 6]])
True
>>> evenrow([[1, 3], [3, 4], [0, 5]])
False
>>> evenrow([[1, 3, 2], [3, 4, 7], [0, 6, 2]])
True
>>> evenrow([[1, 3, 2], [3, 4, 7], [0, 5, 2]])
True
(the last one is not correct though - should be False) I just dont get why this is not working...
You are returning too early. You should check for all the pairs, only returning True afterwards, or return False if a odd sum is encountered.
Spoiler alert:
def evenrow(lst):
for i in range(len(lst)-1):
if sum(lst[i]) % 2 != 0: # here is the problem, it only iterates over the first item in the lst [1, 3] - i cant figure this out - range problem?
return False
return True
This will achieve the goal.