python中的for循环和in运算符

简历

我试图理解用 python 编写的现有代码。我目前正在学习python。有人可以帮我理解这段代码吗?

bits_list = split_string_into_chunks(coding, n)
# take first bit as the sign, and the remaining bits as integers
signs_nums = [(-1 if bits[0] == '0' else 1, int(bits[1:], 2)) 
                  for bits in bits_list]
# use modulo to ensure that the numbers fall within the require interval:
#   -2.048 ≤ x ≤ 2.048
x = [sign * (num % 2.048) for sign, num in signs_nums]
瑞克凯格斯

bits_list = split_string_into_chunks(编码,n)

这行代码调用了一个函数 split_string_into_chunks,有 2 个参数,你没有显示它们是什么。bits_list 是看起来像数据帧列表或字典对象的返回值

sign_nums = [(-1 if bits[0] == '0' else 1, int(bits[1:], 2)) 对于 bits_list 中的位]

方括号的使用告诉我这就是所谓的列表理解。为此,我总是从行尾开始。

for bits in bits_list - This part of the line says I have a list of values and the for loop will process each element of the list via the variable 'bits'.

-1 if bits[0] == '0' - This if statement is a little backwards. what is saying is I will return the number -1 if the first value of bits is equal to 0. From this statement, it apprears that bits is actually a value pair listing which means that bits_list is probably a python dict object.

else 1 - this is the else part of the if above if statement. So if the value of bits[0] is not equal to 0 then I will return 1.

int(bits[1:], 2) - This part is interesting as it converts the bits[1:] to binary.

sign_nums - this is the returned list of binary values based 

x = [sign * (num % 2.048) for sign, num insigns_nums]

这再次使用列表理解。有了这个结构,我发现从右边开始向左移动更容易。所以分解它;

sign_nums - is a python dictionary or two-dimensional array object which the for loop will loop over.
num - is an individual value from the sign_nums dictionary object.
sign - is the second element from the sings_num dictionary that is associated with num.
The for loop will pull out individual value pair items from signs_nums.

sign * (num % 2.048) the first part in brackets takes the modulus of num divided by 2.048 and then multiplies that by whatever is in sign
x - this is the returned list from the line of code, which happens to be the answer to the sum sign * (num % 2.048).

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python中的循环置换运算符

来自分类Dev

带有“ for”循环和“ if”语句的Python“ in”运算符

来自分类Dev

使用“ for”循环和使用星号运算符(*)解包在Python中打印元组

来自分类Dev

与 rest 运算符和循环混淆

来自分类Dev

按位运算符和for循环

来自分类Dev

循环和范围运算符的基准

来自分类Dev

for循环和模运算符

来自分类Dev

while循环和算术运算符

来自分类Dev

Python中赋值运算符和复合运算符之间的区别

来自分类Dev

比较运算符和'is'-Python中的运算符优先级?

来自分类Dev

python中的运算符

来自分类Dev

Python for()循环与数学运算符

来自分类Linux

缓存的整数,Python 3.7中的is运算符和id()

来自分类Python

为什么Python中没有++和-运算符?

来自分类Python

Python中递增和递减运算符的行为

来自分类Python

Python中!=和<>运算符之间有区别吗?

来自分类Dev

python中.find()和'in'运算符的区别

来自分类Dev

Python中的索引运算符和列表

来自分类Dev

变量python中的逻辑运算符'or'和'and'

来自分类Dev

Firebird 和 Python 中的多个 AND 运算符

来自分类Python

在Python中使用AND和NOT运算符

来自分类Java

Elasticsearch查询中的OR和AND运算符

来自分类Java

在Java中equals()和运算符“ ==”

来自分类Dev

:=运算符和Golang中的if语句

来自分类Java

读“?” 和Java中的“:”运算符

来自分类Dev

PostgreSQL中的'!='和'<>'运算符

来自分类Dev

在Dart中重载++和+ =运算符

来自分类Dev

解析查询中的和运算符

来自分类Dev

R中的赋值运算符:“ <-”和“ <<-”