首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不在pandas中生成列表的空列表

不在pandas中生成列表的空列表
EN

Stack Overflow用户
提问于 2019-06-05 08:10:59
回答 1查看 32关注 0票数 0

背景

1)我有以下代码来创建df

代码语言:javascript
复制
import pandas as pd
word_list = ['crayons', 'cars', 'camels']
l = ['there are many different crayons in the bright blue box',
     'i like a lot of sports cars because they go really fast',
     'the middle east has many camels to ride and have fun']
df = pd.DataFrame(l, columns=['Text'])
df

    Text
0   there are many different crayons in the bright blue box
1   i like a lot of sports cars because they go really fast
2   the middle east has many camels to ride and have fun

2),我有以下代码来创建一个函数

代码语言:javascript
复制
 def find_next_words(row, word_list):

    sentence = row[0]

    # trigger words are the elements in the word_list
    trigger_words = []
    next_words = []
    last_words = []

    for keyword in word_list:

        words = sentence.split()
        for index in range(0, len(words) - 1):

            if words[index] == keyword:

                trigger_words.append(keyword)

                #get the 3 words that follow trigger word
                next_words.append(words[index + 1:index + 4]) 

                #get the 3 words that come before trigger word
                #DOES NOT WORK...PRODUCES EMPTY LIST
                last_words.append(words[index - 1:index - 4])


    return pd.Series([trigger_words, last_words, next_words], index = ['TriggerWords','LastWords', 'NextWords'])

3)此函数使用上面的word_list中的单词来查找之前的word_list "trigger_words"之后的的3个单词

4)然后我使用以下代码

代码语言:javascript
复制
df = df.join(df.apply(lambda x: find_next_words(x, word_list), axis=1))

5),并生成与我想要的非常接近的df

代码语言:javascript
复制
Text                                  TriggerWords LastWords NextWords
0   there are many different crayons    [crayons]   [[]]    [[in, the, bright]]
1   i like a lot of sports cars          [cars]     [[]]    [[because, they, go]]
2   the middle east has many camels     [camels]    [[]]    [[to, ride, and]]  

Problem

6)但是,LastWords列是列表[[]]的空列表。我认为问题出在上面的find_next_words函数中的这一行代码last_words.append(words[index - 1:index - 4])

7)这让我有点困惑,因为NextWords列使用了非常相似的代码next_words.append(words[index + 1:index + 4]),取自find_next_words函数,并且它可以工作。

问题

8)如何修复我的代码,使其不生成列表[[]]的空列表,而是给出word_list中单词之前的3个单词

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-05 08:26:26

我认为在代码中应该是words[max(index - 4, 0):max(index - 1, 0)]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56452788

复制
相关文章

相似问题

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