如何获取文本中出现的所有单词

用户希望将CSV文件中的所有单词首字母转为小写,并创建一个包含所有不重复单词的列表。已经实现了将单词转小写,但无法生成唯一的单词列表。提供的代码尝试通过检查结果列表来去重,但并未达到预期效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【问题】

I have this csv file

Cat, and, dog, bites

Yahoo, news, claims, a, cat, mated, with, a, dog, and, produced, viable, offspring

Cat, killer, likely, is, a, big, dog

Professional, free, advice, on, dog, training, puppy, training

Cat, and, kitten, training, and, behavior

Dog, &, Cat, provides, dog, training, in Eugene, Oregon

Dog, and, cat, is, a, slang, term, used, by, police, officers, for, a, male-female, relationship

Shop, for, your, show, dog, grooming, and, pet, supplies

I want to make all the words start with a small letter and create a list which will include all the unique items from the above csv file. Have you any idea? Thanks in advance! So far, I have managed to convert all the words with a small letter:

unique_row_items = set([field.strip().lower()for field in row])

But i can't manage the other one.

def unique():

    rows = list(csv.reader(open('example_1.csv', 'r'), delimiter=','))

    result = []

    for r in rows:

        key = r

        if key not in result:

            result.append(r)

    return result

Which does not give the results I want

【回答】

这个问题用SPL实现很方便,脚本如下:

A
1= file("D://Words.txt").read()
2=lower(A1)
3=A2.words().id()

A1:从文本读取字符串

A2:将字符串转换成小写字母

A3:通过 words 函数将字符串拆分成单个单词组成的序列,再通过 id 函数去除重复成员

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值