The program adds "" to the string I'm indexing - python

Im using this piece of code
shares_one = [(None,), ('200 # 496',), ('200 # 486',)]
print(shares_one)
variable_one = StringVar(add_sale_quantity_selector)
variable_one.set(shares_one[1]) # default value
y = OptionMenu(add_sale_quantity_selector, variable_one, *shares_one)
y.place(x=25, y=40)
x = (variable_one.get())
bad_chars = ['"']
for i in bad_chars:
test_string = x.replace(i, '')
print (test_string)
index = shares_one.index(test_string)
print(index)
but it gives me the following error
ValueError: "('200 # 496',)" is not in list
for the line index = shares_one.index(test_string)
whats the possible solution, sorry if anythings out of the picture.

Related

Replacing each character/string in a list one at a time with python

I have a string "MQADKVMEPT" and the desired output I want is:
.QADKVMEPT
M.ADKVMEPT
MQ.DKVMEPT
MQA.KVMEPT
MQAD.VMEPT
MQADK.MEPT
MQADKV.EPT
MQADKVM.PT
MQADKVME.T
MQADKVMEP.
Using this code:
motif = 'MQADKVMEPT'
motiflist = list(motif)
pos = 0
for aa in motiflist:
motiflist[pos] = '.'
pos += 1
str = ''
for a in motiflist:
str += a
print(str)
My output is:
.QADKVMEPT
..ADKVMEPT
...DKVMEPT
....KVMEPT
.....VMEPT
......MEPT
.......EPT
........PT
.........T
..........
How do I reinitialize the original motiflist so that it doesn't give me this output?
"Quick" fix would be copy the original list. Using your code:
motif = "MQADKVMEPT"
motiflist = list(motif)
pos = 0
for aa in motiflist:
motiflist_copy = motiflist.copy() # <--- copy the original list
motiflist_copy[pos] = "."
pos += 1
s = ""
for a in motiflist_copy:
s += a
print(s)
Prints:
.QADKVMEPT
M.ADKVMEPT
MQ.DKVMEPT
MQA.KVMEPT
MQAD.VMEPT
MQADK.MEPT
MQADKV.EPT
MQADKVM.PT
MQADKVME.T
MQADKVMEP.
Shorter solution:
motif = "MQADKVMEPT"
for i in range(len(motif)):
s = motif[:i] + "." + motif[i + 1 :]
print(s)

Changing Image.Grab.grab(value) without retyping

A list helped me make a loop which grabs a part of the screen and compare it to a existing RGB value. Final code is in final edit. Full learnprocess is shown below.
I'm new to coding in general. I am using pixels do verify RGB value of a pixel which declares position of player. But in stead of re-typing location = ImageGrab.grab(position) can I change the position?
seat0 = (658,848,660,850) #btn
seat1 = (428,816,429,817) #co
seat2 = (406,700,407,701) #hj
seat3 = (546,653,547,654) #lj
seat4 = (798,705,799,706) #bb
seat5 = (769,816,770,817) #sb
btnpos = ImageGrab.grab(seat0)
position = btnpos.load()
btn = (251,0,8)
if position[0,0] == btn:
print('Button')
elif ????
So I want to change seat0 (in imagegrab) to seat1 without re-typing the code. Thanks for the help
EDIT1: So I tried Marks suggestion, not sure what I'm doing wrong.
positie = ["(658,848,660,850)","(428,816,430,818)","(406,700,408,702)","(546,653,548,655)","(798,705,799,706)","(769,816,770,817)"]
btn = (251,0,8)
btnpos = ImageGrab.grab(positie[0])
btncheck = btnpos.load()
btn [0,0] not in positie[0]
I get "ValueError: too many values to unpack (expected 4)"
I am trying to RGB value of positie[0] and it has to match btn. if it doesn't match to btn then it has to check positie[1], until it returns true.
Edit2:
Thanks so far Mark. I am so bad at loops.
positie = [(658,848,660,850),(428,816,430,818),(406,700,408,702),(546,653,548,655),(798,705,799,706),(769,816,770,817)]
btn = (251,0,8)
btnpos = ImageGrab.grab(positie[0])
btncheck = btnpos.load()
for x in positie:
#btncheck = btnpos.load()
if btncheck[0,0] ==btn:
print("positie is BTN")
else:
btnpos = (positie[++1])
print(btnpos)
how do I change btnpos with adding up instead of re-typing everything?
So it's supposed to grab first item in list and compare it to btn. If it's correct it can break, if it's wrong it has to compare btn with second item in list.
If true it gives me the position. If false:
Now it prints list[1] six times.
Thank you for your patience
FINAL EDIT: Thank you Mark.
positie = [(658,848,660,850),(428,816,430,818),(406,700,408,702),(546,653,548,655),(798,705,799,706),(769,816,770,817)]
btn = (251,0,8)
counter = 0
max_index = len(positie) - 1
while counter <= max_index:
btnpos = ImageGrab.grab(positie[counter])
btncheck = btnpos.load()
if btncheck[0,0] == btn:
print("Positie")
break
else:
print("controleert de volgende")
counter = counter + 1

