How to reduce many if --for loops!

This is the place for queries that don't fit in any of the other categories.

How to reduce many if --for loops!

Postby birda » Fri Mar 22, 2013 3:32 pm

Code: Select all
Coruption_strings=['guard','guard','boss','guard','thief','court','guard', 'boss','thief','thief','guard','boss','boss','court']
list=[]
for aa in range(len(Coruption_strings)):
    if Coruption_strings[aa]=='court':
      for bb in range(len(Coruption_strings)):
        if bb !=aa:
          if Coruption_strings[bb]=='thief':
               for cc in range(len(Coruption_strings)):
                 if cc !=bb and cc != aa:
                   if Coruption_strings[cc]=='guard':
                      for dd in range(len(Coruption_strings)):
                         if dd!=cc and dd!=bb and dd!=aa:
                           if Coruption_strings[dd]=='court':
                              for ee in range(len(Coruption_strings)):
                                 if ee!=dd and ee!=cc and ee!=bb:
                                    if Coruption_strings[ee]=='court':
                                       list.append([aa,bb,cc,dd,ee])
                                       print 'court-thief-guard-boss-court sequence:', list
                                       print 'number of court-thief-guard-boss-court-sequence:', len(list)
Last edited by birda on Fri Mar 22, 2013 3:52 pm, edited 1 time in total.
birda
 
Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm

Re: How to reduce many if --for loops!

Postby setrofim » Fri Mar 22, 2013 3:40 pm

Please re-post using [code ] tags. See this.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: How to reduce many if --for loops!

Postby setrofim » Fri Mar 22, 2013 4:06 pm

Thanks for re-posting. If I understand correctly what you're trying to do, then
Code: Select all
import itertools

Coruption_strings=['guard','guard','boss','guard','thief','court','guard',
                   'boss','thief','thief','guard','boss','boss','court']

term_map = {}
for i, term in enumerate(Coruption_strings):
    if not term in term_map:
        term_map[term] = []
    term_map[term].append(i)


ctgbc_list = list(itertools.product(term_map['court'], term_map['thief'],
                                    term_map['guard'], term_map['boss'], term_map['court']))

print 'court-thief-guard-boss-court sequence:', ctgbc_list
print 'number of court-thief-guard-boss-court-sequence:', len(ctgbc_list)
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: How to reduce many if --for loops!

Postby birda » Fri Mar 22, 2013 4:23 pm

Hi setrofim, thanks indeed for the fast response.
In fact it is to list and count the number of court-thief-guard-boss-court sequence, court can be repeated but the others can not be repeated!
The above program (my program) produces:
Court-thief-guard-boss-court sequence: [[5, 4, 0, 13, 5], [5, 4, 1, 13, 5], [5, 4, 3, 13, 5], [5, 4, 6, 13, 5], [5, 4, 10, 13, 5], [5, 8, 0, 13, 5], [5, 8, 1, 13, 5], [5, 8, 3, 13, 5], [5, 8, 6, 13, 5], [5, 8, 10, 13, 5], [5,9, 0, 13, 5], [5, 9, 1, 13, 5], [5, 9, 3, 13, 5], [5, 9, 6, 13, 5], [5, 9, 10, 13, 5], [13, 4, 0, 5, 13], [13, 4, 1, 5, 13], [13, 4, 3, 5, 13], [13, 4, 6, 5, 13], [13, 4, 10, 5, 13], [13, 8, 0, 5, 13], [13, 8, 1, 5, 13], [13, 8,3, 5, 13], [13, 8, 6, 5, 13], [13, 8, 10, 5, 13], [13, 9, 0, 5, 13], [13, 9, 1, 5, 13], [13, 9, 3, 5, 13], [13, 9, 6, 5, 13], [13, 9, 10, 5, 13]]
Number of court-thief-guard-boss-court sequence: 30

So your program gives Number of court-thief-guard-boss-court sequence: 240 (except court other indices should not be repeated!)
anyways thanks ;)
birda
 
Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm

Re: How to reduce many if --for loops!

Postby setrofim » Fri Mar 22, 2013 5:28 pm

birda wrote:except court other indices should not be repeated!

Where in my output do you see repeated indexes (apart form the first and last -- 'court')?

The reason you have only 30 sequences returned is becuase you're not generating all the valid results. For example, [5, 4, 6, 12, 13] is a valid court-thief-guard-boss-court sequence but it does not appear in your output.

Your can easiy veryify this by printing out the term_map from my code which shows the mapping of terms (such as 'court' or 'boss') to the corresponding indexes in the list:
Code: Select all
{'boss': [2, 7, 11, 12],
 'court': [5, 13],
 'guard': [0, 1, 3, 6, 10],
 'thief': [4, 8, 9]}


You can find out the expected size of the output by looking at the lengths of the index lists:
Code: Select all
{'boss': 4, 'court': 2, 'guard': 5, 'thief': 3}

So the total number of combinations is
Code: Select all
>>> 2 * 3 * 5 * 4 * 2
240
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: How to reduce many if --for loops!

Postby Yoriz » Fri Mar 22, 2013 5:43 pm

Hows my attempt ?
Code: Select all
from itertools import permutations
from collections import Counter

coruption_strings = ['guard', 'guard', 'boss', 'guard', 'thief', 'court',
                     'guard', 'boss', 'thief', 'thief', 'guard', 'boss',
                     'boss', 'court']

counter = Counter(permutations(coruption_strings, 5))
print counter[('court', 'thief', 'guard', 'boss', 'court')]

120


Though i get a result of 120, i think maybe as one of the courts is used in the first instance so there is only one left for the second instance so the sum becomes 2 * 3 * 5 * 4 * 1 = 120
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 831
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: How to reduce many if --for loops!

Postby setrofim » Fri Mar 22, 2013 5:53 pm

Yoriz wrote:Though i get a result of 120, i think maybe as one of the courts is used in the first instance so there is only one left for the second instance so the sum becomes 2 * 3 * 5 * 4 * 1 = 120

Yes that's correct. Since itertools.permutations() will not repeat elements, you will not get sequences of the form [5, x, x, x, 5] or [13, x, x, x, 13], so you're generating only half of the possible results. Nice use of Counter though (I keep forgetting it exits).
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: How to reduce many if --for loops!

Postby Yoriz » Fri Mar 22, 2013 6:15 pm

setrofim wrote:
Yoriz wrote:Though i get a result of 120, i think maybe as one of the courts is used in the first instance so there is only one left for the second instance so the sum becomes 2 * 3 * 5 * 4 * 1 = 120

Yes that's correct. Since itertools.permutations() will not repeat elements, you will not get sequences of the form [5, x, x, x, 5] or [13, x, x, x, 13], so you're generating only half of the possible results. Nice use of Counter though (I keep forgetting it exits).


O well my codes wrong as usual but i guess at least i realized why and I'm going try and get my head around what product does.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 831
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: How to reduce many if --for loops!

Postby birda » Sat Mar 23, 2013 5:00 pm

Thanks all of you ;)
birda
 
Posts: 9
Joined: Thu Mar 14, 2013 4:08 pm


Return to General Coding Help

Who is online

Users browsing this forum: Yoriz and 4 guests