python - List comprehensions with multiple conditions -
i know subject has been touched on few times already, topics inspected did not ask question ask (i think). wrong declaration below?
from string import ascii_letters, digits letters, digits combinations = [letter1 + letter2 + digit1 + digit2 digit1 in digits if (digit1 % 2 == 0) digit2 in digits if (digit2 % 2 == 0) letter1 in letters if (letter1 != 'a') letter2 in letters if (letter2 != 'a')] print combinations[:500]
i keep on getting:
typeerror: not arguments converted during string formatting
would great know doing wrong, since swear following proper syntax here...
i see few problems.
first of all, import doesn't think does:
>>> string import ascii_letters, digits letters, digits >>> letters '0123456789' >>> digits '0123456789'
try instead?
>>> string import ascii_letters letters, digits digits >>> letters 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' >>> digits '0123456789'
secondly, when use %
operator on number, it's mod, when use on string, it's format
operator. you're using on string, hence mumble string formatting. if want mod operator, have int() thing you're using on.
int(digit1) % 2
thirdly, agree others not easy read/understand. writing differently, using loop or range or functional notation, or @ least formatting differently make structure more apparent, improvement.
hope helps.
Comments
Post a Comment