1

I am having some trouble reaching my goal of reducing coupons for betting in python.

I am trying to reduced rows of bets in 1X2-form to combined coupons. Since the rows are generated by another software they come as single rows and not combined ie. 8 games on one row = 1X21X21X, 1XX1X21X, 1X11X21X etc.

Better look at 3 rows:

1 1 1
X X X
2 X 1 <-- diff
1 1 1
X X X
2 2 2
1 1 1
X X X

The diff could of course be anywhere and there could be diffs in all positions.

Now I want to read every row and then combine as many rows as possible to something like this: 1,X,1X2,1,X,2,1,X (example for the three rows above)

Better look:

1
X
1X2
1
X
2
1
X

I have tried to get this result by looping through the rows and matching them one by one to the same rows. But to no avail.

Last effort has resulted in a nested dict like this:

{u'1': {u'X': {u'1': {u'1': {u'X': {u'2': {u'1': {u'X': ''}}}}}, 
               u'X': {u'1': {u'X': {u'2': {u'1': {u'X': ''}}}}}, 
               u'2': {u'1': {u'X': {u'2': {u'1': {u'X': ''}}}}}}}}

But from there I have no good idea of how to do. And is this a good way to go?

I am very thankful for any help in this matter, preferably with some code examples. Thanks!

Update, thanks to Aaron Hall:

My 20 rows:

product_list = '''2222XX12
22X2XX12
2212XX12
221XX212
221XX112
222XX212
222XX112
22XXX212
22XXX112
22X2X212
22X2X112
2X1XX212
2X1XX112
2X12X212
2X12XX12
2X12X112
2XX2X212
2XX2XX12
2XX2X112
2XXXX212
'''

Making the betting slip:

product_list = product_list.splitlines()

betting_slip = [set(i) for i in zip(*product_list)]

We get one betting slip:

pprint(betting_slip)

[set([u'2']),
 set([u'2', u'X']),
 set([u'1', u'2', u'X']),
 set([u'2', u'X']),
 set([u'2']),
 set([u'1', u'2', u'X']),
 set([u'1']),
 set([u'2']),

Making that betting slip back to single rows:

txt_sets = [set(line) for line in betting_slip]
product_list = list(itertools.product(*txt_sets))

product_list = [''.join(tuple_i) for tuple_i in product_list]

And that results in 36 rows:

pprint(product_list)

[u'2X1XX112',
 u'2X1XXX12',
 u'2X1XX212'
 ...

Problem

But that's the problem I have right now. The betting slip we created now holds 36 rows or combinations. What I want is betting slip/slips that only holds those 20 rows that we started with.

I made a picture illustrating the betting slips: https://i.stack.imgur.com/5tEEn.png

As you can see the 20 rows should result in 6 betting slips and not one that holds 36 rows.

dinc
  • 21
  • 3
  • 2
    I see you're new, typically good to post the code you've already tried and perhaps the results you expect to get. – Russia Must Remove Putin Feb 24 '14 at 23:31
  • 2
    I dont understand what you are trying to do ... – Joran Beasley Feb 24 '14 at 23:31
  • Is the below what you're trying to accomplish? – Russia Must Remove Putin Feb 24 '14 at 23:40
  • @AaronHall Well, what I am trying to do is finding the rows that are connected. I am going to make an update trying to explain it better. – dinc Feb 24 '14 at 23:44
  • @AaronHall I was actually looking through the docs for itertools right now :) And what you showed is on the right track, but I am actually trying to do the reversed. Making betting slips from single rows. Do you have any ideas on how to do that? – dinc Feb 25 '14 at 00:33
  • @AaronHall Ahh, hehe I see I have a lot to learn here :) Well, you almost got it ;) The thing is that when we take the 20 unique rows and combine them and then make them as single rows again, they have increased to 36 rows. We need to create unique betting slips with only those 20 rows in them. I made an update with an image of those 6 slips, just as an example. So how to make set() understand that we only want combined sets with those 20 unique rows? BTW, thanks for your help! :) Very appreciated! – dinc Feb 25 '14 at 01:11
  • I don't see what's special about your 20 "unique" rows. Can you tell me what's special or important about those 20 rows? You're not being clear about your constraints. I've had to puzzle over what you've been asking since you've began. – Russia Must Remove Putin Feb 25 '14 at 02:01
  • @AaronHall I updated my question with your code and tried to make it clearer on what I am trying to achieve. Thank you for your help, it helped me a lot! – dinc Feb 25 '14 at 09:49

1 Answers1

0

Are you trying to split on the lines, and reduce the redundant elements?

txt = '''1 1 1
X X X
2 X 1
1 1 1
X X X
2 2 2
1 1 1
X X X'''

You can remove the spaces, and then split on the lines:

txt_list = txt.replace(' ', '').splitlines()

The txt_list is:

['111', 'XXX', '2X1', '111', 'XXX', '222', '111', 'XXX']

And then remove redundant elements in each string by putting the strings into sets:

txt_sets = [set(line) for line in txt_list]

for i in txt_sets:
    print(i)

prints out:

set(['1'])
set(['X'])
set(['1', 'X', '2'])
set(['1'])
set(['X'])
set(['2'])
set(['1'])
set(['X'])

But it's important to keep in mind that sets are not guaranteed to retain order, so the multiple element sets may lose order.

But if appears that you're trying to list all possible outcomes being selected by these betting slips?

import itertools
product_list = list(itertools.product(*txt_sets))

and product_list is:

[('1', 'X', '2', '1', 'X', '2', '1', 'X'), 
 ('1', 'X', '1', '1', 'X', '2', '1', 'X'), 
 ('1', 'X', 'X', '1', 'X', '2', '1', 'X')]

Using your image:

txt = '''2
X2
1X2
X2
X
1X2
1
2'''
txt_list = txt.replace(' ', '').splitlines()
txt_sets = [set(line) for line in txt_list]
import itertools
product_list = list(itertools.product(*txt_sets))
len(product_list)

returns 36

and we can combine them in strings,

import pprint
product_list = [''.join(tuple_i) for tuple_i in product_list]
pprint.pprint(product_list)

and the result prints out:

['2X1XX112',
 '2X1XXX12',
 '2X1XX212',
 '2X12X112',
 '2X12XX12',
 '2X12X212',
 '2XXXX112',
 '2XXXXX12',
 '2XXXX212',
 '2XX2X112',
 '2XX2XX12',
 '2XX2X212',
 '2X2XX112',
 '2X2XXX12',
 '2X2XX212',
 '2X22X112',
 '2X22XX12',
 '2X22X212',
 '221XX112',
 '221XXX12',
 '221XX212',
 '2212X112',
 '2212XX12',
 '2212X212',
 '22XXX112',
 '22XXXX12',
 '22XXX212',
 '22X2X112',
 '22X2XX12',
 '22X2X212',
 '222XX112',
 '222XXX12',
 '222XX212',
 '2222X112',
 '2222XX12',
 '2222X212']

To get back is even easier:

back_to_sets = [set(i) for i in zip(*product_list)]
pprint.pprint(back_to_sets)

prints out (in Python 3):

[{'2'},
 {'X', '2'},
 {'1', 'X', '2'},
 {'X', '2'},
 {'X'},
 {'1', 'X', '2'},
 {'1'},
 {'2'}]
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • I think you are on to something here. I have updated my post so that it might be a bit clearer. Thank you! – dinc Feb 25 '14 at 22:52