[英]Some of my dataframe values include dictionaries, while others contain values. How do I remove the dictionaries?
[英]Output of my python list / array is repeating the values. How do I remove this?
我希望我的代码将 x 结果存储在列表中。 但是,它重复了 x 的值。 我该如何解决?
list = [1, 3 , 2, 0, 4, 0, 12]
non_zeros = [list for list in list if list != 0]
set_x = []
num_x = 0
for i, (prev_coeff, next_coeff) in enumerate(zip(non_zeros, non_zeros[1:])):
x = (next_coeff*3) / (prev_coeff*2)
print(f"x{i + 1} = {x}")
num_x += 1
for j in range(num_x):
set_x.append(x)
print(set_x)
我的结果是:
x1 = 4.5
x2 = 1.0
x3 = 3.0
x4 = 4.5
[4.5, 1.0, 1.0, 3.0, 3.0, 3.0, 4.5, 4.5, 4.5, 4.5]
如何将列表设置为:
[4.5, 1.0, 3.0, 4.5]
你需要调整你的缩进
list1 = [1, 3 , 2, 0, 4, 0, 12]
non_zeros = [t for t in list1 if t != 0]
set_x = []
num_x = 0
for i, (prev_coeff, next_coeff) in enumerate(zip(non_zeros, non_zeros[1:])):
x = (next_coeff*3) / (prev_coeff*2)
print(f"x{i + 1} = {x}")
num_x += 1
for j in range(num_x): #<--- indent this outside of your loop
set_x.append(x)
print(set_x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.