I ran into a problem when adding variables. Their values are glued together, but I need to calculate
a = check1()
b = check2()
c = check3()
d = check4()
g = check5()
e = check6()
f = check7()
g = сheck8()
i = сheck9()
online = a + b + c + d + g + e + f + g + i
functions are all identical:
def check1():
with valve.source.a2s.ServerQuerier(a) as server:
info = server.info()
players = server.players()
return '{player_count}'.format(**info)
I get as a result: 007220000
how to count them?
It would be of great help if you pasted the whole code segment here, including the functions. It seems to me that you are returning the values from those functions as strings.
Related
What I need to do:
for each key in dictionary1 extract key1:value1 from dictionary1 and key1:value1 from dictionary2
assign those 2 pairs to 4 different variables
use those variables in other methods
move on to the next iteration (extract key2:value2 of both dictionaries 1 and 2, and assign to the same 4 variables)
Example:
d_one = {1:z, 2:x, 3:y}
d_two = {9:o, 8:n, 7:m}
the result has to be
a = 1
b = z
c = 9
d = o
(calling some other methods using those variables here)
(moving on to the next iteration)
a = 2
b = x
c = 8
d = n
(and so on)
My brain is overloaded on this one. Since I can't nest for loops to accomplish this task, I guess the correct usage of 'and' statement should do it? I have no idea how so I try to split it up...
d_one = {'1':'z', '2':'x', '3':'y'}
d_two = {'9':'o', '8':'n', '7':'m'}
for i in range(0, len(d_one)):
for a in list(d_one.keys())[i]:
a = d_one.keys()[i]
b = d_one[a]
for c in list(d_two.keys())[i]:
c = d_two.keys()[i]
d = d_two[c]
print(a, b, c, d)
output:
TypeError: 'dict_keys' object is not subscriptable
Try this:
d_one = {'1':'z', '2':'x', '3':'y'}
d_two = {'9':'o', '8':'n', '7':'m'}
for (a,b), (c,d) in zip(d_one.items(), d_two.items()):
print(a, b, c, d)
I have this section of code:
if storagetypecheck1 == "Virtual":
def storagecheck():
d = 1
dforid = str(1-d)
while d <= int(numcount):
storageidprefix = "specimen" + "[" + dforid + "]"
driver.find_element_by_id(storageidprefix + ".storageContainerForSpecimen").click()
pag.press('a')
pag.press('enter')
time.sleep(1)
d = d + 1
storagecheck()
storagecheck()
When the storage type of a webform is set to virtual, it will run and change the type to auto in the textboxes.
The problem is that it has to do so with multiple textboxes which follow the format specimen[x].storageContainerForSpecimen.
However, when I run this code, it just loops over and over without changing the value of d to 2, 3, etc.
I tried having d = 1 above the if statement, but then it says for line dforid = str(1-d) that d is not defined.
Where should I put the d = 1 expression so that it is able to be recognized by the storagecheck() loop while also being able to increase by 1 every loop?
storagecheck() calls itself recursively. Each time it calls itself, the line d = 1 is being executed, so the value of d is being reset. You need to place d outside of the function definition for d to continue incrementing, like so:
if storagetypecheck1 == "Virtual":
d = 1
def storagecheck():
global d
dforid = str(1-d)
while d <= int(numcount):
storageidprefix = "specimen" + "[" + dforid + "]"
driver.find_element_by_id(storageidprefix + ".storageContainerForSpecimen").click()
pag.press('a')
pag.press('enter')
time.sleep(1)
d = d + 1
storagecheck()
storagecheck()
We use the keyword global to introduce the variable d into the namespace of the function.
So i currently have the following replaces in Python
f = f.replace("</ ", "</")
f = f.replace("<? Xml", "<?xml")
f = f.replace("<String", "<string")
f = f.replace("</String", "</string")
f = f.replace("<Resources", "<resources")
f = f.replace("</Resources", "</resources")
This is because there is some malformed parts in my XML file.
Is there a better, more clean way to write this part?
For a list of tuples (a, b) where you want to replace a with b
def rep_many(s, l):
for a, b in l:
s = s.replace(a, b)
return s
I am a beginner programmer and I am doing a task for school. The task is to assign 4 constant variables and then use a code to work out the value. Each value has a corresponding letter and the program is asking the user to type in 5 numbers then the program will return the word. The code is the following:
array = [["L","N"], #define the 2d array, L=Letters, N=Numbers
["-","-"]] #line for space
a = 2#define the variables
b = 1
c = 7
d = 4
e = (a*b)+b#calcualtions
f = c+b
g = (d/a)-b
h = c*a
i = a+b+d
j = c-a
k = c-d*f
l = c+a
m = (c*a)-b
n = a*d
o = a+d-b
p = (c*d)-a*(b+d)
q = a*(c+(d-b))
r = (d*d)-b
s = r-f-g
array.append(["e",e])
array.append(["f",f])
array.append(["g",g])#append all the calculations
array.append(["h",h])
array.append(["i",i])
array.append(["j",j])
array.append(["k",k])
array.append(["l",l])
array.append(["m",m])
array.append(["n",n])
array.append(["o",o])
array.append(["p",p])
array.append(["q",q])
array.append(["r",r])
array.append(["s",s])
def answer():
len_row = len(array)
number_input = int(input("Enter number: "))
for i in range(len_row):
if number_input == (array[i][1]):
return array[i][0]
break
one_let = answer()
two_let = answer()
thr_let = answer()
fou_let = answer()
fiv_let = answer()
print(one_let,two_let,thr_let,fou_let,fiv_let)
The numbers that I am meant to put in are 6, 18,, 7, 8, and 3.
The word that prints is "spife" and the word that is meant to be printed is "spine". The problem is that there are two letters that have a variable of 8 and Python gets the first one only. Is there a way to print out the two seperate words but first with the first variable in a 2D array and second with the second 2D array? i.e spife then spine
Thank you for your help ahead, I am just a beginner! :)
Yes you can do it but is a bit tricky the secret is to use itertools.product on the list of letters that could have each of the five values.
First you need to use a better data structure such as a dict, (in this case a collection.defaltdict) to hold the letters that have some value. You can do this way:
import collections
import itertools
a = 2#define the variables
b = 1
c = 7
d = 4
e = (a*b)+b#calcualtions
f = c+b
g = (d/a)-b
h = c*a
i = a+b+d
j = c-a
k = c-d*f
l = c+a
m = (c*a)-b
n = a*d
o = a+d-b
p = (c*d)-a*(b+d)
q = a*(c+(d-b))
r = (d*d)-b
s = r-f-g
dat = collections.defaultdict(list)
for c in "abcdefghijklmnopqrs":
dat[eval(c)].append(c)
Now in dat you have a list of letters that match some number, for example
print(dat[6])
print(dat[18])
print(dat[7])
print(dat[8])
print(dat[3])
Outputs:
['s']
['p']
['i']
['f', 'n']
['e']
OK, then you need to change answerto return a list of letters, and collect the user input:
def answer():
number_input = int(input("Enter number: "))
return dat[number_input]
letts = [answer() for _ in range(5)] #collect five answers of the user
And the final magic is done here:
for s in map(lambda x: "".join(x),itertools.product(*letts)):
print(s)
Now if you are confused then study:
collections
collections.defaultdict
itertools
itertools.product
str.join
I have two strings like:
a = '54515923333558964'
b = '48596478923333558964'
Now the longest postfix match is
c = '923333558964'
what will be a solution using re?
Here is a solution I found for prefix match:
import re
pattern = re.compile("(?P<mt>\S*)\S*\s+(?P=mt)")
a = '923333221486456'
b = '923333221486234567'
c = pattern.match(a + ' ' + b).group('mt')
Try the difflib.SequenceMatcher:
import difflib
a = '54515923333558964'
b = '48596478923333558964'
s = difflib.SequenceMatcher(None, a, b)
m = s.find_longest_match(0, len(a), 0, len(b))
print a[m.a:m.a+m.size]
You can use this variation of the regex pattern:
\S*?(?P<mt>\S*)\s+\S*(?P=mt)$
EDIT.
Note, however, that this may require O(n3) time with some inputs. Try e.g.
a = 1000 * 'a'
b = 1000 * 'a' + 'b'
This takes one second to process on my system.