Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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_Arrays_List - Fatal编程技术网

Python 如何替换数组中的项

Python 如何替换数组中的项,python,arrays,list,Python,Arrays,List,我有一个数组,比如: key = ['*', '(DATE*', '*', '*', '*)', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '(GPE*', '*)', '*', '*', '*', '(DATE)', '*'] 我有这样一个数组,我想为它执行如下任务: 遍历数组 一旦我找到以“(”开头但不以“')结尾的条目 替换下一个“”项,直到找不到“

我有一个数组,比如:

key = ['*', '(DATE*', '*', '*', '*)', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '(GPE*', '*)', '*', '*', '*', '(DATE)', '*'] 
我有这样一个数组,我想为它执行如下任务:

  • 遍历数组

  • 一旦我找到以“(”开头但不以“')结尾的条目

  • 替换下一个“”项,直到找不到“)”,并将“*)”替换为以“(”开头的已找到项的条带

  • 如果条目在“()”范围内,则应仅删除。至于倒数第二个元素(日期),则仅替换为日期

  • 例如。 我们有第二个条目“(日期*”后接“,”,“,”*”),因此这些条目应仅替换为日期

    输出应为:

    key = ['*', 'DATE', 'DATE', 'DATE', 'DATE', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', 'GPE', 'GPE', '*', '*', '*', 'DATE', '*'] 
    

    我知道它不太像蟒蛇,无论如何你可以试试这个:

    key = ['*', '(DATE*', '*', '*', '*)', '*', '*', '*', '*', '*', '*',
       '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '(GPE*', '*)',
       '*', '*', '*', '(DATE)', '*']
    
    for i in key:
        if i.startswith('(') and not (i.endswith(')')):
            a = key[key.index(i)+1:]
            for j in a:
                if j.endswith(')'):
                    a = a[:a.index(j)+1]
                    break
            for l in range(key.index(i), key.index(i)+len(a)+1):
                key[l] = i.strip('(').strip('*')
        elif i.startswith('(') and i.endswith(')'):
            key[key.index(i)] = i.strip('(').strip(')')
    
    print(key)
    
    它将给出O/p如:

    ['*', 'DATE', 'DATE', 'DATE', 'DATE', '*', '*', '*', '*', '*', '*', 
     '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', 'GPE', 
    'GPE', '*', '*', '*', 'DATE', '*']
    
    这将提供u输出:

    ['*', 'DATE', 'DATE', 'DATE', 'DATE', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', 'GPE', 'GPE', '*', '*', '*', 'DATE', '*']
    

    我建议您使用这种易于阅读的解决方案。我定义了另一个列表
    newKey
    ,以避免在迭代其owm元素时修改列表:

    key = ['*', '(DATE*', '*', '*', '*)', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '(GPE*', '*)', '*', '*', '*', '(DATE)', '*']
    
    
    newKey = []
    next_x = None
    
    for x in key:
        if x.startswith('(') and x.endswith(')'):
            newKey.append(x.strip('()*'))
        elif x.startswith('('):
            newKey.append(x.strip('(*'))
            next_x = x.strip('(*')
        elif x.endswith(')'):
            newKey.append(next_x.strip('*)'))
            next_x = None
        elif next_x is not None:
            newKey.append(next_x)
        else:
            newKey.append(x)  
    
    key = newKey[:]
    
    print(key)
    

    可以使用简单的正则表达式完成:

    string = ' '.join(['*', '(DATE*', '*', '*', '*)', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '(GPE*', '*)', '*', '*', '*', '(DATE)', '*'])
    result = re.sub(r'\((.*?)\)', lambda m: ' '.join([m.group(1).replace('*', '').strip()
     for n in range(1 if m.group(0).count('*') == 0 else m.group(0).count('*'))]), string).split(' ')
    print(result)
    

    您可以使用以下代码:

    current_entry = None
    for i, k in enumerate(key):
        if k.startswith('(') and k.endswith(')'):
            key[i] = k.strip('(').strip(')')
            continue
        if k.startswith('(') and not k.endswith(')'):
            current_entry = k.strip('(').strip('*')
        if current_entry:
            key[i] = current_entry
        if k.endswith(')'):
            current_entry = None
    
    **只有一些正则表达式和while循环**
    进口稀土
    key=key=['*'、'(日期*'、'*'、'*'、'*'、'*'、'*'、'*'、'*'、'*'、'*'、'*'、'*'、'*',
    “*”、“*”、“*”、“*”、“*”、“*”、“*”、“*”、“*”、“*”、“(GPE*”、“*)”、“*”、“*”、“*”、“*”(日期)、“*”]
    val=0
    当val

    输出-
    
    
    current_entry = None
    for i, k in enumerate(key):
        if k.startswith('(') and k.endswith(')'):
            key[i] = k.strip('(').strip(')')
            continue
        if k.startswith('(') and not k.endswith(')'):
            current_entry = k.strip('(').strip('*')
        if current_entry:
            key[i] = current_entry
        if k.endswith(')'):
            current_entry = None
    
    **Nothing but some regex and while loops**
    import re
    key = key = ['*', '(DATE*', '*', '*', '*)', '*', '*', '*', '*', '*', '*', '*', '*', '*',
                 '*', '*', '*', '*', '*', '*', '*', '*', '*', '(GPE*', '*)', '*', '*', '*', '(DATE)', '*']
    val = 0
    while val < len(key):
        value = key[val]
        if re.findall(r'\(',value):
            value = re.findall(r'\w+', value)[0]
            while re.findall(r'\)', key[val]) == []:
                key[val] = value
                val += 1
            key[val] = value
        val += 1
    print key