我有以下列表:
$('.profile-badge__follow:contains("Follow")').click();
考虑到每个元组的第二个元素,我想提取三重模式。例如,我想说我想提取具有第二个元素data = [('Mr', 'PROPN'), ('.', 'PUNCT'), ('William', 'PROPN'), ('Henry', 'PROPN'), ('Gates', 'PROPN'), (',', 'PUNCT'), ('III', 'NUM'), ('is', 'VERB'), ('Founder', 'PROPN'), ('and', 'CONJ'), ('Technology', 'PROPN'), ('Advisor', 'NOUN'), ('Director', 'NOUN'), ('of', 'ADP'), ('Microsoft', 'PROPN'), ('Corporation', 'PROPN'), ('a', 'DET'), ('cofounder', 'NOUN'), ('served', 'VERB'), ('as', 'ADP'), ('Chairman', 'PROPN'), ('from', 'ADP'), ('our', 'PRON'), ('incorporation', 'NOUN'), ('in', 'ADP'), ('1981', 'NUM'), ('until', 'ADP'), ('2014', 'NUM'), ('He', 'PRON'), ('currently', 'ADV'), ('acts', 'VERB'), ('Technical', 'ADJ'), ('to', 'ADP'), ('Nadella', 'NUM'), ('on', 'ADP'), ('key', 'ADJ'), ('development', 'NOUN'), ('projects', 'NOUN'), ('retired', 'VERB'), ('an', 'DET'), ('employee', 'NOUN'), ('2008', 'NUM'), ('Chief', 'NOUN'), ('Software', 'PROPN'), ('Architect', 'PROPN'), ('2000', 'NUM'), ('2006', 'NUM'), ('when', 'ADV'), ('he', 'PRON'), ('announced', 'VERB'), ('his', 'PRON'), ('two', 'NUM'), ('-', 'PUNCT'), ('year', 'NOUN'), ('plan', 'NOUN'), ('transition', 'VERB'), ('out', 'ADP'), ('day', 'NOUN'), ('full', 'ADJ'), ('time', 'NOUN'), ('role', 'NOUN'), ('Executive', 'PROPN'), ('Officer', 'PROPN'), ('resigned', 'VERB'), ('assumed', 'VERB'), ('the', 'DET'), ('position', 'NOUN'), ('As', 'ADP'), ('co', 'PROPN'), ('chair', 'NOUN'), ('Bill', 'NOUN'), ('&', 'CONJ'), ('Melinda', 'PROPN'), ('Foundation', 'PROPN'), ('shapes', 'NOUN'), ('approves', 'VERB'), ('grant', 'NOUN'), ('making', 'VERB'), ('strategies', 'NOUN'), ('advocates', 'NOUN'), ('for', 'ADP'), ('foundation’s', 'NUM'), ('issues', 'NOUN'), ('helps', 'VERB'), ('set', 'VERB'), ('overall', 'ADJ'), ('direction', 'NOUN'), ('organization', 'NOUN'), ('founder', 'NOUN'), ('’', 'NUM'), ('foresight', 'NOUN'), ('vision', 'NOUN'), ('personal', 'ADJ'), ('computing', 'NOUN'), ('have', 'AUX'), ('been', 'VERB'), ('central', 'ADJ'), ('success', 'NOUN'), ('software', 'NOUN'), ('industry', 'NOUN'), ('has', 'VERB'), ('unparalleled', 'ADJ'), ('knowledge', 'NOUN'), ('Company’s', 'NUM'), ('history', 'NOUN'), ('technologies', 'NOUN'), ('Company', 'NOUN'), ('its', 'PRON'), ('grew', 'VERB'), ('fledgling', 'ADJ'), ('business', 'NOUN'), ('into', 'ADP'), ('world’s', 'NUM'), ('leading', 'VERB'), ('company', 'NOUN'), ('process', 'NOUN'), ('creating', 'VERB'), ('one', 'NUM'), ('most', 'ADV'), ('prolific', 'ADJ'), ('sources', 'NOUN'), ('innovation', 'NOUN'), ('powerful', 'ADJ'), ('brands', 'NOUN'), ('through', 'ADP'), ('motion', 'NOUN'), ('technological', 'ADJ'), ('strategic', 'ADJ'), ('programs', 'NOUN'), ('that', 'DET'), ('are', 'VERB'), ('core', 'NOUN'), ('part', 'NOUN'), ('continues', 'VERB'), ('provide', 'VERB'), ('technical', 'ADJ'), ('input', 'NOUN'), ('evolution', 'NOUN'), ('productivity', 'NOUN'), ('platform', 'NOUN'), ('mobile', 'NOUN'), ('first', 'ADJ'), ('cloud', 'NOUN'), ('world', 'NOUN'), ('His', 'PRON'), ('work', 'NOUN'), ('overseeing', 'VERB'), ('provides', 'VERB'), ('global', 'ADJ'), ('insights', 'NOUN'), ('relevant', 'ADJ'), ('current', 'ADJ'), ('future', 'ADJ'), ('opportunities', 'NOUN'), ('keen', 'ADJ'), ('appreciation', 'NOUN'), ('stakeholder', 'ADJ'), ('interests', 'NOUN')]
和'of'
的元组之间'NOUN'
的所有元组:
'PROPN'
因此,我的问题是如何在不使用正则表达式的情况下提取上述模式?我不想使用正则表达式的原因是因为,我将开始以更多不同的方式提取元组。例如,具有第一个值[('Director', 'NOUN'), ('of', 'ADP'), ('Microsoft', 'PROPN')]
后跟'world’s'
,'VERB'
的元组:
'NOUN'
答案 0 :(得分:1)
你可以用:
循环它data = [('Mr', 'PROPN'), ('.', 'PUNCT'), ('William', 'PROPN'), ('Henry', 'PROPN'), ('Gates', 'PROPN'), (',', 'PUNCT'), ('III', 'NUM'), ('is', 'VERB'), ('Founder', 'PROPN'), ('and', 'CONJ'), ('Technology', 'PROPN'), ('Advisor', 'NOUN'), ('Director', 'NOUN'), ('of', 'ADP'), ('Microsoft', 'PROPN'), ('Corporation', 'PROPN'), ('a', 'DET'), ('cofounder', 'NOUN'), ('served', 'VERB'), ('as', 'ADP'), ('Chairman', 'PROPN'), ('from', 'ADP'), ('our', 'PRON'), ('incorporation', 'NOUN'), ('in', 'ADP'), ('1981', 'NUM'), ('until', 'ADP'), ('2014', 'NUM'), ('He', 'PRON'), ('currently', 'ADV'), ('acts', 'VERB'), ('Technical', 'ADJ'), ('to', 'ADP'), ('Nadella', 'NUM'), ('on', 'ADP'), ('key', 'ADJ'), ('development', 'NOUN'), ('projects', 'NOUN'), ('retired', 'VERB'), ('an', 'DET'), ('employee', 'NOUN'), ('2008', 'NUM'), ('Chief', 'NOUN'), ('Software', 'PROPN'), ('Architect', 'PROPN'), ('2000', 'NUM'), ('2006', 'NUM'), ('when', 'ADV'), ('he', 'PRON'), ('announced', 'VERB'), ('his', 'PRON'), ('two', 'NUM'), ('-', 'PUNCT'), ('year', 'NOUN'), ('plan', 'NOUN'), ('transition', 'VERB'), ('out', 'ADP'), ('day', 'NOUN'), ('full', 'ADJ'), ('time', 'NOUN'), ('role', 'NOUN'), ('Executive', 'PROPN'), ('Officer', 'PROPN'), ('resigned', 'VERB'), ('assumed', 'VERB'), ('the', 'DET'), ('position', 'NOUN'), ('As', 'ADP'), ('co', 'PROPN'), ('chair', 'NOUN'), ('Bill', 'NOUN'), ('&', 'CONJ'), ('Melinda', 'PROPN'), ('Foundation', 'PROPN'), ('shapes', 'NOUN'), ('approves', 'VERB'), ('grant', 'NOUN'), ('making', 'VERB'), ('strategies', 'NOUN'), ('advocates', 'NOUN'), ('for', 'ADP'), ('foundation’s', 'NUM'), ('issues', 'NOUN'), ('helps', 'VERB'), ('set', 'VERB'), ('overall', 'ADJ'), ('direction', 'NOUN'), ('organization', 'NOUN'), ('founder', 'NOUN'), ('’', 'NUM'), ('foresight', 'NOUN'), ('vision', 'NOUN'), ('personal', 'ADJ'), ('computing', 'NOUN'), ('have', 'AUX'), ('been', 'VERB'), ('central', 'ADJ'), ('success', 'NOUN'), ('software', 'NOUN'), ('industry', 'NOUN'), ('has', 'VERB'), ('unparalleled', 'ADJ'), ('knowledge', 'NOUN'), ('Company’s', 'NUM'), ('history', 'NOUN'), ('technologies', 'NOUN'), ('Company', 'NOUN'), ('its', 'PRON'), ('grew', 'VERB'), ('fledgling', 'ADJ'), ('business', 'NOUN'), ('into', 'ADP'), ('world’s', 'NUM'), ('leading', 'VERB'), ('company', 'NOUN'), ('process', 'NOUN'), ('creating', 'VERB'), ('one', 'NUM'), ('most', 'ADV'), ('prolific', 'ADJ'), ('sources', 'NOUN'), ('innovation', 'NOUN'), ('powerful', 'ADJ'), ('brands', 'NOUN'), ('through', 'ADP'), ('motion', 'NOUN'), ('technological', 'ADJ'), ('strategic', 'ADJ'), ('programs', 'NOUN'), ('that', 'DET'), ('are', 'VERB'), ('core', 'NOUN'), ('part', 'NOUN'), ('continues', 'VERB'), ('provide', 'VERB'), ('technical', 'ADJ'), ('input', 'NOUN'), ('evolution', 'NOUN'), ('productivity', 'NOUN'), ('platform', 'NOUN'), ('mobile', 'NOUN'), ('first', 'ADJ'), ('cloud', 'NOUN'), ('world', 'NOUN'), ('His', 'PRON'), ('work', 'NOUN'), ('overseeing', 'VERB'), ('provides', 'VERB'), ('global', 'ADJ'), ('insights', 'NOUN'), ('relevant', 'ADJ'), ('current', 'ADJ'), ('future', 'ADJ'), ('opportunities', 'NOUN'), ('keen', 'ADJ'), ('appreciation', 'NOUN'), ('stakeholder', 'ADJ'), ('interests', 'NOUN')]
[(x,y) for x,y in data if ('NOUN' == y) or ('PROPN' in y)]
我提出了两种方法来评估上面的一个,以便你可以选择。 您还可以通过将其转换为pandas来使用更强大的语法进行查询。这有助于使用数据帧进行更复杂的查询。
import pandas as pd
data = [('Mr', 'PROPN'), ('.', 'PUNCT'), ('William', 'PROPN'), ('Henry', 'PROPN'), ('Gates', 'PROPN'), (',', 'PUNCT'), ('III', 'NUM'), ('is', 'VERB'), ('Founder', 'PROPN'), ('and', 'CONJ'), ('Technology', 'PROPN'), ('Advisor', 'NOUN'), ('Director', 'NOUN'), ('of', 'ADP'), ('Microsoft', 'PROPN'), ('Corporation', 'PROPN'), ('a', 'DET'), ('cofounder', 'NOUN'), ('served', 'VERB'), ('as', 'ADP'), ('Chairman', 'PROPN'), ('from', 'ADP'), ('our', 'PRON'), ('incorporation', 'NOUN'), ('in', 'ADP'), ('1981', 'NUM'), ('until', 'ADP'), ('2014', 'NUM'), ('He', 'PRON'), ('currently', 'ADV'), ('acts', 'VERB'), ('Technical', 'ADJ'), ('to', 'ADP'), ('Nadella', 'NUM'), ('on', 'ADP'), ('key', 'ADJ'), ('development', 'NOUN'), ('projects', 'NOUN'), ('retired', 'VERB'), ('an', 'DET'), ('employee', 'NOUN'), ('2008', 'NUM'), ('Chief', 'NOUN'), ('Software', 'PROPN'), ('Architect', 'PROPN'), ('2000', 'NUM'), ('2006', 'NUM'), ('when', 'ADV'), ('he', 'PRON'), ('announced', 'VERB'), ('his', 'PRON'), ('two', 'NUM'), ('-', 'PUNCT'), ('year', 'NOUN'), ('plan', 'NOUN'), ('transition', 'VERB'), ('out', 'ADP'), ('day', 'NOUN'), ('full', 'ADJ'), ('time', 'NOUN'), ('role', 'NOUN'), ('Executive', 'PROPN'), ('Officer', 'PROPN'), ('resigned', 'VERB'), ('assumed', 'VERB'), ('the', 'DET'), ('position', 'NOUN'), ('As', 'ADP'), ('co', 'PROPN'), ('chair', 'NOUN'), ('Bill', 'NOUN'), ('&', 'CONJ'), ('Melinda', 'PROPN'), ('Foundation', 'PROPN'), ('shapes', 'NOUN'), ('approves', 'VERB'), ('grant', 'NOUN'), ('making', 'VERB'), ('strategies', 'NOUN'), ('advocates', 'NOUN'), ('for', 'ADP'), ('foundation’s', 'NUM'), ('issues', 'NOUN'), ('helps', 'VERB'), ('set', 'VERB'), ('overall', 'ADJ'), ('direction', 'NOUN'), ('organization', 'NOUN'), ('founder', 'NOUN'), ('’', 'NUM'), ('foresight', 'NOUN'), ('vision', 'NOUN'), ('personal', 'ADJ'), ('computing', 'NOUN'), ('have', 'AUX'), ('been', 'VERB'), ('central', 'ADJ'), ('success', 'NOUN'), ('software', 'NOUN'), ('industry', 'NOUN'), ('has', 'VERB'), ('unparalleled', 'ADJ'), ('knowledge', 'NOUN'), ('Company’s', 'NUM'), ('history', 'NOUN'), ('technologies', 'NOUN'), ('Company', 'NOUN'), ('its', 'PRON'), ('grew', 'VERB'), ('fledgling', 'ADJ'), ('business', 'NOUN'), ('into', 'ADP'), ('world’s', 'NUM'), ('leading', 'VERB'), ('company', 'NOUN'), ('process', 'NOUN'), ('creating', 'VERB'), ('one', 'NUM'), ('most', 'ADV'), ('prolific', 'ADJ'), ('sources', 'NOUN'), ('innovation', 'NOUN'), ('powerful', 'ADJ'), ('brands', 'NOUN'), ('through', 'ADP'), ('motion', 'NOUN'), ('technological', 'ADJ'), ('strategic', 'ADJ'), ('programs', 'NOUN'), ('that', 'DET'), ('are', 'VERB'), ('core', 'NOUN'), ('part', 'NOUN'), ('continues', 'VERB'), ('provide', 'VERB'), ('technical', 'ADJ'), ('input', 'NOUN'), ('evolution', 'NOUN'), ('productivity', 'NOUN'), ('platform', 'NOUN'), ('mobile', 'NOUN'), ('first', 'ADJ'), ('cloud', 'NOUN'), ('world', 'NOUN'), ('His', 'PRON'), ('work', 'NOUN'), ('overseeing', 'VERB'), ('provides', 'VERB'), ('global', 'ADJ'), ('insights', 'NOUN'), ('relevant', 'ADJ'), ('current', 'ADJ'), ('future', 'ADJ'), ('opportunities', 'NOUN'), ('keen', 'ADJ'), ('appreciation', 'NOUN'), ('stakeholder', 'ADJ'), ('interests', 'NOUN')]
data = pd.DataFrame(data, columns=['word','type'])
data[(data.type=='NOUN') | (data.type=='PROPN')]
评论部分的附录:
您可以找到有关数据的内容,例如。
data.groupby(data.type).count()
word
type
ADJ 20
ADP 12
ADV 3
AUX 1
CONJ 2
DET 4
NOUN 56
NUM 13
PRON 6
PROPN 16
PUNCT 3
VERB 22
在完成运算后,您可以将其转换回python数据类型。
list(data[(data.type=='NOUN') | (data.type=='PROPN')].word)
答案 1 :(得分:1)
相对较快,但可能是不必要的紧凑解决方案:
from itertools import chain
# Generator of three-tuples matching requirements:
# If `data` is large enough that temp `list`s are a problem, might be worth
# using itertools.islice instead of shallow copy slices
# or using enumerate with lookaround indexing
matchtups = (((wd0, tp0), (wd1, tp1), (wd2, tp2))
for (wd0, tp0), (wd1, tp1), (wd2, tp2) in zip(data, data[1:], data[2:])
if wd1 == 'of' and tp0 == 'NOUN' and tp2 == 'PROPN')
# Flatten out the three-tuple structure:
results = list(chain.from_iterable(matchtups))
答案 2 :(得分:1)
喜欢这样吗?
thing_list = []
for i, x in enumerate(data):
if x[0] == "of":
if (data[i-1][1] == "NOUN") and (data[i+1][1] == "PROPN"):
thing_list.append(data[i-1:i+2])
答案 3 :(得分:1)
这只是因为看起来很有趣而解决了这个问题。如果您愿意忍受map
,lambda
和filter
的汤,这似乎有效:
matches = map(
lambda _: (data[_ - 1], data[_], data[_ + 1]),
filter(
lambda _: data[_ - 1][1] == "NOUN" and data[_ + 1][1] == "PROPN",
map(
lambda _: _[0],
filter(
lambda _: _[1][0] == "of",
enumerate(data)
)
)
)
)
答案 4 :(得分:1)
def trioPattern( trioCols, trioElements):
"""trioCols = (Use element 0 or 1 of the first pair, 0 or 1 of the second pair, 0 or 1 of the third pair)
trioElements = (Phrase of the first element, Phrase of the second element, Phrase of the third element)"""
data = [('Mr', 'PROPN'), ('.', 'PUNCT'), ('William', 'PROPN'), ('Henry', 'PROPN'), ('Gates', 'PROPN'), (',', 'PUNCT'), ('III', 'NUM'), ('is', 'VERB'), ('Founder', 'PROPN'), ('and', 'CONJ'), ('Technology', 'PROPN'), ('Advisor', 'NOUN'), ('Director', 'NOUN'), ('of', 'ADP'), ('Microsoft', 'PROPN'), ('Corporation', 'PROPN'), ('a', 'DET'), ('cofounder', 'NOUN'), ('served', 'VERB'), ('as', 'ADP'), ('Chairman', 'PROPN'), ('from', 'ADP'), ('our', 'PRON'), ('incorporation', 'NOUN'), ('in', 'ADP'), ('1981', 'NUM'), ('until', 'ADP'), ('2014', 'NUM'), ('He', 'PRON'), ('currently', 'ADV'), ('acts', 'VERB'), ('Technical', 'ADJ'), ('to', 'ADP'), ('Nadella', 'NUM'), ('on', 'ADP'), ('key', 'ADJ'), ('development', 'NOUN'), ('projects', 'NOUN'), ('retired', 'VERB'), ('an', 'DET'), ('employee', 'NOUN'), ('2008', 'NUM'), ('Chief', 'NOUN'), ('Software', 'PROPN'), ('Architect', 'PROPN'), ('2000', 'NUM'), ('2006', 'NUM'), ('when', 'ADV'), ('he', 'PRON'), ('announced', 'VERB'), ('his', 'PRON'), ('two', 'NUM'), ('-', 'PUNCT'), ('year', 'NOUN'), ('plan', 'NOUN'), ('transition', 'VERB'), ('out', 'ADP'), ('day', 'NOUN'), ('full', 'ADJ'), ('time', 'NOUN'), ('role', 'NOUN'), ('Executive', 'PROPN'), ('Officer', 'PROPN'), ('resigned', 'VERB'), ('assumed', 'VERB'), ('the', 'DET'), ('position', 'NOUN'), ('As', 'ADP'), ('co', 'PROPN'), ('chair', 'NOUN'), ('Bill', 'NOUN'), ('&', 'CONJ'), ('Melinda', 'PROPN'), ('Foundation', 'PROPN'), ('shapes', 'NOUN'), ('approves', 'VERB'), ('grant', 'NOUN'), ('making', 'VERB'), ('strategies', 'NOUN'), ('advocates', 'NOUN'), ('for', 'ADP'), ('foundation’s', 'NUM'), ('issues', 'NOUN'), ('helps', 'VERB'), ('set', 'VERB'), ('overall', 'ADJ'), ('direction', 'NOUN'), ('organization', 'NOUN'), ('founder', 'NOUN'), ('’', 'NUM'), ('foresight', 'NOUN'), ('vision', 'NOUN'), ('personal', 'ADJ'), ('computing', 'NOUN'), ('have', 'AUX'), ('been', 'VERB'), ('central', 'ADJ'), ('success', 'NOUN'), ('software', 'NOUN'), ('industry', 'NOUN'), ('has', 'VERB'), ('unparalleled', 'ADJ'), ('knowledge', 'NOUN'), ('Company’s', 'NUM'), ('history', 'NOUN'), ('technologies', 'NOUN'), ('Company', 'NOUN'), ('its', 'PRON'), ('grew', 'VERB'), ('fledgling', 'ADJ'), ('business', 'NOUN'), ('into', 'ADP'), ('world’s', 'NUM'), ('leading', 'VERB'), ('company', 'NOUN'), ('process', 'NOUN'), ('creating', 'VERB'), ('one', 'NUM'), ('most', 'ADV'), ('prolific', 'ADJ'), ('sources', 'NOUN'), ('innovation', 'NOUN'), ('powerful', 'ADJ'), ('brands', 'NOUN'), ('through', 'ADP'), ('motion', 'NOUN'), ('technological', 'ADJ'), ('strategic', 'ADJ'), ('programs', 'NOUN'), ('that', 'DET'), ('are', 'VERB'), ('core', 'NOUN'), ('part', 'NOUN'), ('continues', 'VERB'), ('provide', 'VERB'), ('technical', 'ADJ'), ('input', 'NOUN'), ('evolution', 'NOUN'), ('productivity', 'NOUN'), ('platform', 'NOUN'), ('mobile', 'NOUN'), ('first', 'ADJ'), ('cloud', 'NOUN'), ('world', 'NOUN'), ('His', 'PRON'), ('work', 'NOUN'), ('overseeing', 'VERB'), ('provides', 'VERB'), ('global', 'ADJ'), ('insights', 'NOUN'), ('relevant', 'ADJ'), ('current', 'ADJ'), ('future', 'ADJ'), ('opportunities', 'NOUN'), ('keen', 'ADJ'), ('appreciation', 'NOUN'), ('stakeholder', 'ADJ'), ('interests', 'NOUN')]
#Elements of the triple pattern
ColE1, ColE2, ColE3 = trioCols
trios = dict([( (data[e][ColE1], data[e+1][ColE2], data[e+2][ColE3]), (data[e], data[e+1], data[e+2])) for e in range(0, len(data)-2)])
#Triple pattern phrases
E1, E2, E3 = trioElements
if trios.has_key((E1, E2, E3)):
return trios[(E1, E2, E3)]
else:
return "Not found"
示例:
trioPattern((1,0,1),(“NOUN”,“of”,“PROPN”))
(('Director', 'NOUN'), ('of', 'ADP'), ('Microsoft', 'PROPN'))
trioPattern((0,1,1),(“世界”,“VERB”,“NOUN”))
(('world’s', 'NUM'), ('leading', 'VERB'), ('company', 'NOUN'))