python 中的变量不是它们在 for 循环中的值
Variables in python aren't their value inside a for loop
我正在制作一个函数,该函数使用 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)
更新
我已经根据评论更新了代码:
'the intended goal of the function was to make it so you gave it a list of integers and another empty list that the function would fill.'
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
= 整数列表(例如 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']]
我正在制作一个函数,该函数使用 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)
更新
我已经根据评论更新了代码:
'the intended goal of the function was to make it so you gave it a list of integers and another empty list that the function would fill.'
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
= 整数列表(例如 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']]