【问题标题】:Function to conditionally split values of a list有条件地拆分列表值的函数
【发布时间】:2021-09-29 00:36:20
【问题描述】:

我尝试将使用 selenium 导出的值拆分为 excel,同时考虑文章和任何介词。 例如我的清单是:

Neon Five Apple A incandescence The book

我想使用:

for l in lst:
    list_elem.send_keys(l)

Bul 将采用如下值:

Neon,Five,Apple,A,incandescence,The,book

发送密钥时我想得到的是:

Neon,Five,Apple,A incandescence,The book

在发送密钥之前我可以在这里添加什么吗?

df = pd.DataFrame({'col1': [1], 'col2': ['value4, value5, value6']})

【问题讨论】:

  • 你能发布示例数据框吗?
  • @Anurag Dabas,完成,它从 col2 的第一行获取值
  • 这个问题的答案有帮助吗?如果您觉得有帮助,请花点时间投票和/或将其标记为答案。

标签: python pandas selenium split


【解决方案1】:

我认为 ntlk 可以在这里使用。这将取决于您的 df.xml 中包含的实际数据。我生成了一些随机的英文 lorum ipsum。

然后您使用 nltk 对单词进行标记和标记。然后使用函数中if 语句中的标签将它们添加到下一个单词中。您可以找到标签列表here。你需要玩一点才能得到你想要的确切结果。

我还提供了一种在其中放置手动排除项的方法,主要用于标点符号。

import nltk
from pprint import pprint


# download data sets
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')


dummy_text = '''The European languages are members of the same family.
Their separate existence is a myth.
For science, music, sport, etc, Europe uses the same vocabulary.
The languages only differ in their grammar, their pronunciation and their most common words.
Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.
To achieve this, it would be necessary to have uniform grammar, pronunciation and more common words.
If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual languages.
The new common language will be more simple and regular than the existing European languages.
It will be as simple as Occidental; in fact, it will be Occidental.
To an English person, it will seem like simplified English, as a skeptical Cambridge friend of mine told me what Occidental is.
The European languages are members of the same family.
Their separate existence is a myth.
For science, music, sport, etc, Europe uses the same vocabulary.
The languages only differ in their grammar, their pronunciation and their most common words.
Everyone realizes why a new common language would be desirable: one could refuse to pay expensive translators.
'''


# use only lowercase words in this exception list
my_exceptions = ['a']
ignore_tags = '.,;:'


def create_list(text):
    tokens = nltk.word_tokenize(text)
    # see https://www.ling.upenn.edu/courses/Fall_2003/ling001/penn_treebank_pos.html
    tagged = nltk.pos_tag(tokens)
    pprint(tagged)

    results = []
    current = ''
    for word, nltktag in tagged:
        if (
            nltktag == 'DT' or 
            nltktag == 'VB' or
            word.lower() in my_exceptions
        ):
            current = f'{current}{word} '
        elif nltktag not in ignore_tags:
            results.append(f'{current}{word}')
            current = ''
    return results


pprint(create_list(dummy_text))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 2014-03-05
    • 2022-11-18
    相关资源
    最近更新 更多