首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在循环重新开始之前返回循环中的列表

在循环重新开始之前返回循环中的列表
EN

Stack Overflow用户
提问于 2022-08-14 10:43:37
回答 2查看 60关注 0票数 -3

我的代码:

代码语言:javascript
复制
lis2 = []
lis1 = []
for cm in comments:
    sp = cm.split()
    for s in sp:
        for tf in tfidf:
            if tf == s:
                lis2.append(tf)
            else:
                continue

lis1.append(lis2)      
print(lis1)            
data = pd.DataFrame(lis1)

在这段代码中,有两个列表:

  1. comments:句子列表
  2. tfidf:一个单词列表。

我想迭代每个句子(comments),并从tfidf列表中找到任何单词,并将其附加到新列表 lis2中。

此外,当第一句完成后,将lis2追加到lis1,然后转到下一句。

但是我的代码只返回这样的单词:

代码语言:javascript
复制
[['custom', 'servic', 'portfolio', 'time', 'custom', 'servic', 'custom', 'servic', 'support', 'ticket', 'custom', 'servic', 'experi', 'platform', 'user', 'experi', 'account', 'portfolio', 'experi', 'user', 'experi', 'user', 'platform', 'account', 'time', 'fast', 'platform', 'custom', 'custom', 'account', 'time', 'fast', 'time', 'time', 'account', 'custom', 'servic', 'servic', 'account', 'user', 'custom', 'custom', 'account', 'time', 'account', 'user', 'time', 'account']
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-14 10:57:12

代码语言:javascript
复制
comments = ['a1 a2 a3',  'b1 b2 b3',  'c1 c2 c3']
tfidf = ['a2', 'b1', 'b3']

lis_1 = []

for sentence in comments:
    lis_2 = []
    words = sentence.split()
    for word in words:
        if word in tfidf:
            lis_2.append(word)
    # After all words of a sentence are processed:
    lis_1.append(lis_2)

print(lis_1)

输出:

代码语言:javascript
复制
[['a2'], ['b1', 'b3'], []]

使用列表理解集合可以更简洁地获得相同的结果。

代码语言:javascript
复制
lis_1 = [list(set(sentence.split()).intersection(tfidf)) for sentence in comments]
票数 0
EN

Stack Overflow用户

发布于 2022-08-14 12:38:08

利用pandas.Series.str.findall,就可以避免循环。

代码语言:javascript
复制
comments = ["customer service emergency for service department", "client portfolio information about client spends lots of time on searching the website", "supporting customer to buy campaign ticket", "free bitcoin faucet", "experienced trading manager recruiter john cena", "user platform account deletion","Fast response platform with rock", "Login time consuming", "apple juice discount"]
comments
###
['customer service emergency for service department',
 'client portfolio information about client spends lots of time on searching the website',
 'supporting customer to buy campaign ticket',
 'free bitcoin faucet',
 'experienced trading manager recruiter john cena',
 'user platform account deletion',
 'Fast response platform with rock',
 'Login time consuming',
 'apple juice discount']
代码语言:javascript
复制
tfidf = ["customer", "account", "service", "user", "time"]
re_pat = "|".join(tfidf)
df = pd.DataFrame({'comments':comments})
代码语言:javascript
复制
output = df['comments'].str.findall(re_pat).tolist()
output
###
[['customer', 'service', 'service'],
 ['time'],
 ['customer'],
 [],
 [],
 ['user', 'account'],
 [],
 ['time'],
 []]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73350972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档