我是Python的新手,我有一个问题。我有一个看起来像这样的清单: List = ["B-Guild","I-Guild","I-Guild","L-Guild","B-Gene","L-Gene","U-Car"]所有带有B-(I)-L的单词都属于彼此,我想使用一个函数来表明这一点。def combine(x): foo = [] regexp_B = ("B-" + r'.*') regexp_I = ("I-" + r'.*') regexp_L = ("L-" + r'.*') regexp_U = ("U-" + r'.*') for i in range(0,len(x),1): if re.match(regexp_B, x[i]): print("Found B") foo.append[i+x[i]] if re.match(regexp_I, x[i+1]): print("Found I") foo.append[i+1+x[i+1]] if re.match(regexp_I, x[i+1]): print("Found I") foo.append[i+1+x[i+1]] else: print("Found L") foo.append[i+1+x[i+1]] else: print("Found L") foo.append[i1+x[i1]] elif re.match(regexp_L, x[i]): print("L") foo.append[i1+x[i1]] elif re.match(regexp_U, x[i]): print("Found U") foo.append[i1+x[i1]]return fooList_New = combine(List)所需的输出:foo = ["0B-Guild","0I-Guild","0I-Guild","OL-Guild","1B-Gene","1L-Gene","2U-Car"]编辑:输出遵循此逻辑:每次"B-"出现前缀时,要跟随的单词都是一个“主题”的一部分,直到出现"L-"前缀。这些单词之前必须具有相同的编号,以便可以对其进行分组以实现进一步的功能。"U-"前缀不遵循该逻辑,仅需在其前面加上一个数字即可将它们与其他单词区分开。可以将其视为将这些单词归为一组的计数器。
1 回答
杨魅力
TA贡献1811条经验 获得超6个赞
def combine(some_list):
current_group = 0 # starts with 0
g_size = 0 # current group size
for elem in some_list:
g_size += 1
if elem.startswith('U-') and g_size > 1:
g_size = 1
current_group += 1
yield '{}{}'.format(current_group, elem)
if elem.startswith(('L-', 'U-')): # each L- or U- also finishes a group
g_size = 0
current_group += 1
>>> List = ["B-Guild","I-Guild","I-Guild","L-Guild","B-Gene","L-Gene","U-Car"]
>>> print(list(combine(List)))
>>> List = ["B-Guild","I-Guild","I-Guild","L-Guild","B-Guild","L-Guild","U-Guild"]
>>> print(list(combine(List)))
添加回答
举报
0/150
提交
取消