Python 需要根据平滑逻辑从字符串中查找缺少的值
我得到了一个带有数字和“”符号(缺少值)的字符串,我必须替换“”符号,如下所述:Python 需要根据平滑逻辑从字符串中查找缺少的值,python,curve-fitting,missing-data,smoothing,Python,Curve Fitting,Missing Data,Smoothing,我得到了一个带有数字和“”符号(缺少值)的字符串,我必须替换“”符号,如下所述: Input1: "_,_,_,24" Output1: 6,6,6,6 Input2: "40,_,_,_,60" Output2: 20,20,20,20,20 Input4: "_,_,30,_,_,_,50,_,_" Output4: 10,10,12,12,12,12,4,4,4 我尝试过使用basic for循环,if-then-else使用2个轴心点,但当输入字符串发生变化时,所有这些方法都受到了挑战
Input1: "_,_,_,24"
Output1: 6,6,6,6
Input2: "40,_,_,_,60"
Output2: 20,20,20,20,20
Input4: "_,_,30,_,_,_,50,_,_"
Output4: 10,10,12,12,12,12,4,4,4
我尝试过使用basic for循环,if-then-else使用2个轴心点,但当输入字符串发生变化时,所有这些方法都受到了挑战。我发现设计一个通用系统有点挑战性。我不确定python中是否有任何特定的库可以用来完成这项工作。欢迎以任何形式的伪代码提出建议。尝试以下代码:
output='10,35,67400'
output=output.split(',')
新的_输出=[]
对于输出中的i:
如果我!='':
新的_输出追加(i)
尝试以下代码:
output='10,35,67400'
output=output.split(',')
新的_输出=[]
对于输出中的i:
如果我!='':
新的_输出追加(i)
当然可以增强,但这可以实现以下目的:
string = "_,_,30,_,_,_,50,_,_"
output = string.split(',')
pos = 0
next_value = 0
last_pos = 0
last_value = 0
while pos < len(output):
if output[pos] != '_' or (pos + 1 == len(output)):
if output[pos] != '_':
next_value = int(output[pos])
else:
next_value = 0
new_value = (next_value + last_value) / (pos - last_pos + 1)
for i in range(last_pos, pos + 1):
output[i] = new_value
last_value = new_value
last_pos = pos
pos += 1
print(output)
string=“u,u,30,u,u,u,50,u,uu”
输出=字符串。拆分(',')
pos=0
下一个值=0
最后位置=0
最后一个值=0
当pos
这将生成一个浮点数组:[10.0,10.0,12.0,12.0,12.0,12.0,12.0,4.0,4.0,4.0]
额外信息:
- 您必须通过迭代数组来找到一个非缺失值
- 找到一个后,将其添加到找到的最后一个非缺失值(0)中 否则)并设置 里程碑(包括里程碑本身)
- 当你到达数组的末尾时,别忘了做同样的事情。当前值变为0,将其添加到上一个值,然后再次共享
\uu30、\u50、\u50、\u50、\u30、\u50、
首先我们找到30个。我们在开始和当前位置之间共享它
我们得到:10,10,10,,,,,50,,
然后我们找到50个。前面的数值是10。因此,我们在10和50(即5个单元格)的pos之间共享60
我们得到:10,10,12,12,12,12,12,uu,u
我们到达阵列的末端
0+12=12->我们在当前位置和最后12个位置(即3个单元格)之间共享它
我们得到
10,10,12,12,12,12,4,4,4
确实可以增强,但这可以做到:
string = "_,_,30,_,_,_,50,_,_"
output = string.split(',')
pos = 0
next_value = 0
last_pos = 0
last_value = 0
while pos < len(output):
if output[pos] != '_' or (pos + 1 == len(output)):
if output[pos] != '_':
next_value = int(output[pos])
else:
next_value = 0
new_value = (next_value + last_value) / (pos - last_pos + 1)
for i in range(last_pos, pos + 1):
output[i] = new_value
last_value = new_value
last_pos = pos
pos += 1
print(output)
def curve_smoothing(string):
S=string.split(',') #Splitting the string, storing it in new variable
index=0 #initialising index variable to track current index
while index<len(S)-1:
if S[index] =='_': #Handling the case where first element is '_'
for i in range(index,len(S)):
if S[i]!='_': #when first number traced
S[index:i+1]=[int(S[i])//(i-index+1) for x in range(index,i+1)]
index=i
break
else: #If string only contains '_' , return 0
S[index:len(S)]=[0 for x in range(len(S))]
else: #When first number is not '_'
if S[index+1]!='_': #If numbers found at consecutive position, iterate index by 1
index=index+1
for i in range(index+1,len(S)): #Handling the case when there are '_' between two numbers
if S[i]!='_':
S[index:i+1]=[(int(S[index])+int(S[i]))//(i-index+1) for x in range(index,i+1)]
index=i
break
else: #If the only number present in list is at 0th index and rest elements are '_'
S[index:len(S)]=[int(S[index])//(len(S)-index) for x in range(index,len(S))]
return S
S= "_,_,_,_,50"
smoothed_values= curve_smoothing(S)
print(smoothed_values)
string=“u,u,30,u,u,u,50,u,uu”
输出=字符串。拆分(',')
pos=0
下一个值=0
最后位置=0
最后一个值=0
当pos
这将生成一个浮点数组:[10.0,10.0,12.0,12.0,12.0,12.0,12.0,4.0,4.0,4.0]
额外信息:
- 您必须通过迭代数组来找到一个非缺失值
- 找到一个后,将其添加到找到的最后一个非缺失值(0)中 否则)并设置 里程碑(包括里程碑本身)
- 当你到达数组的末尾时,别忘了做同样的事情。当前值变为0,将其添加到上一个值,然后再次共享
\uu30、\u50、\u50、\u50、\u30、\u50、
首先我们找到30个。我们在开始和当前位置之间共享它
我们得到:10,10,10,,,,,50,,
然后我们找到50个。前面的数值是10。因此,我们在10和50(即5个单元格)的pos之间共享60
我们得到:10,10,12,12,12,12,12,uu,u
我们到达阵列的末端
0+12=12->我们在当前位置和最后12个位置(即3个单元格)之间共享它
我们得到了10,10,12,12,12,12,4,4,4
def曲线平滑(字符串):
def curve_smoothing(string):
S=string.split(',') #Splitting the string, storing it in new variable
index=0 #initialising index variable to track current index
while index<len(S)-1:
if S[index] =='_': #Handling the case where first element is '_'
for i in range(index,len(S)):
if S[i]!='_': #when first number traced
S[index:i+1]=[int(S[i])//(i-index+1) for x in range(index,i+1)]
index=i
break
else: #If string only contains '_' , return 0
S[index:len(S)]=[0 for x in range(len(S))]
else: #When first number is not '_'
if S[index+1]!='_': #If numbers found at consecutive position, iterate index by 1
index=index+1
for i in range(index+1,len(S)): #Handling the case when there are '_' between two numbers
if S[i]!='_':
S[index:i+1]=[(int(S[index])+int(S[i]))//(i-index+1) for x in range(index,i+1)]
index=i
break
else: #If the only number present in list is at 0th index and rest elements are '_'
S[index:len(S)]=[int(S[index])//(len(S)-index) for x in range(index,len(S))]
return S
S= "_,_,_,_,50"
smoothed_values= curve_smoothing(S)
print(smoothed_values)
S=string.split(',')#拆分字符串,将其存储在新变量中
index=0#初始化索引变量以跟踪当前索引
索引def曲线平滑时(字符串):
S=string.split(',')#拆分字符串,将其存储在新变量中
index=0#初始化索引变量以跟踪当前索引
而indexHi Jon..谢谢你的代码。但是我的要求有点不同。让我解释一下。无论在哪里,我们都缺少价值,我们必须添加进程和下一个可用的值,并用缺失值的数量除以它。所以你只需要用平均值填充空白空间?嗨,乔恩。谢谢你的代码。但是我的要求有点不同。让我解释一下。无论我们在哪里丢失了值,我们必须添加进程和下一个可用值,并将它与缺失值的数目分开。因此,您只需要用平均值填充空白空间?是的,这确实有帮助。我在实现这一逻辑方面遇到了一些困难,但是您的代码似乎是可行的。谢谢你。我已经用几个用例对它进行了测试,所有测试都很好。再次感谢..是的,这确实有帮助..我在实现这个逻辑时遇到了一些困难,但您的代码似乎做到了这一点。谢谢你。我已经用几个用例对它进行了测试,所有测试都很好。再次感谢……涵盖了所有用例。可以用一个函数替换两个相同的if条件,但这看起来更直观。感谢涵盖了所有用例。可以用替换两个相同的if条件