NLTK output changes when using the same input on the same machine

The next piece of code returns different output with the same input(self.SynSets)
Why can it be happening? Am I doing something wrong? or is it caused by python?
def FilterSynSets(self):
self.filteredSysNets = {}
for synset in self.SysNets:
for subsynset in self.SysNets:
length = wn.path_similarity(synset,subsynset)
if not length is None\
and length !=1\
and length >0:
target = synset.__str__().replace("'","")
source =subsynset.__str__().replace("'","")
connection="\"{}\"->\"{}\" [label={}]".format(
target,
source,
str(round(length,3)))
self.filteredSysNets[connection] = length
oldLength = len(self.filteredSysNets)
avarageVal = sum(self.filteredSysNets.values())/len(self.filteredSysNets)
self.filteredSysNets = {k: v for k,v in self.filteredSysNets.items() if v>=avarageVal}
newLength = len(self.filteredSysNets)
prt = newLength/oldLength*100
print ("avr -{}\n old -{}\n new - {}\n prec={}".format(avarageVal,oldLength,newLength,prt))
return self
Outputs:
http://screencast.com/t/eFMOROfkPXR
http://screencast.com/t/Fdd6ufhA

Append column field name with date range operator dynamically in django?

This is the code I'm using :
where_con=''
#loop on model name
# getting all info for one model
where_con = {}
for k in model_k_j:
type_val = type(model_k_j[k])
if type_val== dict:
print "dictonary type"
"""
for model_field_dict in model_k_j[k]:
start= model_k_j[k][model_field_dict]
end= model_k_j[k][model_field_dict]
where_con[k] = medical_home_last_visit__range=[start,end ]
break
"""
else:
col_name.append(k)
where_con[k] = model_k_j[k]
# covert data type
# **where_con {unpack tuple}
# where_con =str(where_con)
# print where_con
qs_new = model_obj.objects.filter(**where_con)
The field medical_home_last_visit is not static, it is coming dynamically.
How do I append it ? I have tried something like:
colname_variable = medical_home_last_visit
where_con[k] = colname_variable + __range=[start,end ]
but it is not working properly, and gives this error :
where_con[k] = colname_variable + __range=[start,end ]
^
SyntaxError: invalid syntax
where_con is dict and key name should be equal colname_variable__range:
#k = 'medical_home_last_visit__range'
where_con[k] = (start, end)
qs_new = model_obj.objects.filter(**where_con)
it is equal to:
model_obj.objects.filter(medical_home_last_visit__range=(start, end))
and any other filter args should be keys in where_con, for example:
#k = 'some_date__lte'
where_con[k] = datetime.datetime.now()

Python and PyQt string can't print

