所以我试图从随机文章中处理整个段落。细节很乏味,但有一件事让我感到困惑。
这是我的代码:
def prevword_ave_len(word):
count = 0
wordlength = 0
mystr = "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."
l1 = mystr.split()
s1= list()
#print(l1)
if word in l1:
if l1.index(word) == 0:
return 0
else:
for element in l1:
s1.append(l1[l1.index(word) - 1]) #get that word to s1 list for future use
l1.pop(l1.index(word)) # delete the occurrence so that it will not mess up later on in this loop.
#print(s1)
else:
return False
我的目标是确定这个巨大的单词列表中是否存在单词。然而,当我试图测试它时,似乎出现了问题,在我对代码进行了大约两个小时的痛苦审查后,我无法弄明白。
我的错误是当我尝试这个时:
prevword_ave_len('the')
Python向我返回False
而不是'the'
的真实索引。你可以看到我试图得到那个索引,然后尝试找到其余的索引,这样我就可以在他们面前得到这个词并做blablabla。但那不是我现在被困住的重点。有人可以指出我做错了什么吗?
ERROR
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "program.py", line 14, in prevword_ave_len
s1.append(l1[l1.index(word)])
ValueError: 'the' is not in list
答案 0 :(得分:1)
Youse if y in y:
method
您可能希望先用空格
替换某些字符(例如, - - :;)或者您可以使用
private Map<Integer, List<List<MyObject>>> paginateDataRequest(List<List<MyObject>> requestLists, double pageSize) {
Map<Integer, List<List<MyObject>>> result = new LinkedHashMap<Integer, List<List<MyObject>>>();
int totalElements = 0;
//We calculate the total of the elements contained in the requestLists.
for(List<MyObject> subList : requestLists) {
if(subList != null) {
totalElements += subList.size();
}
}
//We round it up. The result Map will contain x pages with {pageSize} elements each one. For example, if the total amount of request is 101,
//our Map will have 2 pages (100 elements + 1 element)
int totalRequests = (int)Math.ceil(totalElements / pageSize);
//We iterate over each page
for(int i=0; i<totalRequests; i++) {
List<List<MyObject>> entry = new LinkedList<List<MyObject>>();
int freeElements = (int)pageSize;
for(List<MyObject> list : requestLists) {
List<MyObject> subList = new LinkedList<MyObject>();
if(freeElements > 0) {
if(list.size() > freeElements) {
subList.addAll(list.subList(0, freeElements));
}else {
subList.addAll(list);
}
//We update the left free elements
freeElements -= subList.size();
}
entry.add(subList);
list.removeAll(subList);
}
//We add a new page to the result Map
result.put(i, entry);
}
return result;
}
这将返回0或找到单词的索引。
答案 1 :(得分:0)
当在第一个位置找到单词时,此代码返回0,当在段落中找不到单词时返回False,而在所有其他情况下都不返回任何内容。没有返回语句实际返回索引。
试试这个:
def prevword_ave_len(word):
mystr = "Call me Ishmael. [...] ocean with me."
# Convert the string to an array of words
l1 = mystr.split()
# 'word' has been found in 'mystr'
if word in l1:
# return the index of 'word' in 'l1'
return l1.index(word)
else:
return False
此外,您循环遍历列表中的每个元素,并从列表l1中删除搜索到的单词以将其放入列表s1中。因此,当你的循环到达下一个元素时,它会尝试执行l1.index(word),但是&#39; word&#39;已被删除(使用list.pop())从上一步的列表中删除。这就是为什么你会收到类似&#34; ValueError:&#39;&#39;不在列表中#34;。
答案 2 :(得分:0)
这似乎是一种更简单的做事方式:
def prevword_ave_len(word):
mystr = "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."
l1 = mystr.split()
s1 = list()
if not word in l1:
return False
while word in l1:
prevword = l1.pop(l1.index(word) - 1)
s1.append(prevword) #get that prevword to s1 list for future use
l1.pop(l1.index(word)) # remove that instance of word
return sum(len(w) for w in s1) / len(s1) # remember to use float(len(s1)) for Python 2.x
print prevword_ave_len('the')