鱼C论坛

 找回密码
 立即注册
查看: 1413|回复: 9

[已解决]查找每个字符所在的位置

[复制链接]
发表于 2017-8-31 08:50:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
例如,给一段:
It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had
everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way—in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.

如何写一个方程,找到一个特定单词所有前一个单词长度的总和?例如输入 "the", 前面的单词从一开始有 was , was, ... short,.... in. 如何找出这些单词并对长度进行加和呢?

谢谢

最佳答案
2017-8-31 10:53:43
frankzhang57 发表于 2017-8-31 09:59
para = """Call me Ishmael. Some years ago - never mind how long precisely - having little or no mo ...

没那么麻烦的,几句搞定~
  1. >>> def preword(word,txt):
  2.         words_ls = txt.split()
  3.         rslt = [words_ls[i-1]for i,w in enumerate(words_ls)if w==word]
  4.         return len(''.join(rslt)),rslt

  5. >>> preword('the',txt)
  6. (48, ['was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'direct', 'short,', 'like', 'in'])
  7. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-31 09:03:05 | 显示全部楼层
正则表达式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-31 09:08:30 | 显示全部楼层
有没有用一个loop完成的方法?没有学过正则表达式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-31 09:11:47 | 显示全部楼层
frankzhang57 发表于 2017-8-31 09:08
有没有用一个loop完成的方法?没有学过正则表达式

循环每个字符, 以空格分割形成 list, 取目标单词下标的前一个下标组成新 list, 最后对这个 list 进行长度求和
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-31 09:34:23 | 显示全部楼层
  1. >>> txt='''
  2. It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had
  3. everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way—in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.
  4. '''
  5. >>> import re
  6. >>> def findpre(word,txt):
  7.         ptn = re.compile(r'\w+(?=\W+%s)' % word)
  8.         rslt = ptn.findall(txt)
  9.         return len(''.join(rslt)),rslt

  10. >>> findpre('the',txt)
  11. (47, ['was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'direct', 'short', 'like', 'in'])
  12. >>>
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
jerryxjr1220 + 5 + 5 支持楼主!666

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-31 09:59:54 | 显示全部楼层
hldh214 发表于 2017-8-31 09:11
循环每个字符, 以空格分割形成 list, 取目标单词下标的前一个下标组成新 list, 最后对这个 list 进行长度 ...

para = """Call me Ishmael. Some years ago - never mind how long precisely - having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off - then, I account it high time to get to sea as soon as I can. This is my substitute for pistol and ball. With a philosophical flourish Cato throws himself upon his sword; I quietly take to the ship. There is nothing surprising in this. If they but knew it, almost all men in their degree, some time or other, cherish very nearly the same feelings towards the ocean with me."""

def prevword_ave_len(text):
    string = para.split()
    position_list = []
    pre_word_list = []
    for n in range(len(string)):
        if string[n] == text:
            position = int(string.index(string[n]))
            position_list.append(position)
    return position_list

这个是for loop 的一部分,本来想找出所有位置的index, 例如"the", 但是这样一共有10个,显示出来的是[39,39,39,39,39,39,39,39,39,39]。第一个位置应该是39,但是不知道为什么无法找到后面的index. 谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-31 10:01:03 | 显示全部楼层

非常感谢,但是好像看不懂。初学者还没接触到这么多
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-31 10:45:49 | 显示全部楼层
frankzhang57 发表于 2017-8-31 09:59
para = """Call me Ishmael. Some years ago - never mind how long precisely - having little or no mo ...

index 只返回第一个找到的位置
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-8-31 10:53:43 | 显示全部楼层    本楼为最佳答案   
frankzhang57 发表于 2017-8-31 09:59
para = """Call me Ishmael. Some years ago - never mind how long precisely - having little or no mo ...

没那么麻烦的,几句搞定~
  1. >>> def preword(word,txt):
  2.         words_ls = txt.split()
  3.         rslt = [words_ls[i-1]for i,w in enumerate(words_ls)if w==word]
  4.         return len(''.join(rslt)),rslt

  5. >>> preword('the',txt)
  6. (48, ['was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'was', 'direct', 'short,', 'like', 'in'])
  7. >>>
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-31 11:03:48 | 显示全部楼层
SixPy 发表于 2017-8-31 10:53
没那么麻烦的,几句搞定~

好的,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-18 09:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表