简体   繁体   English

Python列表切片范围

[英]Python list slicing range

I am a new Python learner. 我是一名新的Python学习者。 I was stopped by some codes like below: 我被下面的一些代码拦住了:

return ['FizzBuzz'[i%-3&-4:i%-5&8^12]or`i`for i in range(1,n+1)]

Especially what does it mean: [i%-3&-4:i%-5&8^12] 特别是什么意思: [i%-3&-4:i%-5&8^12]

Thank you so much! 非常感谢!

: is the range symbol, meaning give me the list elements between start and end in list[start:end] . :是范围符号,意思是给我list[start:end] start和end之间的列表元素。

% is the modulo operator, & is the binary (bit-wise) and operator, so the first part: %是模运算符, &是二进制(按位)和运算符,因此第一部分:

i%-3&-4

is equal to mod(i, -3) & -4 等于mod(i, -3) & -4

^ is the exclusive or operator, so the second part: ^是互斥或运算符,因此第二部分:

i%-5&8^12

is equal to mod(i, -5 ) & 8^12 等于mod(i, -5 ) & 8^12

The modulo operator yields the remainder from the division of the first argument by the second. 模运算符从第一个参数除以第二个参数得到余数。

If you need to know more about python binary (bit-wise) operators look here 如果您需要进一步了解python二进制(按位)运算符,请查看此处

As stated in the comments, without the back-tics `` , the last part after the or statement returns a range from 1 to n+1 , if the resulting fist list is empty. 如评论中所述,不带反斜线``,如果结果拳头列表为空,则or语句之后的最后一部分返回1 to n+1的范围。

The back ticks, in that statement is Python2 syntax for the repr() function. 该语句中的反勾号是repr()函数的Python2语法。 See repr documentation for more info 有关更多信息,请参见repr文档

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

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