1

How can I exclude certain tuples from being written to a file?

I found a useful program that would let me create my own wordlist based on certain characters and a chosen wordlength range. The program creates tuples and write them to a file via a for-loop.

Below is a snippet of the program.

import itertools

char = 'qwer'
output_file = 'Question4Stack.txt'

wordList = open(output_file, 'a')
for i in range(len(char), (len(char) + 1)):
    for xt in itertools.product(char, repeat=i):
        # xt retuns a tuple. Some of which I want to skip with continue.
        wordList.write(''.join(xt) + '\n')
wordList.close()

print("\nDone Sucessfully")

I want the program to be able to skip certain tuples in which there are 3 or more of the same character present.

Outcome of snippet: (Before writing to a file)

1. ('q','q','q','q')
2. ('q','q','q','w')
3. ('q','q','q','e')
4. ('q','q','q','r')
5. ('q','q','w','q')
6. ('q','q','w','w')
7. ('q','q','w','e')
8. ('q','q','w','r')

How to skip tuple 1 - 4 in the for-loop?

2Chill
  • 11
  • 1

2 Answers2

0

You can use a Counter to get the maximum number of the same character in the tuple. Then just continue without writing if it's more than 2.

import itertools
from collections import Counter

char = 'qwer'
output_file = 'Question4Stack.txt'

wordList = open(output_file, 'a')
for i in range(len(char), (len(char) + 1)):
    for xt in itertools.product(char, repeat=i):
        cnt = Counter(xt)
        if max(cnt.values()) > 2:
            continue
        # xt retuns a tuple. Some of which I want to skip with continue.
        wordList.write(''.join(xt) + '\n')
wordList.close()

print("\nDone Sucessfully")

EDIT: This is for only counting consecutive characters as a group

import itertools
from collections import Counter

char = 'qwer'
output_file = 'Question4Stack.txt'

wordList = open(output_file, 'a')
for i in range(len(char), (len(char) + 1)):
    for xt in itertools.product(char, repeat=i):
        cnts = [sum(1 for i in grp[1]) for grp in itertools.groupby(xt)]
        if max(cnts) > 2:
            continue
        # xt retuns a tuple. Some of which I want to skip with continue.
        wordList.write(''.join(xt) + '\n')
wordList.close()

print("\nDone Sucessfully")
PyPingu
  • 1,697
  • 1
  • 8
  • 21
  • That is a great solution! But what if you want to include tuples like: `('q','w',q,'e')`? – 2Chill Aug 17 '19 at 14:19
  • That tuple would be included, because it has less than 3 as the max value in the counter. – PyPingu Aug 17 '19 at 14:21
  • You are right, I'm sorry. I wanted to edit my previous comment but I was to late to correct my mistake. And what if you only want to exclude tuples which has sequential characters in them like: `('q','q','q','e')`? – 2Chill Aug 17 '19 at 14:37
  • Thank you, PyPingu! That is exactly what I need. Plus I'm deep diving in the Itertools Module. Thank you so much for you time and help. Grt, M – 2Chill Sep 02 '19 at 08:42
0

You can try this:

import itertools
import pandas as pd

char = 'qwer'
output_file = 'Question4Stack.txt'

wordList = open(output_file, 'a')
for i in range(len(char), (len(char) + 1)):
    for xt in itertools.product(char, repeat=i):
        if pd.Series(xt).value_counts().max() < 3:
            # xt retuns a tuple. Some of which I want to skip with continue.
            print(xt)
            wordList.write(''.join(xt) + '\n')
wordList.close()

print("\nDone Sucessfully")
René
  • 4,594
  • 5
  • 23
  • 52