So, I'm using Python with PyQt and I have a very strange problem. A string that prints OK at one point doesn't print OK after a few lines of code! Here's my code:
name = str(self.lineEdit.text().toUtf8())
self.let_change = Search()
name_no_ind = self.let_change.indentation(name)
print(name_no_ind)
name_cap = self.let_change.capital(name)
name_low = self.let_change.lower(name)
print(name_no_ind, name_cap, name_low)
col = self.combobox.currentIndex()
row = 0
for i in range(0, self.tableWidget.rowCount()):
try:
find_no_ind = self.let_change.indentation(self.tableWidget.item(row, col).text())
find_cap = self.let_change.capital(self.tableWidget.item(row, col).text())
find_lower = self.let_change.lower(self.tableWidget.item(row, col).text())
if name_no_ind or name_cap or name_low in find_no_ind or find_cap or find_lower:
self.tableWidget.setItemSelected(self.tableWidget.item(row, col), True)
print("Item found in %d, %d" % (row,col))
row += 1
except AttributeError:
row += 1
And here's what I get:
Αντωνης
('\xce\x91\xce\xbd\xcf\x84\xcf\x89\xce\xbd\xce\xb7\xcf\x82', '\xce\x91\xce\x9d\xce\xa4\xcf\x8e\xce\x9d\xce\x97\xce\xa3', '\xce\xb1\xce\xbd\xcf\x84\xcf\x8e\xce\xbd\xce\xb7\xcf\x82')
Item found in 0, 0
Isn't that strange? It prints OK and then it doesn't. Does anybody know what can I do?
P.S.: Here are the functions:
# -*- coding: utf-8 -*-
class Search():
#A function that removes indentations:
def indentation(self, name):
a = name
b = ["ά", "Ά", "ή", "Ή", "ώ", "Ώ", "έ", "Έ", "ύ", "Ύ", "ί", "Ί", "ό", "Ό"]
c = ['α', 'Α', 'η', 'Η', 'ω', 'Ω', 'ε', 'Ε', 'υ', 'Υ', 'ι', 'Ι', 'ο', 'Ο']
for i in b:
a = a.replace(i, c[b.index(i)])
return a
# A function that makes letters capital:
def capital(self, name):
a = name
greek_small = ["α", "β", "γ", "δ", "ε", "ζ", "η", "θ", "ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π", "ρ", "σ", "τ", "υ", "φ", "χ", "ψ", "ω", "ς"]
greek_capital = ["Α", "Β", "Γ", "Δ", "Ε", "Ζ", "Η", "Θ", "Ι", "Κ", "Λ", "Μ", "Ν", "Ξ", "Ο", "Π", "Ρ", "Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω", "Σ"]
for i in greek_small:
a = a.replace(i, greek_capital[greek_small.index(i)])
return a
#A function that makes letters lower:
def lower(self, name):
a = name
greek_small = ["α", "β", "γ", "δ", "ε", "ζ", "η", "θ", "ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π", "ρ", "σ", "τ", "υ", "φ", "χ", "ψ", "ω", "ς"]
greek_capital = ["Α", "Β", "Γ", "Δ", "Ε", "Ζ", "Η", "Θ", "Ι", "Κ", "Λ", "Μ", "Ν", "Ξ", "Ο", "Π", "Ρ", "Σ", "Τ", "Υ", "Φ", "Χ", "Ψ", "Ω", "Σ"]
for i in greek_capital:
a = a.replace(i, greek_small[greek_capital.index(i)])
return a
Basically, it capitalizes or lowers Greek characters...
SOLUTION!!!:
Steve solved the initial problem and based on what he said, I came up with this that solves everything:
name = str(self.lineEdit.text().toUtf8())
self.let_change = Search()
name_no_ind = self.let_change.indentation(name)
name_cap = self.let_change.capital(name)
name_low = self.let_change.lower(name)
name_list = [name, name_no_ind, name_cap, name_low]
col = self.combobox.currentIndex()
row = 0
for i in range(0, self.tableWidget.rowCount()):
try:
item_ = str(self.tableWidget.item(row, col).text().toUtf8())
find_no_ind = self.let_change.indentation(item_)
find_cap = self.let_change.capital(item_)
find_lower = self.let_change.lower(item_)
item_list = [find_no_ind, find_cap, find_lower]
for x in name_list:
for y in item_list:
if x in y:
self.tableWidget.setItemSelected(self.tableWidget.item(row, col), True)
row += 1
except AttributeError:
row += 1
I would say that one r both of self.let_change.capital(name) or self.let_change.lower(name) is overwriting it by using the name of the input parameter or possibly changing the encoding. Since you have not posted the code for them I can not be sure.
Sorry, they are not the problem. The problem is that you are printing them differently:
>>> print(capital(name))
ΑΝΤΩΝΗΣ
>>> print(capital(name), name)
('\xce\x91\xce\x9d\xce\xa4\xce\xa9\xce\x9d\xce\x97\xce\xa3', '\xce\x91\xce\xbd\xcf\x84\xcf\x89\xce\xbd\xce\xb7\xcf\x82')
>>> print(capital(name))
ΑΝΤΩΝΗΣ
>>> print(name, name)
('\xce\x91\xce\xbd\xcf\x84\xcf\x89\xce\xbd\xce\xb7\xcf\x82', '\xce\x91\xce\xbd\xcf\x84\xcf\x89\xce\xbd\xce\xb7\xcf\x82')
>>> print(name,)
('\xce\x91\xce\xbd\xcf\x84\xcf\x89\xce\xbd\xce\xb7\xcf\x82',)
>>> print(name)
Αντωνης
>>> print("%s = %s" % (name, capital(name)))
Αντωνης = ΑΝΤΩΝΗΣ
>>>
So you either need separate print statements or the use of a format string.

Categories