gpt4 book ai didi

python - 如何在 Python 的某些条件下根据其他列表中的索引列表从一个列表中查找字符串?

转载 作者:太空狗 更新时间:2023-10-30 02:26:45 25 4
gpt4 key购买 nike

我是 python 的新手,并且不断学习如何在 python 中构建更好的代码。我有两个 list ;一个索引存储在 x 变量中,其中 x 中的索引表示名为 bb 的列表中带有字符串 ('IN') 的元组的索引,并且在两侧至少被一个包含 'NN' 的元组包围。

我想从下面的代码中得到的是,从 bb 中 x 中提到的每个索引,在 bb 列表中的字符串元组的两侧出现了多少个以 'NN' 开头的连续字符串。

我试过下面的代码,但代码效率不够。请任何人帮助我提高代码效率。

     bb = [('The', 'RB'),
('company', 'NN'),
('whose', 'NNS'),
('stock', 'IN'),
('has', 'NNP'),
('been', 'NNS'),
('on', 'NNP'),
('tear', 'VBJ'),
('this', 'VB'),
('week', 'NNS'),
('already', 'NN'),
('sells', 'IN'),
('its', 'NNP'),
('graphics', 'NNS'),
('processing', 'VB'),
('units', 'VBJ'),
('biggest', 'NNS'),
('cloud', 'NN'),
('companies', 'IN'),
('just', 'NNP'),
('that', 'IN')]

def solvr(bb):
x = []
for i in range(len(bb)-1):
if bb[i][1] == 'IN':
if 'NN' in (bb[i-1][1]) and 'NN' in (bb[i+1][1]):
x.append(i)
#===============================

for i in range(len(bb)-1):
if i in x:
k=[]
front = bb[i+1:]
v = 0-i
back = bb[:-v]
#======================

for i in back:
if 'NN' in i[1]:
k.append(i[0])
[[] for i in k]
#================================


for i, j in enumerate(front):
if front[i][1][:2] == 'NN':
k.append(front[i][0])
else:
break
return(k)

>> solvr(bb)

output:

['company',
'whose',
'has',
'been',
'on',
'week',
'already',
'its',
'graphics',
'biggest',
'cloud',
'just']

我对代码的期望是在新列表中获取每个迭代结果,每个列表中还包含“IN”字符串。

 [['company', 'whose', 'stock', 'has', 'been', 'on'],
['week', 'already', 'sells', 'its', 'graphics'],
['biggest', 'cloud', 'companies', 'just']]

如果有人对我的代码进行任何更改,我将不胜感激。

最佳答案

这对 itertools.groupby 来说似乎是个好问题根据您定义的某些条件,它根据每个元素是否为真将列表的连续元素分组在一起。

在您的情况下,您可以使用以下内容:

groups = itertools.groupby(bb, lambda x: x[1][:2] in ['IN', 'NN']) 
result = [list(b) for a,b in groups if a]
result = [[w[0] for w in b] for b in result if 'IN' in [w[1] for w in b]]

print(result)

[['company', 'whose', 'stock', 'has', 'been', 'on'],
['week', 'already', 'sells', 'its', 'graphics'],
['biggest', 'cloud', 'companies', 'just', 'that']]

这是有效的,因为每当条件(第二个元素是“IN”或以“NN”开头)从假变为真(反之亦然)时,groups 会将您的原始 bb 列表拆分为子列表).如果我们显示组,您可以看到它是如何拆分的:

groups = itertools.groupby(bb, lambda x: x[1][:2] in ['IN', 'NN']) 

print([(a,list(b)) for a,b in groups])

[(False, [('The', 'RB')]),
(True,
[('company', 'NN'),
('whose', 'NNS'),
('stock', 'IN'),
('has', 'NNP'),
('been', 'NNS'),
('on', 'NNP')]),
(False, [('tear', 'VBJ'), ('this', 'VB')]),
(True,
[('week', 'NNS'),
('already', 'NN'),
('sells', 'IN'),
('its', 'NNP'),
('graphics', 'NNS')]),
(False, [('processing', 'VB'), ('units', 'VBJ')]),
(True,
[('biggest', 'NNS'),
('cloud', 'NN'),
('companies', 'IN'),
('just', 'NNP'),
('that', 'IN')])]

bool 值表示以下列表是否包含满足或不满足条件的元素。现在您所要做的只是保持 one's who 的 bool 值为真(满足条件),然后将包含 'IN' 的子列表保留为词性标记之一。

只是为了好玩,如果您希望将整个解决方案作为一个(几乎不可读的长)单行,您可以使用:

[[w[0] for w in b] for b in [list(b) for a,b in itertools.groupby(bb, lambda x: x[1][:2] in ['IN', 'NN'])  if a] if 'IN' in [w[1] for w in b]]

编辑

为了仅保留包含 'IN' 字词的子列表,并且 至少有一个 'NN' 字词在你的任一侧可以执行以下操作:

从与之前相同的初始 groupsresults 变量开始:

groups = itertools.groupby(bb, lambda x: x[1][:2] in ['IN', 'NN']) 
result = [list(b) for a,b in groups if a]

将相同的 groupby 函数应用于子列表,但这次将条件设置为词性等于 'IN':

result = [[(a,list(b)) for a,b in itertools.groupby(r, lambda x: x[1] == 'IN')] for r in result]

现在遍历 result 并删除子列表中 groupby 的 bool 值为真(POS 为 'IN')的任何元素并且它位于子列表的右边缘或左边缘(索引为 0-1)

result = [[b for i,(a,b) in enumerate(r) if (a and i not in [0,len(r)-1]) or not a] for r in result]

现在我们已经删除了这些,我们可以将所有内容连接在一起并删除 POS 标签以获得正确的输出格式(有关列表展平语法的详细信息,请参阅 here)

result = [[w[0] for sub in r for w in sub] for r in result]

print(result)

[['company', 'whose', 'stock', 'has', 'been', 'on'],
['week', 'already', 'sells', 'its', 'graphics'],
['biggest', 'cloud', 'companies', 'just']]

关于python - 如何在 Python 的某些条件下根据其他列表中的索引列表从一个列表中查找字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com