Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用指定格式填充缺少的值_Python - Fatal编程技术网

Python 用指定格式填充缺少的值

Python 用指定格式填充缺少的值,python,Python,我有一个问题需要解决,那就是用特定的格式填充缺少的值 输入: "_,_,30,_,_,_,50,_,_" 输出: 10,10,12,12,12,12,4,4,4 我们将从左到右填充缺少的值 首先,我们将把30分配给左边两个缺失的值10,10,10,u,u,u,50,u_ 现在将总和10+50缺失值分布在10,10,12,12,12,12,12,12,_ 现在我们将12分配到右侧缺失值10、10、12、12、12、4、4、4 我只能解决到一定程度 s="_,_

我有一个问题需要解决,那就是用特定的格式填充缺少的值

输入:

"_,_,30,_,_,_,50,_,_"  
输出:

10,10,12,12,12,12,4,4,4
我们将从左到右填充缺少的值

首先,我们将把30分配给左边两个缺失的值10,10,10,u,u,u,50,u_ 现在将总和10+50缺失值分布在10,10,12,12,12,12,12,12,_ 现在我们将12分配到右侧缺失值10、10、12、12、12、4、4、4 我只能解决到一定程度

s="_,_,30,_,_,_,50,_,_"
s1=s.split(",")
print(s1)
print('**************')

w=[]

for i in s1:
    if i=='_':
        w.append(0)
    else:
        w.append(int(i))
print(w)

print('*****************')

x=w[0:3]
y=w[3:]

print(x)
print('**********')
print(y)
print('***********')

avg=int(np.mean(x))
print(avg)
print('********')

c=list(map(lambda x: avg,x) )

print(c)

你说这将是一种特殊的格式。所以我假设30和50代表索引[2]和索引[7]。所以,这里是我的解决方案。如果你说30和50总是存在于列表中,但它们的索引不是常数,你只需要使用s.index函数来查找索引,并使用该索引来获得新的计数和值。 代码如下:

s = "_,_,30,_,_,_,50,_,_"
s = s.split(",")  
result = []
count = s.index('30') + 1
value = int(s[2]) / count
while count > 1:
    result.append(str(int(value)))
    count -= 1
value = int((value + int(s[6])) / (6 - 2 + 1))
count = 6 - 2 + 1
while count > 1:
    result.append(str(value))
    count -= 1
value = int(value / (len(s) - 6))
count = len(s) - 6
while count > 0:
    result.append(str(value))
    count -= 1
print(result)

您可以创建一个要填充的函数,将您编写的代码模块化是一个好主意:

from typing import List


def fill_missing_values(s: str) -> str:
    s1: List[str] = s.split(',')
    first_num_value: int = 0
    start_at: int = s1.index('_')
    end_at: int = 0
    for index, value in enumerate(s1[start_at:]):
        end_at = index
        if value.isdigit():
            first_num_value = int(value)
            break
    # Now you can replace values
    start_at = 0 if start_at == 0 else start_at - 1
    end_at += 0 if start_at == 0 else start_at + 1
    previous_value: int = 0 if start_at == 0 else int(s1[start_at])
    range_len: int = (end_at - start_at + 1) if start_at == 0 else (end_at - start_at + 1)
    new_value: float = (first_num_value + previous_value) / range_len
    for index in range(start_at, end_at+1):
        s1[index] = new_value
    return ','.join([str(x).replace('.0', '') for x in s1])
然后在循环中调用它:

s: str = "_,_,30,_,_,_,50,_,_"
matches = ['_,', ',_,', ',_']
index: int = 1
while any(x in s for x in matches):
    s = fill_missing_values(s)
    print('step {}: {}'.format(index, s))  # this is to show temporarily status, you can remove it
    index += 1
print(s.split(','))
在本例中,我在控制台中打印了输出

step 1: 10,10,10,_,_,_,50,_,_
step 2: 10,10,12,12,12,12,12,_,_
step 3: 10,10,12,12,12,12,4,4,4
['10', '10', '12', '12', '12', '12', '4', '4', '4']

它并不总是30,50会出现在列表中,它可以是任何否,也可以是除第一个和最后一个0以外的任何位置,n-1。是的,所以我在这里使用了索引,那么你只需要使用第一个和第二个非字符的索引,而不是30和50的索引。