This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
I'm making a function that identify if has two kings in a chess board
And i used an or operator but it just dosn't works. I "fix" this using if and elif but i don't get why it dosn't works with or
chessboard = {
"a1":"" ,"a2":"" ,"a3":"" ,"a4":"" ,"a5":"" ,"a6":"" ,"a7":"" ,"a8":"bking" ,
"b1":"" ,"b2":"" ,"b3":"" ,"b4":"" ,"b5":"" ,"b6":"" ,"b7":"" ,"b8":"" ,
"c1":"" ,"c2":"" ,"c3":"" ,"c4":"" ,"c5":"" ,"c6":"" ,"c7":"" ,"c8":"" ,
"d1":"" ,"d2":"" ,"d3":"" ,"d4":"" ,"d5":"" ,"d6":"" ,"d7":"" ,"d8":"" ,
"e1":"" ,"e2":"" ,"e3":"" ,"e4":"" ,"e5":"" ,"e6":"" ,"e7":"" ,"e8":"" ,
"f1":"" ,"f2":"" ,"f3":"" ,"f4":"" ,"f5":"" ,"f6":"" ,"f7":"" ,"f8":"" ,
"g1":"wking" ,"g2":"" ,"g3":"" ,"g4":"" ,"g5":"" ,"g6":"" ,"g7":"" ,"g8":"" ,
"h1":"" ,"h2":"" ,"h3":"" ,"h4":"" ,"h5":"" ,"h6":"" ,"h7":"" ,"h8":"" }
def kingschess(board):
kings = 0
print(board.values())
for a in board.values():
print(a)
if a == "bking" or "wking":
print("True")
kings += 2
isValidChessBoard(mychessboard)
you should actually use if a == "bking" or a == "wking": since the interpreter cannot infer that you are considering a for both statements
Related
This question already has answers here:
Why doesn't a python dict.update() return the object?
(11 answers)
python setdefault(key,set())).update(... returns None
(1 answer)
Why does dict(k=4, z=2).update(dict(l=1)) return None in Python? [duplicate]
(3 answers)
Closed last year.
Trying to sum to sets from 2 different txt files with very similar content.
examplet of content:
https://articulo.mercadolibre.com.ar/MLA-1113638999-placa-de-video-colorful-gt710-nf-1gb-ddr3-garantia-oficial-_JM;Placa De Video Colorful Gt710 Nf 1gb Ddr3 Garantia Oficial;gold_special;True;6999
https://articulo.mercadolibre.com.ar/MLA-795327882-placa-de-video-geforce-gt-710-1gb-ddr3-pc-gamer-gt710-dx12-_JM;Placa De Video Geforce Gt 710 1gb Ddr3 Pc Gamer Gt710 Dx12;gold_special;True;7499
I'm trying to do that with the following code (simplifaction of the original):
with open("gt-710.txt","r") as f:
a = f.readlines()
with open("gt-7102.txt","r") as f:
b = f.readlines()
c = set(a).update(set(b))
print(c)
The output i'm getting constantly is NONE, I tried printing each set without updating and they do so properly, but once I try to sume them up they return NONE.
update method of set returns None. So what you can do is use a instead of the output of that line;
a = set(a)
a.update(set(b))
Or better yet I would do
c = set(a) | set(b)
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I have a relatively stupid question which I am not able to formulate very well (and I think it explains why I am not finding any answer)
I would like to calculate the mean, minimum, and maximum of a panda series in my dataframe for many variable (let's say age and weight)
dataframe.age.min()
dataframe.age.max()
dataframe.age.mean()
dataframe.weight.min()
dataframe.weight.max()
dataframe.weight.mean()
I would like to create some kind of loop, which would do something like:
list = ['age','weight']
for x in list:
min-"x" = dataframe.x.min()
max-"x" = dataframe.x.max()
mean-"x" = dataframe.x.mean()
I would like to have variables called min-age, max-age, mean-age
I don't understand how to define a function, and how to insert in the name min-"x" the name of my variable (x)...
Use describe on you dataframe then manipulate the index.
dfd = df.describe().stack()
dfd.index = dfd.index.to_series().str.join('-')
count-age 10.000000
count-weight 10.000000
mean-age -0.200662
mean-weight 0.298352
std-age 1.175323
std-weight 0.901915
min-age -1.778043
min-weight -0.860798
25%-age -1.144173
25%-weight -0.488076
50%-age -0.092748
50%-weight 0.294160
75%-age 0.276348
75%-weight 0.892405
max-age 1.670823
max-weight 1.680473
dtype: float64
This question already has answers here:
How do I merge two dictionaries in a single expression in Python?
(43 answers)
Closed 7 years ago.
The question is
This is what I have so far:
dict(nafta_capitals) = canadian_capitals, mexican_capitals, us_capitals
Given three dictionaries, associated with the variables , canadian_capitals, mexican_capitals, and us_capitals, that map provinces or states to their respective capitals, create a new dictionary that combines these three dictionaries, and associate it with a variable , nafta_capitals.
You may need to use defaultdict-
Here nafta is used as key to the three ( canadian_capitals, mexican_capitals, us_capitals) as below-
>>>dic = defaultdict(list)
>>>lst = ['nafta1', 'canadian_capitals1', 'mexican_capitals1', 'us_capitals1', 'nafta2', 'canadian_capitals2', 'mexican_capitals2', 'us_capitals2']
>>>grouped_lst = [lst[i:i+4] for i in range(0,len(lst),4)]
>>>[['nafta1', 'canadian_capitals1', 'mexican_capitals1', 'us_capitals1'], ['nafta2', 'canadian_capitals2', 'mexican_capitals2', 'us_capitals2']]
>>>for i in grouped_lst:dic[i[0]]=i[1:]
>>>dic.items()
>>>[('nafta1', ['canadian_capitals1', 'mexican_capitals1', 'us_capitals1']), ('nafta2', ['canadian_capitals2', 'mexican_capitals2', 'us_capitals2'])]
>>>for i in dic.keys():print dic[i]
>>>['canadian_capitals1', 'mexican_capitals1', 'us_capitals1']
['canadian_capitals2', 'mexican_capitals2', 'us_capitals2']
This question already has answers here:
Alternatives for returning multiple values from a Python function [closed]
(14 answers)
Closed 8 years ago.
def ethos(file):
f = open(file)
raw = f.read()
token = nltk.word_tokenize(raw)
words_to_match = ['love' , 'good' , 'excellent' , 'perfect' , 'brilliant']
words_to_match2 = ['bad' , 'primitive' , 'struggle' , 'annoying' , 'problem' , 'time-consuming', 'fiddly']
positive_tokens = []
negative_tokens = []
for tokens in token:
if tokens in words_to_match:
positive_tokens.append(tokens)
and tokens in words_to_match2:
negative_tokens.append(tokens)
return negative_tokens
I wrote this code with an intention of returning two lists one positive and one negative, I cannot give two return statement, but I want two separate lists. And this program is showing syntax error in the 'and' statement, kindly help.
Change the last part of your program in the following way:
for tokens in token:
if tokens in words_to_match:
positive_tokens.append(tokens)
if tokens in words_to_match2:
negative_tokens.append(tokens)
return (positive_tokens, negative_tokens)
this will return a tuple with two elements. You use it as such:
(positive_tokens, negative_tokens) = ethos(yourfile)
I would do something like:
def ethos(file):
...
positive_tokens = [t for t in token if t in words_to_match]
negative_tokens = [t for t in token if t in words_to_match2]
return positive_tokens, negative_tokens
You can use this like:
positive, negative = ethos("somefile.txt")
See How do you return multiple values in Python? for a more advanced discussion on returning multiple values from functions
This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 8 years ago.
Quick question:
disc_list=[] #creating an empty list to put the discs into
rad = 70
for i in range(5):
disc_list.append(cmds.polyCylinder( name = 'disc' + str(i), radius = rad, height = 10)[0]) # create discs
cmds.move(0, 10*i, 0, ('disc' + str(i)))
rad = rad*0.7
print disc_list
anyone know why when I print the disc_list, this is returned:
[u'disc0', u'disc1', u'disc2', u'disc3', u'disc4']
where has the u come from?
The u simply denotes that it is a unicode string. You should not worry about it. When you print it, it will still be the same.
for i in disc_list:
print i
[OUTPUT]
disc0
disc1
disc2
disc3
disc4
Suggested link