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