Python script to generate words from letters and two letter combination - python
I'm looking to write a short script that will allow me to generate all possible letter combinations with the parameters I set.
For example:
_ _ b _ a
Parameters:
word = 5 letters
3th, 5th letter = b, a
1st letter = ph, sd, nn, mm or gh
2nd, 4rd letter = any vowel (aeiouy) and rc
in other words, I'm looking to write a script that would return me all 5*7*7 results.
Thank you
You can create iterables for keeping the possible alternatives for each place:
firsts = ['ph', 'sd', 'nn', 'mm', 'gh']
seconds = fourths = ['a', 'e', 'i', 'o', 'u', 'y', 'rc']
thirds = 'b'
fifths = 'a'
List comprehension
You could use a list comprehension:
print [''.join((first, second, third, fourth, fifth))
for first in firsts
for second in seconds
for third in thirds
for fourth in fourths
for fifth in fifths]
Output
['phabaa', 'phabea', 'phabia', 'phaboa', 'phabua', 'phabya', 'phabrca', 'phebaa', 'phebea', 'phebia', 'pheboa', 'phebua', 'phebya', 'phebrca', 'phibaa', 'phibea', 'phibia', 'phiboa', 'phibua', 'phibya', 'phibrca', 'phobaa', 'phobea', 'phobia', 'phoboa', 'phobua', 'phobya', 'phobrca', 'phubaa', 'phubea', 'phubia', 'phuboa', 'phubua', 'phubya', 'phubrca', 'phybaa', 'phybea', 'phybia', 'phyboa', 'phybua', 'phybya', 'phybrca', 'phrcbaa', 'phrcbea', 'phrcbia', 'phrcboa', 'phrcbua', 'phrcbya', 'phrcbrca', 'sdabaa', 'sdabea', 'sdabia', 'sdaboa', 'sdabua', 'sdabya', 'sdabrca', 'sdebaa', 'sdebea', 'sdebia', 'sdeboa', 'sdebua', 'sdebya', 'sdebrca', 'sdibaa', 'sdibea', 'sdibia', 'sdiboa', 'sdibua', 'sdibya', 'sdibrca', 'sdobaa', 'sdobea', 'sdobia', 'sdoboa', 'sdobua', 'sdobya', 'sdobrca', 'sdubaa', 'sdubea', 'sdubia', 'sduboa', 'sdubua', 'sdubya', 'sdubrca', 'sdybaa', 'sdybea', 'sdybia', 'sdyboa', 'sdybua', 'sdybya', 'sdybrca', 'sdrcbaa', 'sdrcbea', 'sdrcbia', 'sdrcboa', 'sdrcbua', 'sdrcbya', 'sdrcbrca', 'nnabaa', 'nnabea', 'nnabia', 'nnaboa', 'nnabua', 'nnabya', 'nnabrca', 'nnebaa', 'nnebea', 'nnebia', 'nneboa', 'nnebua', 'nnebya', 'nnebrca', 'nnibaa', 'nnibea', 'nnibia', 'nniboa', 'nnibua', 'nnibya', 'nnibrca', 'nnobaa', 'nnobea', 'nnobia', 'nnoboa', 'nnobua', 'nnobya', 'nnobrca', 'nnubaa', 'nnubea', 'nnubia', 'nnuboa', 'nnubua', 'nnubya', 'nnubrca', 'nnybaa', 'nnybea', 'nnybia', 'nnyboa', 'nnybua', 'nnybya', 'nnybrca', 'nnrcbaa', 'nnrcbea', 'nnrcbia', 'nnrcboa', 'nnrcbua', 'nnrcbya', 'nnrcbrca', 'mmabaa', 'mmabea', 'mmabia', 'mmaboa', 'mmabua', 'mmabya', 'mmabrca', 'mmebaa', 'mmebea', 'mmebia', 'mmeboa', 'mmebua', 'mmebya', 'mmebrca', 'mmibaa', 'mmibea', 'mmibia', 'mmiboa', 'mmibua', 'mmibya', 'mmibrca', 'mmobaa', 'mmobea', 'mmobia', 'mmoboa', 'mmobua', 'mmobya', 'mmobrca', 'mmubaa', 'mmubea', 'mmubia', 'mmuboa', 'mmubua', 'mmubya', 'mmubrca', 'mmybaa', 'mmybea', 'mmybia', 'mmyboa', 'mmybua', 'mmybya', 'mmybrca', 'mmrcbaa', 'mmrcbea', 'mmrcbia', 'mmrcboa', 'mmrcbua', 'mmrcbya', 'mmrcbrca', 'ghabaa', 'ghabea', 'ghabia', 'ghaboa', 'ghabua', 'ghabya', 'ghabrca', 'ghebaa', 'ghebea', 'ghebia', 'gheboa', 'ghebua', 'ghebya', 'ghebrca', 'ghibaa', 'ghibea', 'ghibia', 'ghiboa', 'ghibua', 'ghibya', 'ghibrca', 'ghobaa', 'ghobea', 'ghobia', 'ghoboa', 'ghobua', 'ghobya', 'ghobrca', 'ghubaa', 'ghubea', 'ghubia', 'ghuboa', 'ghubua', 'ghubya', 'ghubrca', 'ghybaa', 'ghybea', 'ghybia', 'ghyboa', 'ghybua', 'ghybya', 'ghybrca', 'ghrcbaa', 'ghrcbea', 'ghrcbia', 'ghrcboa', 'ghrcbua', 'ghrcbya', 'ghrcbrca']
itertools.product
Another nice way is to use itertools.product:
from itertools import product
print [''.join(letters)
for letters in product(firsts, seconds, thirds, fourths, fifths)]
Output
['phabaa', 'phabea', 'phabia', 'phaboa', 'phabua', 'phabya', 'phabrca', 'phebaa', 'phebea', 'phebia', 'pheboa', 'phebua', 'phebya', 'phebrca', 'phibaa', 'phibea', 'phibia', 'phiboa', 'phibua', 'phibya', 'phibrca', 'phobaa', 'phobea', 'phobia', 'phoboa', 'phobua', 'phobya', 'phobrca', 'phubaa', 'phubea', 'phubia', 'phuboa', 'phubua', 'phubya', 'phubrca', 'phybaa', 'phybea', 'phybia', 'phyboa', 'phybua', 'phybya', 'phybrca', 'phrcbaa', 'phrcbea', 'phrcbia', 'phrcboa', 'phrcbua', 'phrcbya', 'phrcbrca', 'sdabaa', 'sdabea', 'sdabia', 'sdaboa', 'sdabua', 'sdabya', 'sdabrca', 'sdebaa', 'sdebea', 'sdebia', 'sdeboa', 'sdebua', 'sdebya', 'sdebrca', 'sdibaa', 'sdibea', 'sdibia', 'sdiboa', 'sdibua', 'sdibya', 'sdibrca', 'sdobaa', 'sdobea', 'sdobia', 'sdoboa', 'sdobua', 'sdobya', 'sdobrca', 'sdubaa', 'sdubea', 'sdubia', 'sduboa', 'sdubua', 'sdubya', 'sdubrca', 'sdybaa', 'sdybea', 'sdybia', 'sdyboa', 'sdybua', 'sdybya', 'sdybrca', 'sdrcbaa', 'sdrcbea', 'sdrcbia', 'sdrcboa', 'sdrcbua', 'sdrcbya', 'sdrcbrca', 'nnabaa', 'nnabea', 'nnabia', 'nnaboa', 'nnabua', 'nnabya', 'nnabrca', 'nnebaa', 'nnebea', 'nnebia', 'nneboa', 'nnebua', 'nnebya', 'nnebrca', 'nnibaa', 'nnibea', 'nnibia', 'nniboa', 'nnibua', 'nnibya', 'nnibrca', 'nnobaa', 'nnobea', 'nnobia', 'nnoboa', 'nnobua', 'nnobya', 'nnobrca', 'nnubaa', 'nnubea', 'nnubia', 'nnuboa', 'nnubua', 'nnubya', 'nnubrca', 'nnybaa', 'nnybea', 'nnybia', 'nnyboa', 'nnybua', 'nnybya', 'nnybrca', 'nnrcbaa', 'nnrcbea', 'nnrcbia', 'nnrcboa', 'nnrcbua', 'nnrcbya', 'nnrcbrca', 'mmabaa', 'mmabea', 'mmabia', 'mmaboa', 'mmabua', 'mmabya', 'mmabrca', 'mmebaa', 'mmebea', 'mmebia', 'mmeboa', 'mmebua', 'mmebya', 'mmebrca', 'mmibaa', 'mmibea', 'mmibia', 'mmiboa', 'mmibua', 'mmibya', 'mmibrca', 'mmobaa', 'mmobea', 'mmobia', 'mmoboa', 'mmobua', 'mmobya', 'mmobrca', 'mmubaa', 'mmubea', 'mmubia', 'mmuboa', 'mmubua', 'mmubya', 'mmubrca', 'mmybaa', 'mmybea', 'mmybia', 'mmyboa', 'mmybua', 'mmybya', 'mmybrca', 'mmrcbaa', 'mmrcbea', 'mmrcbia', 'mmrcboa', 'mmrcbua', 'mmrcbya', 'mmrcbrca', 'ghabaa', 'ghabea', 'ghabia', 'ghaboa', 'ghabua', 'ghabya', 'ghabrca', 'ghebaa', 'ghebea', 'ghebia', 'gheboa', 'ghebua', 'ghebya', 'ghebrca', 'ghibaa', 'ghibea', 'ghibia', 'ghiboa', 'ghibua', 'ghibya', 'ghibrca', 'ghobaa', 'ghobea', 'ghobia', 'ghoboa', 'ghobua', 'ghobya', 'ghobrca', 'ghubaa', 'ghubea', 'ghubia', 'ghuboa', 'ghubua', 'ghubya', 'ghubrca', 'ghybaa', 'ghybea', 'ghybia', 'ghyboa', 'ghybua', 'ghybya', 'ghybrca', 'ghrcbaa', 'ghrcbea', 'ghrcbia', 'ghrcboa', 'ghrcbua', 'ghrcbya', 'ghrcbrca']
The nice part of this second solution is that you don't have to hardcode the logic, and if needed you could just replace the iterables with others, even when you have more or less places:
from itertools import product
def genwords(*iterables):
return [''.join(letters) for letters in product(*iterables)]
print genwords(firsts, seconds, thirds, fourths, fifths)
print genwords('123', 'abc')
Output
['phabaa', 'phabea', 'phabia', 'phaboa', 'phabua', 'phabya', 'phabrca', 'phebaa', 'phebea', 'phebia', 'pheboa', 'phebua', 'phebya', 'phebrca', 'phibaa', 'phibea', 'phibia', 'phiboa', 'phibua', 'phibya', 'phibrca', 'phobaa', 'phobea', 'phobia', 'phoboa', 'phobua', 'phobya', 'phobrca', 'phubaa', 'phubea', 'phubia', 'phuboa', 'phubua', 'phubya', 'phubrca', 'phybaa', 'phybea', 'phybia', 'phyboa', 'phybua', 'phybya', 'phybrca', 'phrcbaa', 'phrcbea', 'phrcbia', 'phrcboa', 'phrcbua', 'phrcbya', 'phrcbrca', 'sdabaa', 'sdabea', 'sdabia', 'sdaboa', 'sdabua', 'sdabya', 'sdabrca', 'sdebaa', 'sdebea', 'sdebia', 'sdeboa', 'sdebua', 'sdebya', 'sdebrca', 'sdibaa', 'sdibea', 'sdibia', 'sdiboa', 'sdibua', 'sdibya', 'sdibrca', 'sdobaa', 'sdobea', 'sdobia', 'sdoboa', 'sdobua', 'sdobya', 'sdobrca', 'sdubaa', 'sdubea', 'sdubia', 'sduboa', 'sdubua', 'sdubya', 'sdubrca', 'sdybaa', 'sdybea', 'sdybia', 'sdyboa', 'sdybua', 'sdybya', 'sdybrca', 'sdrcbaa', 'sdrcbea', 'sdrcbia', 'sdrcboa', 'sdrcbua', 'sdrcbya', 'sdrcbrca', 'nnabaa', 'nnabea', 'nnabia', 'nnaboa', 'nnabua', 'nnabya', 'nnabrca', 'nnebaa', 'nnebea', 'nnebia', 'nneboa', 'nnebua', 'nnebya', 'nnebrca', 'nnibaa', 'nnibea', 'nnibia', 'nniboa', 'nnibua', 'nnibya', 'nnibrca', 'nnobaa', 'nnobea', 'nnobia', 'nnoboa', 'nnobua', 'nnobya', 'nnobrca', 'nnubaa', 'nnubea', 'nnubia', 'nnuboa', 'nnubua', 'nnubya', 'nnubrca', 'nnybaa', 'nnybea', 'nnybia', 'nnyboa', 'nnybua', 'nnybya', 'nnybrca', 'nnrcbaa', 'nnrcbea', 'nnrcbia', 'nnrcboa', 'nnrcbua', 'nnrcbya', 'nnrcbrca', 'mmabaa', 'mmabea', 'mmabia', 'mmaboa', 'mmabua', 'mmabya', 'mmabrca', 'mmebaa', 'mmebea', 'mmebia', 'mmeboa', 'mmebua', 'mmebya', 'mmebrca', 'mmibaa', 'mmibea', 'mmibia', 'mmiboa', 'mmibua', 'mmibya', 'mmibrca', 'mmobaa', 'mmobea', 'mmobia', 'mmoboa', 'mmobua', 'mmobya', 'mmobrca', 'mmubaa', 'mmubea', 'mmubia', 'mmuboa', 'mmubua', 'mmubya', 'mmubrca', 'mmybaa', 'mmybea', 'mmybia', 'mmyboa', 'mmybua', 'mmybya', 'mmybrca', 'mmrcbaa', 'mmrcbea', 'mmrcbia', 'mmrcboa', 'mmrcbua', 'mmrcbya', 'mmrcbrca', 'ghabaa', 'ghabea', 'ghabia', 'ghaboa', 'ghabua', 'ghabya', 'ghabrca', 'ghebaa', 'ghebea', 'ghebia', 'gheboa', 'ghebua', 'ghebya', 'ghebrca', 'ghibaa', 'ghibea', 'ghibia', 'ghiboa', 'ghibua', 'ghibya', 'ghibrca', 'ghobaa', 'ghobea', 'ghobia', 'ghoboa', 'ghobua', 'ghobya', 'ghobrca', 'ghubaa', 'ghubea', 'ghubia', 'ghuboa', 'ghubua', 'ghubya', 'ghubrca', 'ghybaa', 'ghybea', 'ghybia', 'ghyboa', 'ghybua', 'ghybya', 'ghybrca', 'ghrcbaa', 'ghrcbea', 'ghrcbia', 'ghrcboa', 'ghrcbua', 'ghrcbya', 'ghrcbrca']
['1a', '1b', '1c', '2a', '2b', '2c', '3a', '3b', '3c']
I would approach this as follows, using itertools.product in a generator function (to avoid building the whole list unless you absolutely have to):
from itertools import product
def words(definition):
for t in product(*definition):
yield "".join(t)
The only trick is providing the definition in an appropriate format; it must be a list of iterables, each of which provides the options for each "letter". This is easy where each option for a letter is a single character:
>>> list(words(["f", "o", "aeiou"]))
['foa', 'foe', 'foi', 'foo', 'fou']
But with your multiple-character letters you will need to supply a list or tuple:
>>> list(words([['ph', 'sd', 'nn', 'mm', 'gh'],
['a', 'e', 'i', 'o', 'u', 'y', 'rc'],
'b',
['a', 'e', 'i', 'o', 'u', 'y', 'rc'],
'a']))
['phabaa', 'phabea', 'phabia', ..., 'ghrcbya', 'ghrcbrca']
Note that in Python 3.3 onwards, this can be done in a single line with yield from:
def words(definition):
yield from map("".join, product(*definition))
Related
I'm coding in Python and keep receiving: UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 31: ordinal not in range(128)
The code below is the timeclienthandler.py code that I created in Visual Studio Code. It works sometimes but it still gives me the error if I continuously run the code. I don't understand how it works off and on. from time import ctime from threading import Thread import random class TimeClientHandler(Thread): def __init__(self, client): Thread.__init__(self) self.client = client def run(self): msgList = ["There are some idiots who always answer 'No' to every question, now tell me. Are you one of them?","There's nothing to fear. Except maybe that weird guy over there.","If I'm driving you crazy just remember to put on your seat belt.","I wondered why the baseball was getting bigger. Then it hit me.","You're Just Jealous Because The Voices Are Talking To Me.","Quickest way to get on your feet...miss a car payment.","Why do psychics ask your name?","I'm not opinionated. I'm just always right.","Sanity is the playground for the unimaginative.","It isn't homework unless it's due tomorrow."] msg = msgList[random.randint(0,len(msgList))] msge ="\n"+msg self.client.send(bytes(ctime() + msge,"ascii")) self.client.close()
The error you specified is occuring at line 18 in your code Specifically - bytes(ctime() + msge,"ascii") This error occured because of encoding you specified "ascii" which only supports the following characters: ['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\t', '\n', '\x0b', '\x0c', '\r', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\x7f'] but the input you are giving to it is '\u2019' or {’}, which is not in the above list hence resulting in the UnicodeEncodeError ('ascii' codec can't encode character '\u2019' in position 0: ordinal not in range(128)) So the solution to this problem is to change the specified encoding (ascii) to (UTF-8) Try the code below: from time import ctime from threading import Thread import random class TimeClientHandler(Thread): def __init__(self, client): Thread.__init__(self) self.client = client def run(self): msgList = ["There are some idiots who always answer 'No' to every question, now tell me. Are you one of them?","There's nothing to fear. Except maybe that weird guy over there.","If I'm driving you crazy just remember to put on your seat belt.","I wondered why the baseball was getting bigger. Then it hit me.","You're Just Jealous Because The Voices Are Talking To Me.","Quickest way to get on your feet...miss a car payment.","Why do psychics ask your name?","I'm not opinionated. I'm just always right.","Sanity is the playground for the unimaginative.","It isn't homework unless it's due tomorrow."] msg = msgList[random.randint(0,len(msgList))] msge ="\n"+msg self.client.send(bytes(ctime() + msge,"UTF-8")) self.client.close()
Encoding with UTF-8. Turn to the folder Lib\site-packages in current used python interpreter, create a file called sitecustomize.py and add the following code in it: #coding=utf8 import sys reload(sys) sys.setdefaultencoding('utf8') Then restart the VS Code to see if the question goes away.
String.Strip is skipping a character the second for loop
For some reason after the second loop in my array the code is skipping a character for some reason. I think here is the problem: for word in range(int(len(ShortArray))): localString = LongArray[word] #print(word) if localString[:2] == ShortArray[word]: print(LongArray[word]) print(word) Here is the full code: kleuren = ["Rood","Geel","Groen","Blauw","Wit","Paars","Oranje","Zwart"] KleurenShort = [] def splitArray(string): for lenght in range(int(len(string) / 2)): KleurenShort.append(string[:2]) print(KleurenShort) string = string.strip(string[:2]) return KleurenShort def tekst_naar_kleur(string): return 0 def matchFirst2Letters(ShortArray,LongArray): for word in range(int(len(ShortArray))): localString = LongArray[word] #print(word) if localString[:2] == ShortArray[word]: print(LongArray[word]) print(word) matchFirst2Letters(splitArray("RoGeGrBl"),kleuren) The outcome is: ['Ro'] ['Ro', 'Ge'] ['Ro', 'Ge', 'rB'] ['Ro', 'Ge', 'rB', 'l'] when it should be: ['Ro'] ['Ro', 'Ge'] ['Ro', 'Ge', 'Gr'] ['Ro', 'Ge', 'Gr', 'Bl']
The problem is the use of the string.strip() method. 'aaaaaabcdb'.strip('ab') gives 'cd' as every instance of 'a' and 'b' in your input string is removed. You can simply get rid of the first two letters of the input string by indexing: 'abcde'[2:] will give 'cde'. Implemented in your code the corrected version is: kleuren = ["Rood","Geel","Groen","Blauw","Wit","Paars","Oranje","Zwart"] KleurenShort = [] def splitArray(string): for lenght in range(int(len(string) / 2)): KleurenShort.append(string[:2]) print(KleurenShort) string = string[2:] return KleurenShort def tekst_naar_kleur(string): return 0 def matchFirst2Letters(ShortArray,LongArray): for word in range(int(len(ShortArray))): localString = LongArray[word] #print(word) if localString[:2] == ShortArray[word]: print(LongArray[word]) print(word) matchFirst2Letters(splitArray("RoGeGrBl"),kleuren) which outputs ['Ro'] ['Ro', 'Ge'] ['Ro', 'Ge', 'Gr'] ['Ro', 'Ge', 'Gr', 'Bl'] Rood 0 Geel 1 Groen 2 Blauw 3 With the answer from the comment linked below, your splitArray function simply becomes: def splitArray(string): return [string[i:i+2] for i in range(0, len(string), 2)]
python preg_replace translate message to gsm formatted message
i am trying to to make a replace in order to send a gsm message later. i use the function below where it takes the message as parameter and convert it to a gsm formatted message. import re #staticmethod def gsm_message(message): expressions = { '/[άΆαΑ]/u': 'A', '/[βΒ]/u': 'B', '/[έΈεΕ]/u': 'E', ......... more } translated_message = re.sub(expressions.keys(), expressions.values(), message) print(translated_message) the error i get when i am trying to print is: unhashable type: 'list'. what can i do to make it work?
The following works in Python3. import re def gsm_message(message): expressions = { 'ά': 'A', 'Ά': 'A', 'α': 'A', 'Α': 'A', 'β': 'B', 'Β': 'B', 'έ': 'E', 'Έ': 'E', 'ε': 'E', 'Ε': 'E', } pattern = re.compile('|'.join(expressions.keys())) translated_message = pattern.sub(lambda x: expressions[x.group()], message) print(translated_message) message = "ββααέ" gsm_message(message) Gives: BBAAE
Python : Apply distributive law to elements in list
I want to do below in python2.7 . It works in case of 2 subelements but I can have multiple subelements. NOT = "not" OR = "or" AND = "and" def convertMain(prop) : if isinstance(prop, str) : answer = prop else : op = prop[0] #tree1 = convertIntoCNF(prop[1]) #tree2 = convertIntoCNF(prop[2]) """{ assert: tree1 and tree2 are in cnf }""" if op == AND : answer = [AND] + [convertIntoCNF(item) for item in prop[1:]] else : # op == OR if (len(prop) == 3) : tree1 = convertIntoCNF(prop[1]) tree2 = convertIntoCNF(prop[2]) answer = distOr2(tree1, tree2) return answer def distOr2(p1,p2): if isinstance(p1, list) and p1[0] == AND : #{ assert: p1 = P11 & P12 } answer = [AND, distOr2(p1[1],p2), distOr2(p1[2],p2)] elif isinstance(p2, list) and p2[0] == AND : #{ assert: p2 = P21 & P22 } answer = [AND, distOr2(p1,p2[1]), distOr2(p1,p2[2])] else : #{ assert: since p1 and p2 are both in cnf, then both are disjunctive clauses, which can be appended } answer = [OR, p1, p2] return answer The above code works for below: Input : ['and', ['or', ['and', '-P', 'Q'], 'R'], ['or', '-P', '-R']] Output : ['and', ['and', ['or', '-P', 'R'], ['or', 'Q', 'R']], ['or', '-P', '-R']] Explanation: Input is expression ((-P V Q) V R) ^ (-P V -R)) Output is expression ((-P V R) ^ (Q V R)) ^ (-P V -R) I want to make this work for any number of subelements, like in below example 'S' is third element in input so ['or', 'S', 'R'] should be added in output: Input : ['and', ['or', ['and', '-P', 'Q', 'S'], 'R'], ['or', '-P', '-R']] Output : ['and', ['and', ['or', '-P', 'R'], ['or', 'Q', 'R'], ['or', 'S', 'R']], ['or', '-P', '-R']] Thanks.
You can create a method that recursively converts anything with more than two subelements into a form where every list has two subelements (i.e. where every logical connective only has 2 arguments). For example: def makeBinary(prop): if isinstance(prop, str): return prop elif len(prop) == 3: return [prop[0], makeBinary(prop[1]), makeBinary(prop[2])] else: return [prop[0], makeBinary(prop[1]), makeBinary([prop[0]] + prop[2:])] Then you can call this on any proposition before running it through the code you already have, and your code can safely assume that no connective will have more than two arguments.
read a multi tier csv file in python
I need to read the following data out of a text file; [L02] g,g,g,g,g,g,g,g,g,g,w,w,w,w,g,g g,g,g,g,g,g,g,g,g,w,w,w,w,w,g,g g,g,g,g,g,g,g,g,w,w,w,w,w,g,g,g g,g,g,g,g,g,g,g,w,w,w,w,g,g,g,g g,g,g,g,g,g,g,g,g,w,w,w,w,g,g,g g,g,g,g,g,g,g,g,g,g,w,w,w,w,g,g g,g,g,g,g,g,g,g,g,g,g,w,w,w,g,g g,g,g,g,g,g,g,g,g,g,g,w,w,g,g,g g,g,g,g,g,g,g,g,g,g,g,w,w,g,g,g g,g,g,g,g,g,g,g,g,g,w,w,w,g,g,g g,g,g,g,g,g,g,g,g,w,w,w,g,g,g,g g,g,g,g,g,g,g,g,w,w,w,w,g,g,g,g g,g,g,g,g,g,g,w,w,w,w,g,g,g,g,g g,g,g,g,g,g,g,w,w,w,g,g,g,g,g,g g,g,g,g,g,g,w,w,w,w,w,g,g,g,g,g g,g,g,g,g,g,g,w,w,w,w,g,g,g,g,g [L01] d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d I can read a single block as a csv file but I don't know how to read each file as a separate list The output I want is to have arrays/lists for each block with the block contents as the list elements. Any ideas?
Here's a script that demonstrates how to break down the problem into reusable steps (functions) and performs the transformation your need. import itertools import operator import re import csv import pprint class TaggedLine(str): """ Override str to allow a tag to be added. """ def __new__(cls, val, tag): return str.__new__(cls, val) def __init__(self, val, tag): super(TaggedLine, self).__init__(val) self.tag = tag def sections(stream): """ Tag each line of the stream with its [section] (or None) """ section_pattern = re.compile('\[(.*)\]') section = None for line in stream: matcher = section_pattern.match(line) if matcher: section = matcher.group(1) continue yield TaggedLine(line, section) def splitter(stream): """ Group each stream into sections """ return itertools.groupby(sections(stream), operator.attrgetter('tag')) def parsed_sections(stream): for section, lines in splitter(stream): yield section, list(csv.reader(lines)) if __name__ == '__main__': with open('data.csv') as stream: for section, data in parsed_sections(stream): print 'section', section pprint.pprint(data[:2]) Save your file as 'data.csv' and the script will run on your data with this output: section L02 [['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'w', 'w', 'w', 'w', 'g', 'g'], ['g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'g', 'w', 'w', 'w', 'w', 'w', 'g', 'g']] section L01 [['d', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd'], ['d', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd', 'd']]
If you have numpy, you could read the file into a numpy array. comments='[' tells np.genfromtxt to ignore lines that begin with [. The reshape method places each 16x16 block in its own "layer". import numpy as np arr=np.genfromtxt('data.csv',comments='[',delimiter=',',dtype=None) arr=arr.reshape(-1,16,16) You can access the nth layer with arr[n].