鉴于以下列表
['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats',
'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and',
'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.',
'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats',
'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise',
'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle',
'Moon', 'to', 'rise.', '']
Run Code Online (Sandbox Code Playgroud)
我试图计算每个单词出现的次数并显示前3个.
但是我只想找到第一个字母大写的前三个,并忽略所有没有首字母大写的单词.
我相信有比这更好的方法,但我的想法是做以下事情:
我正在使用 R 使用“optim”函数来优化函数。然而,我正在优化的变量的真实值间隔至少 10^-5 左右。但是,据我了解,默认步长(即向每个控制变量添加多少 optim 以查看如何改变目标函数)约为 10^-8。
有没有简单的方法告诉“optim”函数将步长增加到 10^5 或更高?
作为参考,我的代码在这里:
Optimal <- optim(par = starting, fn =expectedSeats,
propensities = propsShocked, n = NumberofDistricts,
shockType = "normal", shockSD = 0.1,
method = "L-BFGS-B",
lower = rep(0,NumberofDistricts), upper = rep(1,NumberofDistricts),
control=list(factr = 1e12)
)
Run Code Online (Sandbox Code Playgroud)
我环顾四周,似乎无法弄清楚这一点。谢谢!
嗨,按照早先的帖子.
鉴于以下列表:
['Jellicle','猫','是','黑','和','白色','Jellicle','猫','是','宁可','小;','Jellicle' ,'猫','是','快乐','和','明亮','和','愉快','到','听','什么时候','他们','caterwaul'. ,'Jellicle','猫','有','开朗','面孔','Jellicle','猫','有','明亮','黑','眼睛;','他们' ,'喜欢','到','练习','他们','空气','和','增添','和','等','为',''','Jellicle','月亮','到','上升.','']
我试图计算每个出现在资本中的单词出现多少次并显示前三个单词.
我对那些不以资本开头的话语感兴趣.
如果一个单词出现多次,有时以资本开头而有时不出现,只计算它与资本的次数.
这就是我的代码目前的样子:
words = ""
for word in open('novel.txt', 'rU'):
words += word
words = words.split(' ')
words= list(words)
words = ('\n'.join(words)).split('\n')
word_counter = {}
for word in words:
if word in word_counter:
word_counter[word] += 1
else:
word_counter[word] = 1
popular_words = sorted(word_counter, key = word_counter.get, reverse = True)
top_3 = popular_words[:3]
matches = []
for i in range(3):
print word_counter[top_3[i]], top_3[i]
Run Code Online (Sandbox Code Playgroud)