Python 文本中的第 n 个单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4495176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
nth word in a text
提问by user531225
how can I find the nth word in a text.
如何在文本中找到第 n 个单词。
example:
例子:
my_txt("hello to you all" , 3)
all
I don't wanna use any built in function ...and this is not a homework :D
我不想使用任何内置函数......这不是作业:D
采纳答案by John Machin
OK you asked for this. You need a "split into words" function. Here it is. Assumes that "words" are delimited by whitespace.
好的,你要求这个。您需要“拆分成单词”功能。这里是。假设“单词”由空格分隔。
No built-in functions, no imported anythings, no methods of built-in types, not even any panty-waist stuff like +=. And it's tested.
没有内置函数,没有导入任何东西,没有内置类型的方法,甚至没有像+=. 它已经过测试。
C:\junk>\python15\python
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> def mysplit(s):
... words = []
... inword = 0
... for c in s:
... if c in " \r\n\t": # whitespace
... inword = 0
... elif not inword:
... words = words + [c]
... inword = 1
... else:
... words[-1] = words[-1] + c
... return words
...
>>> mysplit('')
[]
>>> mysplit('x')
['x']
>>> mysplit('foo')
['foo']
>>> mysplit(' foo')
['foo']
>>> mysplit(' foo ')
['foo']
>>> mysplit('\nfoo\tbar\rzot ugh\n\n ')
['foo', 'bar', 'zot', 'ugh']
>>>
回答by Lennart Regebro
Since everythingis a built in function in one way or another, I'm gonna ignore your claim to not want to use built in functions.
由于所有东西都以某种方式是内置函数,因此我将忽略您不想使用内置函数的说法。
def my_txt(text, n):
return text.split()[n]
Main drawback with this is that you'll get punctuation included. I leave it as exercise to figure out how to get rid of that. :)
这样做的主要缺点是你会得到标点符号。我把它作为练习来弄清楚如何摆脱它。:)
回答by jsbueno
The obvious way to do it is:
显而易见的方法是:
"hello to you all".split()[3]
The 80's way to do it is - that is, you have to walk the text, keeping note of the state of things you have found - it can become better than this, probably, but that is the idea. Perceive one has to use a lot o "built-in" functions either way. I just avoid the ones that make it straight like above.
80 年代的做法是——也就是说,你必须遍历文本,注意你发现的事物的状态——它可能会变得比这更好,但这就是想法。无论哪种方式,人们都必须使用很多“内置”功能。我只是避免像上面那样直截了当的那些。
def my_txt(text, target):
count = 0
last_was_space = False
start = end = 0
for index, letter in enumerate(text):
if letter.isspace():
if not last_was_space:
end = index
last_was_space = True
elif last_was_space:
last_was_space = False
count += 1
if count > target:
return text[start:end]
elif count == target:
start = index
if count == target:
return text[start:].strip()
raise ValueError("Word not found")
回答by Andrew Clark
First let me say that I absolutely agree with the comments and the other answer, not using built in functions is stupid. That being said, I found that attempting to write this code using as few built in function callsto be an interesting challenge, so I'll post what I came up with anyways.
首先让我说我完全同意评论和其他答案,不使用内置函数是愚蠢的。话虽如此,我发现尝试使用尽可能少的内置函数调用来编写此代码是一个有趣的挑战,因此无论如何我都会发布我的想法。
def my_txt(txt, n, i=0):
if n == 1:
r = ""
s = 0
for c in txt:
if s >= i:
if c == " ":
return r
r += c
s += 1
while txt[i] != " ":
i += 1
return my_txt(txt, n - 1, i + 1)
my_txt("hello to you all", 3) # returns 'you'
My self inflicted rules: no slices, comprehensions, generators, or built in function calls.
我自己造成的规则:没有切片、理解、生成器或内置函数调用。
This code will horribly fail when attempting to get the last word (unless there is a trailing space) or for any nout of the range of words.
当尝试获取最后一个单词(除非有尾随空格)或任何n超出单词范围的单词时,此代码将严重失败。
回答by Zun Gamer
obviously that's not without "built in functions" but it won't give error, no matter how hard u try :)
显然,这并非没有“内置函数”,但无论您多么努力,它都不会出错:)
def my_text(string, integer=None):
try:
integer = int(integer)
except ValueError:
return 'enter a number'
else:
try:
return string.split()[integer - 1]
except IndexError:
string = string.split()
return f"text contains only {len(string)} word"
def inp():
try:
return int(input('Which word you need? ex: 1,2,3 etc.: '))
except ValueError:
return 'Enter number please'
print(my_text(input('Enter text here: '), inp()))

