gpt4 book ai didi

python - 选择包含至少一个数字的 n-gram

转载 作者:行者123 更新时间:2023-12-04 09:41:56 24 4
gpt4 key购买 nike

我有一个 n-gram 列表

('allo', 'stesso', 'modo', 'dell’italia,', 'che')
('stesso', 'modo', 'dell’italia,', 'che', 'sta')
('modo', 'dell’italia,', 'che', 'sta', 'già')
('dell’italia,', 'che', 'sta', 'già', 'pensando')
('che', 'sta', 'già', 'pensando', 'alla')
('sta', 'già', 'pensando', 'alla', 'riapertura')
('soli', '2.900,', 'contando', 'un', 'crollo')
('2.900,', 'contando', 'un', 'crollo', 'del')
('contando', 'un', 'crollo', 'del', '99.9%')
('un', 'crollo', 'del', '99.9%', 'rispetto')
('che', 'prevede', '12,5', 'miliardi', 'di')
('prevede', '12,5', 'miliardi', 'di', 'dollari')
('12,5', 'miliardi', 'di', 'dollari', 'per')
...

由...制作
from nltk import ngrams

n = 5
list_ngrams=[]

for i in my_list:
grams = ngrams(i.split(), n)

for gram in grams:
print(gram)
list_ngrams.append(gram)

我只想选择包含至少一个数字的 n-gram,例如
('soli', '2.900,', 'contando', 'un', 'crollo')
('2.900,', 'contando', 'un', 'crollo', 'del')
('contando', 'un', 'crollo', 'del', '99.9%')
('un', 'crollo', 'del', '99.9%', 'rispetto')
('che', 'prevede', '12,5', 'miliardi', 'di')
('prevede', '12,5', 'miliardi', 'di', 'dollari')
('12,5', 'miliardi', 'di', 'dollari', 'per')

你能帮我选吗?

最佳答案

你可以这样做:

l = [('allo', 'stesso', 'modo', 'dell’italia,', 'che'),
('stesso', 'modo', 'dell’italia,', 'che', 'sta'),
('modo', 'dell’italia,', 'che', 'sta', 'già'),
('dell’italia,', 'che', 'sta', 'già', 'pensando'),
('che', 'sta', 'già', 'pensando', 'alla'),
('sta', 'già', 'pensando', 'alla', 'riapertura'),
('soli', '2.900,', 'contando', 'un', 'crollo'),
('2.900,', 'contando', 'un', 'crollo', 'del'),
('contando', 'un', 'crollo', 'del', '99.9%'),
('un', 'crollo', 'del', '99.9%', 'rispetto'),
('che', 'prevede', '12,5', 'miliardi', 'di'),
('prevede', '12,5', 'miliardi', 'di', 'dollari'),
('12,5', 'miliardi', 'di', 'dollari', 'per')]

l2 = [i for i in l if any(any(w.isdigit() for w in s) for s in i)]

print(l2)

输出:
[('soli', '2.900,', 'contando', 'un', 'crollo'), ('2.900,', 'contando', 'un', 'crollo', 'del'), ('contando', 'un', 'crollo', 'del', '99.9%'), ('un', 'crollo', 'del', '99.9%', 'rispetto'), ('che', 'prevede', '12,5', 'miliardi', 'di'), ('prevede', '12,5', 'miliardi', 'di', 'dollari'), ('12,5', 'miliardi', 'di', 'dollari', 'per')]

关于python - 选择包含至少一个数字的 n-gram,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62292731/

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