python checking crosswords from left to right - python-2.7
how do i make element of list join together and check if the elements make one of some specific words ? and remove every element that does not make any words and replacing them with a blank space. I am a beginner so go easy on me.
appreciate the help.
My Code:
def looking_word_left_to_right(the_list):
for any_words in the_valid_words:
for every in the_list:
for letters_s in every:
index_list=0
the valid words : aardvark
albatross
alligator
armadillo
barracuda
camel
caribou
cat
cattle
chamois
chicken
cod
deer
dinosaur
dog
dogfish
dotterel
dragonfly
eel
ferret
finch
fish
flamingo
fly
goldfinch
dragon
otter
ox
deer
the lists = ['e', 'l', 'l', 'e', 'z', 'a', 'g', 'c', 'h', 'i', 'c', 'k', 'e', 'n', 'b', 'n', 'd', 'o']
['d', 'e', 't', 'b', 'a', 'l', 'l', 'i', 'g', 'a', 't', 'o', 'r', 'a', 'a', 'a', 'o', 'l']
['m', 'e', 'z', 'n', 'a', 'c', 'a', 't', 't', 'l', 'e', 'e', 'b', 'l', 'r', 'c', 't', 'a']
['c', 'f', 'e', 'n', 'a', 'r', 'c', 'e', 's', 's', 's', 'a', 'd', 'l', 'r', 'i', 't', 'f']
['h', 'f', 't', 'r', 'a', 'r', 'r', 's', 's', 'o', 's', 'a', 'o', 'i', 'a', 'r', 'e', 'f']
['a', 'a', 'r', 'e', 'p', 'p', 'o', 'h', 's', 's', 'a', 'r', 'g', 'h', 'c', 'e', 'r', 'u']
['m', 'r', 'h', 'e', 'i', 'r', 'm', 'm', 'w', 'i', 'l', 'd', 'f', 'c', 'u', 'm', 'e', 'b']
['o', 'i', 'm', 'l', 't', 'a', 'a', 'i', 'r', 'f', 'o', 'v', 'i', 'n', 'd', 'a', 'l', 'b']
['i', 'g', 'l', 'a', 'l', 'o', 'b', 'e', 'h', 'o', 'e', 'a', 's', 'i', 'a', 'x', 'u', 'e']
['s', 'a', 'b', 'l', 'd', 'i', 'n', 'b', 'b', 'c', 'c', 'r', 'h', 'h', 'r', 't', 'y', 'l']
['r', 'l', 'a', 'h', 's', 'i', 'f', 'd', 'l', 'o', 'g', 'k', 'r', 'c', 't', 'l', 'a', 'i']
['a', 'r', 'e', 'o', 'g', 'o', 'l', 'd', 'f', 'i', 'n', 'c', 'h', 'e', 'f', 'n', 'n', 'd']
['d', 'm', 'n', 'e', 'e', 'p', 'o', 'l', 'e', 't', 'n', 'a', 'r', 'n', 't', 'a', 'd', 'o']
['d', 'i', 'n', 'o', 's', 'a', 'u', 'r', 'o', 'x', 'o', 'f', 'o', 'e', 'c', 'l', 'i', 'c']
['t', 'n', 'a', 'h', 'p', 'e', 'l', 'e', 'p', 'a', 'l', 'g', 'a', 'i', 'e', 'r', 'h', 'o']
['h', 'c', 'a', 'o', 'r', 'k', 'c', 'o', 'c', 'y', 'a', 't', 'r', 'm', 'o', 'a', 'c', 'r']
['f', 'l', 'a', 'm', 'i', 'n', 'g', 'o', 'e', 'r', 'e', 'f', 'a', 'f', 'l', 'o', 'e', 'c']
['h', 'a', 't', 'e', 'e', 'h', 'c', 'h', 'd', 'r', 'a', 'c', 'a', 'r', 'i', 'b', 'o', 'u']
my_str="ellezagchickenbndodetballigatoraaaolmeznacattleeblrctacfenarcesssadlritfhftrarrssosaoiarefaareppohssarghcerumrheirmmwildfcumeboimltaairfovindalbiglalobehoeasiaxuesabldinbbccrhhrtylrlahsifdlogkrctlaiareogoldfinchefnnddmneepoletnarntadodinosauroxofoeclictnahpelepalgaierhohcaorkcocyatrmoacrflamingoerefafloechateehchdracaribou"
def create_2d_list(N):
output_list=[]
counter=0
for row in range(0,N):
temp=[]
for col in range(0,N):
temp.append(my_str[counter])#you can add a charcter instead of counter
counter=counter+1
output_list.append(temp[:])
return output_list
N=18
x=create_2d_list(N)
for row in range(0,N):
total=0
s="|"
for col in range(0,N):
my_str="{0:2} ".format(x[row][col])
s=s+my_str+"|"
print "-"*(N*4+1)
print s,
print " "
the_valid_words=open("E:/asd/words.txt","r").readlines()
def list_duplicates_of(seq,item):
start_at = -1
locs = []
while True:
try:
loc = seq.index(item,start_at+1)
except ValueError:
break
else:
locs.append(loc)
start_at = loc
return locs
def looking_word_left_to_right(the_list):
for any_words in the_valid_words:
for every in the_list:
for letters_s in every:
index_list=0
Converting your rows to strings makes it easier to find your target words. the_list needs to be a list of lists for this to work.
row_letters = [''.join(r) for r in the_lists]
words = 'aardvark albatross alligator armadillo barracuda camel caribou cat '
'cattle chamois chicken cod deer dinosaur dog dogfish dotterel '
'dragonfly eel ferret finch fish flamingo fly goldfinch dragon otter '
'ox deer'.split()
for i, row in enumerate(row_letters[:]):
for word in words:
if word in row:
wi = row.find(word)
row_letters[i] = ' '*wi + word + ' '*(len(row) - wi - len(word))
if ' ' not in row_letters[i]: #works if word size < row size
row_letters[i] = ' '*len(row)
>>> row_letters
[' chicken ', ' alligator ', ' cattle ',
' ', ' ', ' ',
' ', ' ', ' ',
' ', ' ', ' goldfinch ',
' ', ' ox ', ' ',
' ', 'flamingo ', ' caribou']
>>> new_the_list = [list(r) for r in row_letters] # puts back to list format
Related
Why my RegexTokenizer transformation in PySpark gives me the opposite of the required pattern?
When I use the RegexTokenizer from pyspark.ml.feature to tokenize sentences column in my dataframe to find all the word characters, I get the opposite of what I would get when the python re package is used for the same sentence. Here is the sample code: from pyspark.sql import SparkSession from pyspark.ml.feature import RegexTokenizer spark = SparkSession.builder \ .master("local") \ .appName("Word list") \ .getOrCreate() df = spark.createDataFrame(data = [["Hi there, I have a question about RegexTokenizer, Could you please help me..."]], schema = ["Sentence"]) regexTokenizer = RegexTokenizer(inputCol="Sentence", outputCol="letters", pattern="\\w") df = regexTokenizer.transform(df) df.first()['letters'] This gives the following output: [' ', ', ', ' ', ' ', ' ', ' ', ' ', ', ', ' ', ' ', ' ', ' ', '...'] On the other hand if I use the re module on the same sentence and use the same pattern to match the letters, using this code here: import re sentence = "Hi there, I have a question about RegexTokenizer, could you please help me..." letters_list = re.findall("\\w", sentence) print(letters_list) I get the desired output as per the regular expression pattern as: ['H', 'i', 't', 'h', 'e', 'r', 'e', 'I', 'h', 'a', 'v', 'e', 'a', 'q', 'u', 'e', 's', 't', 'i', 'o', 'n', 'a', 'b', 'o', 'u', 't', 'R', 'e', 'g', 'e', 'x', 'T', 'o', 'k', 'e', 'n', 'i', 'z', 'e', 'r', 'c', 'o', 'u', 'l', 'd', 'y', 'o', 'u', 'p', 'l', 'e', 'a', 's', 'e', 'h', 'e', 'l', 'p', 'm', 'e'] I also found that I need to use \W instead of \w in pySpark to solve this problem. Why is this difference? Or have I misunderstood the usage of pattern argument in RegexTokenizer?
From what the documentation on RegexTokenizer says, on creation it has a parameter called gaps. In one mode, the regexp matches gaps (true and is the default), in other it matches tokens (not the gaps, false). Try setting it manually to the value you need: in your case, gaps = false.
Using 'sorted' function for words gives an output with letters being split and sorted
I'm new to python (2.7) and stackoverflow. I'm trying to learn how to use the 'sorted' function. When I use the 'sorted' function, the sentence splits into individual letters and sorts those letters in ascending order. But that is not what I want. I want to sort my words in ascending order. I'm trying to run this code peace = "This is one of the most useful sentences in the whole wide world." def pinkan (one): return sorted (one) print pinkan (peace) But the output I get is something of this sort: [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'T', 'c', 'd', 'd', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'f', 'f' , 'h', 'h', 'h', 'h', 'i', 'i', 'i', 'i', 'l', 'l', 'l', 'm', 'n', 'n', 'n', 'n', 'o', 'o', 'o', 'o', 'o', 'r', 's', 's', 's', 's', 's ', 's', 't', 't', 't', 't', 'u', 'u', 'w', 'w', 'w'] I would appreciate any help/suggestion. Thanks :-)
You should be using first split() to generate a list of words, then sort(), to sort that list alphabetically in ascending order: peace = "This is one of the most useful sentences in the whole wide world." terms = peace.split() terms.sort(key=str.lower) output = " ".join(terms) print(output) ['in', 'is', 'most', 'of', 'one', 'sentences', 'the', 'the', 'This', 'useful', 'whole', 'wide', 'world.']
How do i remove all the letters in a list if it repeats n number of times. where n is a number between 1-10
This is the list. list1 =['F', 'L', 'Y', 'W', 'B', 'E', 'G', 'A', 'L', 'K', 'R', 'U', 'B', 'E', 'T', 'L', 'H', 'G', 'E', 'C', 'K', 'Y', 'U', 'B', 'H', 'L', 'U', 'G', 'A', 'F', 'K', 'Y', 'F', 'M', 'P', 'U', 'B', 'K', 'F', 'G', 'I', 'O', 'N', 'S', 'Y'] I want to delete the letters that repeat n numbers of time. In the context of this problem n is 4. This is what i have tried so far. n = 4 alphabet = ["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"] i = 0 for x in range(len(alphabet)-1): print(alphabet[i]) h = list1.count(alphabet[x]) print("h: ",h) if h == n: while alphabet[x] in alphabet: alphabet.remove(alphabet[x]) print(alphabet) I An getting an error saying that list.remove(x): x not in list
to delete the letters that repeat n numbers of time The solution using collections.Counter subclass: import collections n = 4 list1 =['F', 'L', 'Y', 'W', 'B', 'E', 'G', 'A', 'L', 'K', 'R', 'U', 'B', 'E', 'T', 'L', 'H', 'G', 'E', 'C', 'K', 'Y', 'U', 'B', 'H', 'L', 'U', 'G', 'A', 'F', 'K', 'Y', 'F', 'M', 'P', 'U', 'B', 'K', 'F', 'G', 'I', 'O', 'N', 'S', 'Y'] counts = collections.Counter(list1) list1 = [l for l in list1 if l in counts and counts[l] != n] print(list1) The output: ['W', 'E', 'A', 'R', 'E', 'T', 'H', 'E', 'C', 'H', 'A', 'M', 'P', 'I', 'O', 'N', 'S']
Not to propose whole new solution, you can modify your code to something like this: for x in alphabet: print(x) h = list1.count(x) print("h: ",h) if h == n: while x in alphabet: alphabet.remove(x) print(alphabet) The problem with your code is that you have a while loop that removes many elements from the alphabet if there is even only one letter that have h = 4 (the element and all forward letters). This is caused by: while alphabet[x] in alphabet: alphabet.remove(alphabet[x]) when you remove alphabet[x], next element becomes alphabet[x] (indexing must be continuous), so while loop removes one letter and all forward letters. But as you want to remove letters from list not alphabet you should modify: list1 =['F', 'L', 'Y', 'W', 'B', 'E', 'G', 'A', 'L', 'K', 'R', 'U', 'B', 'E', 'T', 'L', 'H', 'G', 'E', 'C', 'K', 'Y', 'U', 'B', 'H', 'L', 'U', 'G', 'A', 'F', 'K', 'Y', 'F', 'M', 'P', 'U', 'B', 'K', 'F', 'G', 'I', 'O', 'N', 'S', 'Y'] n = 4 alphabet = ["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"] i = 0 for x in alphabet: print(x) h = list1.count(x) print("h: ",h) if h == n: while x in list1: list1.remove(x) print(''.join(list1))
Too many parameters error, but called with correct amount
got a question about this error: TypeError: rot13() takes exactly 1 argument (2 given) Which occurs on this code: def get(self): <-- called on every get request ch = self.rot13("abc") def rot13(input): <-- fairly untested rot 13 ;) alpha = ['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'] escaped = escape(input) ciphered = "" for char in escaped: originalIndex = alpha.index(char) newIndex = (originalIndex + 13) % 26 ciphered = chipered + alpha[newIndex] Do not know why there is this error. I'm just handing one parameter there.
It seems that you're missing this: def rot13(self, input): … That's because rot13() appears to be a method inside a class, not a stand-alone function, so it needs to receive self.
Comparing lists - Homework Python
correct_ans = ['B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', \ 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'] here is my statement to import the list from txt file # import user answers into a list infile = open('testscores.txt', 'r') driver_ans = infile.readlines() infile.close() driver_ans = ['B', 'D', 'A', 'A', 'C', 'B', 'B', 'A', 'C', 'D', 'B', 'C', \ 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'] for index in range(0, 20): if driver_ans[index] == correct_ans[index]: total_correct += 1 else: wrong_ans.append(index + 1) This logic continues to return that all are wrong answers. This is not correct comparing visually my "correct_ans" list and my "driver_ans" list. What am I doing wrong?!
Only guessing. If testscores.txt has the content B D A A ... keep in mind, that driver_ans will be ['B\n', 'D\n', 'A\n', 'A\n', ... try maybe driver_ans = [x.strip ('\n') for x in infile.readlines()]
The readlines() function returns lines that include the trailing newline. So, try: driver_ans = [x.strip() for x in infile.readlines()]