简体   繁体   English

列出具有多个条件的理解

[英]List comprehensions with multiple conditions

I know this subject has been touched on a few times already, but all the topics I inspected did not ask the question I am about to ask (I think). 我知道这个主题已经被触及过几次,但是我检查过的所有主题都没有提出我要问的问题(我认为)。 What is wrong with declaration below? 下面的声明有什么问题?

from string import ascii_letters, digits as letters, digits

combinations = [letter1 + letter2 + digit1 + digit2 for digit1 in digits if (digit1 % 2 == 0) for digit2 in digits if (digit2 % 2 == 0) for letter1 in letters if (letter1 != 'a') for letter2 in letters if (letter2 != 'A')]

print combinations[:500]

I keep on getting: 我不断得到:

TypeError: not all arguments converted during string formatting

Would be great to know what I am doing wrong, since I could swear I am following proper syntax here... 很高兴知道我在做什么错,因为我发誓我在这里遵循正确的语法...

I see a few problems. 我看到一些问题。

First of all, your import doesn't do what you think it does: 首先,导入不会执行您认为的操作:

>>> from string import ascii_letters, digits as letters, digits
>>> letters
'0123456789'
>>> digits
'0123456789'

Try something like this instead? 试试这样的东西吗?

>>> from string import ascii_letters as letters, digits as digits
>>> letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> digits
'0123456789'

Secondly, when you use the % operator on a number, it's mod, but when you use it on a string, it's the format operator. 其次,当对数字使用%运算符时,它是mod,但是当对字符串使用%运算符时,它是format运算符。 You're using it on a string, hence the mumble about string formatting. 您正在字符串上使用它,因此对字符串格式一无所知。 If you want it to be the mod operator, you have to int() the thing you're using it on. 如果希望它成为mod运算符,则必须int()使用它的东西。

int(digit1) % 2

Thirdly, I agree with the others who say this is not easy to read/understand. 第三,我同意其他人的观点,他们说这不容易阅读/理解。 Writing it differently, using a loop or range or functional notation, or at least formatting it differently to make the structure more apparent, would be an improvement. 使用循环或范围或功能符号以不同的方式编写它,或者至少以不同的方式设置格式以使结构更明显,将是一种改进。

Hope this helps. 希望这可以帮助。

You're trying to do integer mod on a string containing a digit. 您正在尝试对包含数字的字符串进行整数模运算。 I guess wrap digitFOO % 2 == 0 like int(digitFOO) % 2 == 0 . digitFOO % 2 == 0 int(digitFOO) % 2 == 0一样包装digitFOO % 2 == 0 int(digitFOO) % 2 == 0

As Chris_Rands points out, it still won't work properly because the imports are broken. 正如Chris_Rands指出的那样,由于导入已损坏,它仍然无法正常工作。

The problem here is that the digits you import is a string, and so each digit1 and digit2 is also a (single-character) string. 这里的问题是您导入的digits是一个字符串,因此每个digit1digit2也是一个(单字符)字符串。 This makes (for example) digit1 % 2 appear to be an attempt to use Python's string interpolation mechanism - it's a % operator with a string left-hand operand. 例如,这使得digit1 % 2似乎是尝试使用Python的字符串插值机制-它是带有字符串左操作数的%运算符。

Because that operand contains no % signs, there is no marker to locate the insertion of the right-hand operand, 2 , hence the rather incomprehensible error message. 因为该操作数不包含%符号,所以没有标记来定位右侧操作数2的插入,因此是相当难以理解的错误消息。

You should also lay your code out rather better - long single lines don't work! 您还应该将代码布置得更好-长单行不起作用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM