【问题标题】:Variables in python aren't their value inside a for looppython中的变量不是它们在for循环中的值
【发布时间】:2019-12-20 11:14:13
【问题描述】:

我正在制作一个函数,该函数使用 bin() 函数将整数转换为二进制数,然后删除“0b”,将其添加到输出嵌套列表中,输出应如下所示:

[['0','0','0','0','0','0','0','0'],['0','0','0','0','0','0','0','1'],...] 8 位字符串列表

但是,当添加前导零(见下文)的 for 循环运行时,我得到 128 个前导零。我检查了前导零值,它是正确的。我必须改变什么才能让它输出它应该输出的东西?

谢谢

def convert_to_binary(num_list,func_list):
    ...
    for i in range(len(num_list) - 1):
        leading_zeros = 8 - len(func_list[i])
        #print(leading_zeros)
        for j in range(leading_zeros):
            func_list[j].insert(0,'0')
    print(func_list)

【问题讨论】:

  • 我怀疑问题在于您如何创建原始 func_list 参数。显示你是如何调用函数的。
  • 我的猜测是func_list 的所有元素都是对同一个列表的引用。
  • 为什么你迭代num_list的长度,然后从func_list[i]获取值?这两个输入有什么关系?为什么要从长度中减去 1?

标签: python list function binary nested-lists


【解决方案1】:

更新

我已经根据评论更新了代码:

'该函数的预期目标是让你给它一个整数列表和另一个函数将填充的空列表。'

def convert_to_binary(num_list, func_list):
    for i in range(len(num_list)):
        # Collects the binary string removing '0b'
        binary_string = bin(num_list[i])[2:] 

        # Break the string into a char array (for loop shorthand)
        temp = [char for char in binary_string] 

        # Add the binary array to func_list
        func_list.append(temp) 

        # Pad with 0's        
        leading_zeros = 8 - len(temp)
        for j in range(leading_zeros):
            func_list[i].insert(0,'0')
    print(func_list)

if __name__ == '__main__':
    num_list = range(0, 10)
    func_list = []
    convert_to_binary(num_list, func_list)

原创

我不太确定在输入之前 ... 中发生了什么 - 或者在 func_list 的状态下,但是我已经做出了这些假设:

num_list = int 数字列表(例如 1 - 10)

func_list = 二进制字符串列表:其中func_list[i] = bin(num_list[i]) 去掉了字符串前面的 0b。

在上述条件下 - 以下将满足您的要求:

def convert_to_binary(num_list, func_list):
    for i in range(len(num_list)):
        leading_zeros = 8 - len(func_list[i])

        # Build an array of the char's in the func_list index
        # can be done in shorthand with temp = [char for char in func_list[i][k]]
        temp = []
        for k in range(len(func_list[i])):
            temp.append(func_list[i][k])

        # Replace the binary string with binary char array at func_list index
        # e.g. '1' becomes ['1']
        func_list[i] = temp

        # Add 0's to the front of the char array 
        for j in range(leading_zeros):
            func_list[i].insert(0,'0')
    print(func_list)

if __name__ == '__main__':
    num_list = range(0, 10)

    # Create an array of Binary strings 
    # (for each value in num_list - removing '0b' from the front)
    func_list = [bin(i)[2:]for i in num_list] 

    convert_to_binary(num_list, func_list)

输出:

[['0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0', '0', '1'], ['0', '0', '0', '0', '0', '0', '1', '0'], ['0', '0', '0', '0', '0', '0', '1', '1'], ['0', '0', '0', '0', '0', '1', '0', '0'], ['0', '0', '0', '0', '0', '1', '0', '1'], ['0', '0', '0', '0', '0', '1', '1', '0'], ['0', '0', '0', '0', '0', '1', '1', '1'], ['0', '0', '0', '0', '1', '0', '0', '0'], ['0', '0', '0', '0', '1', '0', '0', '1']]

【讨论】:

  • 谢谢。有两件事,该函数的预期目标是让你给它一个整数列表和另一个函数将填充的空列表。另外,if 语句中的func_list 等于什么。那里发生了什么?谢谢!
  • 好的 - 我可以根据新信息更改我的答案。您希望清楚的代码行是什么?我很乐意解释:D
  • @ForestCayden 我已经用 cmets 更新了我的原始帖子(希望能从评论中回答你的问题),并且我已经添加了另一个解决方案,给出了新的细节。希望这是您正在寻找的:D
猜你喜欢
  • 2014-01-21
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
  • 2020-03-11
  • 1970-01-01
  • 2013-06-09
相关资源
最近更新 更多