I'm working on this project where I check a finished tic tac toe game. The user inputs a combination of x's and o's and periods (max of 3) in each row (there are 3 rows). I have to check if either x or o took too many turns. This is what I have:
if ttt.count(x) > 5:
print("x took too many turns")
This is just for x. When I run the program and I test to see if this statement comes up for x, and nothing comes up. What am i doing wrong?
Here is a sample run:
0 - check a finished tic-tac-toe
1 - check a finished sudoku
2 - check a game of connect four
3 - quit
choose an option: 0
option 0
For each row, start with x. Enter a combination of x'sand o's up to three characters. For a blank space,enter a period '.'.
top row:xox
middle row:xox
bottom row:xox
['xox', 'xox', 'xox']
0 - check a finished tic-tac-toe
1 - check a finished sudoku
2 - check a game of connect four
3 - quit
choose an option:
This is a bigger portion of the code:
for x in i:
if x not in valid_symbols:
print("invalid board - invalid symbol " + x )
done = True
break
if ttt.count(x) > 5:
print("x took too many turns")
if I insert an invalid symbol, then it prints that statement.
It appears that you misunderstand what the contents of ttt are. Open up the interpreter and try the following:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> tic_tac_toe = []
>>> tic_tac_toe.append(input('Top row: '))
Top row: xxx
>>>
>>> tic_tac_toe
['xxx']
>>> tic_tac_toe.count('x')
0
>>> tic_tac_toe.count('xxx')
1
What happens when you .append('xox')? What does the list look like?
Related
I am trying to remove everythin but letters, numbers and ! ? . ; , # ' from my python pandas column text.
I have already read some other questions on the topic, but still can not make mine work.
Here is an example of what I am doing:
import pandas as pd
df = pd.DataFrame({'id':[1,2,3,4],
'text':['hey+ guys! wuzup',
'hello p3ople!What\'s up?',
'hey, how- thing == do##n',
'my name is bond, james b0nd']}
)
Then we have the following table:
id text
1 hey+ guys! wuzup
2 hello p3ople!What\'s up?
3 hey, how- thing == do##n
4 my name is bond, james b0nd
Now, tryng to remove everything but letters, numbers and ! ? . ; , # '
First try:
df.loc[:,'text'] = df['text'].str.replace(r"^(?!(([a-zA-z]|[\!\?\.\;\,\#\'\"]|\d))+)$",' ',regex=True)
output
id text
1 hey+ guys! wuzup
2 hello p3ople!What's up?
3 hey, how- thing == do##n
4 my name is bond, james b0nd
Second try
df.loc[:,'text'] = df['text'].str.replace(r"(?i)\b(?:(([a-zA-Z\!\?\.\;\,\#\'\"\:\d])))",' ',regex=True)
output
id text
1 ey+ uys uzup
2 ello 3ople hat p
3 ey ow- hing == o##
4 y ame s ond ames 0nd
Third try
df.loc[:,'text'] = df['text'].str.replace(r'(?i)(?<!\w)(?:[a-zA-Z\!\?\.\;\,\#\'\"\:\d])',' ',regex=True)
output
id text
1 ey+ uys! uzup
2 ello 3ople! hat' p?
3 ey, ow- hing == o##
4 y ame s ond, ames 0nd
Afterwars, I also tried using re.sub() function using the same regex patterns, but still did not manage to have the expected the result. Being this expected result as follows:
id text
1 hey guys! wuzup
2 hello p3ople!What's up?
3 hey, how- thing don
4 my name is bond, james b0nd
Can anyone help me with that?
Links that I have seen over the topic:
Is there a way to remove everything except characters, numbers and '-' from a string
How do check if a text column in my dataframe, contains a list of possible patterns, allowing mistyping?
removing newlines from messy strings in pandas dataframe cells?
https://stackabuse.com/using-regex-for-text-manipulation-in-python/
Is this what you are looking for?
df.text.str.replace("(?i)[^0-9a-z!?.;,#' -]",'')
Out:
0 hey guys! wuzup
1 hello p3ople!What's up?
2 hey, how- thing don
3 my name is bond, james b0nd
Name: text, dtype: object
My if statement is not working:
choice = float(input("Enter 1 to enter sec. Enter 2 to enter mins")):
#My problem:
if 'choice' == 1
a = float(input("Enter secs"))
etc...
BTW, I was making a timer, but I need to know... It told me invalid syntax...
Fixed code (with my comments):
choice = int(raw_input("Enter 1 to enter sec. Enter 2 to enter mins")) # no need to use float here, also in python2 please use raw_input
if choice == 1: # you tried to compare `choice` string with 1 (always False), compare choice variable value with 1 instead. Also note, that you missed : after if
a = float(input("Enter secs"))
I figured it out!
choice + int(input("Bla bla bla")):
I forgot the ':'
I noticed that while you are inputting emojis in your phone message some of them take 1 character and some of them are taking 2. For example, "♊" takes 1 char but "😁" takes 2. In python, I'm trying to get length of emojis and I'm getting:
len("♊") # 3
len("😁") # 4
len(unicode("♊", "utf-8")) # 1 OH IT WORKS!
len(unicode("😁", "utf-8")) # 1 Oh wait, no it doesn't.
Any ideas?
This site has emojis length in Character.charCount() row: http://www.fileformat.info/info/unicode/char/1F601/index.htm
Read sys.maxunicode:
An integer giving the value of the largest Unicode code point, i.e.
1114111 (0x10FFFF in hexadecimal).
Changed in version 3.3: Before PEP 393, sys.maxunicode used to
be either 0xFFFF or 0x10FFFF, depending on the configuration
option that specified whether Unicode characters were stored as
UCS-2 or UCS-4.
The following script should work in both Python versions 2 an 3:
# coding=utf-8
from __future__ import print_function
import sys, platform, unicodedata
print( platform.python_version(), 'maxunicode', hex(sys.maxunicode))
tab = '\t'
unistr = u'\u264a \U0001f601' ### unistr = u'♊ 😁'
print ( len(unistr), tab, unistr, tab, repr( unistr))
for char in unistr:
print (len(char), tab, char, tab, repr(char), tab,
unicodedata.category(char), tab, unicodedata.name(char,'private use'))
Output shows consequence of different sys.maxunicode property value. For instance, the 😁 character (unicode codepoint 0x1f601 above the Basic Multilingual Plane) is converted to corresponding surrogate pair (codepoints u'\ud83d' and u'\ude01') if sys.maxunicode results to 0xFFFF:
PS D:\PShell> [System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8
PS D:\PShell> . py -3 D:\test\Python\Py\42783173.py
3.5.1 maxunicode 0x10ffff
3 ♊ 😁 '♊ 😁'
1 ♊ '♊' So GEMINI
1 ' ' Zs SPACE
1 😁 '😁' So GRINNING FACE WITH SMILING EYES
PS D:\PShell> . py -2 D:\test\Python\Py\42783173.py
2.7.12 maxunicode 0xffff
4 ♊ 😁 u'\u264a \U0001f601'
1 ♊ u'\u264a' So GEMINI
1 u' ' Zs SPACE
1 �� u'\ud83d' Cs private use
1 �� u'\ude01' Cs private use
Note: above output examples were taken from Unicode-aware Powershell-ISE console pane.
I am new to python. I tried the example given in here http://docs.bokeh.org/en/latest/docs/gallery/color_scatter.html with my own dataset, which looks like this
Unnamed: 0 fans id stars
0 0 69 18kPq7GPye-YQ3LyKyAZPw 4.14
1 1 1345 rpOyqD_893cqmDAtJLbdog 3.67
2 2 105 4U9kSBLuBDU391x6bxU-YA 3.68
3 3 2 fHtTaujcyKvXglE33Z5yIw 4.64
4 4 5 SIBCL7HBkrP4llolm4SC2A 3.80
here's my code:
import pandas as pd
from bokeh.plotting import figure, show, output_file
op = pd.read_csv('FansStars.csv')
x = op.stars
y = op.fans
radii = 1.5
colors = ["#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)]
TOOLS="hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,redo,reset,tap,save,box_select,poly_select,lasso_select,"
p = figure(tools=TOOLS)
p.scatter(x, y, radius=radii,
fill_color=colors, fill_alpha=0.6,
line_color=None)
output_file("color_scatter.html", title="color_scatter.py example")
show(p)
However, when I run this code, I get no error and a webpage is opened, but BLANK. On reloading several times, I can finally see the tools, but that's all.
Can anyone tell me where am I going wrong?
Thanks!
I cant replicate this on Python 3.4 with Bokeh 0.12.3. So in that way, your code seems fine. I tried it both in the notebook (output_notebook) and to a file like you do and both seem to work fine.
The radius of 1.5 which you specify is taken in data units (x apparently), this makes the circles extremely big, covering the entire screen at first render. But using the wheelzoom to zoom out a bit reveals all circles as expected. Here is what your code looks like in Firefox for me (after zooming out):
Problem: I have a large number of scanned documents that are linked to the wrong records in a database. Each image has the correct ID on it somewhere that says where it belongs in the db.
I.E. A DB row could be:
| user_id | img_id | img_loc |
| 1 | 1 | /img.jpg|
img.jpg would have the user_id (1) on the image somewhere.
Method/Solution: Loop through the database. Pull the image text in to a variable with OCR and check if user_id is found anywhere in the variable. If not, flag the record/image in a log, if so do nothing and move on.
My example is simple, in the real world I have a guarantee that user_id wouldn't accidentally show up on the wrong form (it is of a specific format that has its own significance)
Right now it is working. However, it is incredibly strict. If you've worked with OCR you understand how fickle it can be. Sometimes a 7 = 1 or a 9 = 7, etc. The result is a large number of false positives. Especially among images with low quality scans.
I've addressed some of the image quality issues with some processing on my side - increase image size, adjust the black/white threshold and had satisfying results. I'd like to add the ability for the prog to recognize, for example, that "81*7*23103" is not very far from "81*9*23103"
The only way I know how to do that is to check for strings >= to the length of what I'm looking for. Calculate the distance between each character, calc an average and give it a limit on what is a good average.
Some examples:
Ex 1
81723103 - Looking for this
81923103 - Found this
--------
00200000 - distances between characters
0 + 0 + 2 + 0 + 0 + 0 + 0 + 0 = 2
2/8 = .25 (pretty good match. 0 = perfect)
Ex 2
81723103 - Looking
81158988 - Found
--------
00635885 - distances
0 + 0 + 6 + 3 + 5 + 8 + 8 + 5 = 35
35/8 = 4.375 (Not a very good match. 9 = worst)
This way I can tell it "Flag the bottom 30% only" and dump anything with an average distance > 6.
I figure I'm reinventing the wheel and wanted to share this for feedback. I see a huge increase in run time and a performance hit doing all these string operations over what I'm currently doing.