我想在python中删除字符串中的字符:
string.replace(',', '').replace("!", '').replace(":", '').replace(";", '')...
Run Code Online (Sandbox Code Playgroud)
但是我必须删除许多字符.我想到了一个清单
list = [',', '!', '.', ';'...]
Run Code Online (Sandbox Code Playgroud)
但是我如何使用list替换中的字符string?
假设我有一个string "Hello"和一个列表
words = ['hello', 'Hallo', 'hi', 'house', 'key', 'screen', 'hallo','question', 'Hallo', 'format']
Run Code Online (Sandbox Code Playgroud)
如何找到n words最接近"Hello"列表并出现在列表中的words?
在这种情况下,我们会 ['hello', 'hallo', 'Hallo', 'hi', 'format'...]
因此,策略是将列表单词从最接近的单词排序到最远的单词.
我想过这样的事情
word = 'Hello'
for i, item in enumerate(words):
if lower(item) > lower(word):
...
Run Code Online (Sandbox Code Playgroud)
但是在大型名单中它很慢.
更新
difflib工作,但它也很慢.(words list里面有630000+个单词(排序,每行一个)).因此,每次搜索最接近的单词时,检查列表需要5到7秒!
让模型Quote具有属性[price, description]
让模型Invoice具有属性[price, description, priority]
让invoiceModel中的对象Invoice具有属性{price: 10, description: 'lamp', priority: 10}
invoice = {price: 10, description: 'lamp', priority: 10}
Run Code Online (Sandbox Code Playgroud)
假设我想将invoice属性复制到新的quote.
quote = Quote.new(invoice.attributes)
Run Code Online (Sandbox Code Playgroud)
这会引发priority模型中不存在的错误Quote.
如何将invoice属性复制到新的quote但只能quote接受的属性?
我正在尝试rails 3.2在prawn类中使用帮助器,但是rails会抛出:
undefined method `number_with_precision' for #<QuotePdf:0x83d4188>
Run Code Online (Sandbox Code Playgroud)
虾类
class QuotePdf < Prawn::Document
def initialize(quote)
super()
text "sum: #{number_with_precision(quote.sum)}"
end
end
Run Code Online (Sandbox Code Playgroud)
调节器
def show
@quote = current_user.company.quotes.where(:id => params[:id]).first
head :unauthorized and return unless @quote
respond_with @quote, :layout => !params[:_pjax] do |format|
format.pdf do
send_data QuotePdf.new(@quote).render, filename: "Devis-#{@quote.date_emission.strftime("%d/%m/%Y")}.pdf",
type: "application/pdf"
end
end
end
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助.
保护使用许多不同形式和mongodb作为数据库的Sinatra应用程序的最佳实践是什么?
我需要编写一个查询,查找在指定日期创建的所有文档.
我们假设日期是今天.
我试过这个:
Document.all(:created_at => Date.parse(Time.now.strftime('%Y/%m/%d')))
Run Code Online (Sandbox Code Playgroud)
但我得到了:
无法将类Date的对象序列化为BSON.
谢谢你的帮助.
更新 此链接说明了如何使用MongoMapper进行日期范围查询.
Document.count( :created_at => { '$gt' => 2.days.ago.midnight, '$lt' => 1.day.ago.midnight } )
Run Code Online (Sandbox Code Playgroud) <div id="content">
...
<p>NUMBER times...</p>
...
<p>Place N°: NUMBER</p>
</div>
Run Code Online (Sandbox Code Playgroud)
如何替换内容div中的所有NUMBER?我试过replace方法,但它没有用.
谢谢.
我在$('document').ready.
$('document').ready(function() {
function visit(url) {
$.ajax({
url: url,
container: '#maincontainer',
success: function(data) {
init();
}
});
}
function init() {
...
}
)};
Run Code Online (Sandbox Code Playgroud)
但是当我init()在 Chrome 控制台中调用时,我得到:ReferenceError: init is not defined。
更新:谢谢大家的帮助。我做到了window.init = init;,而且效果很好。