小编cod*_*nce的帖子

python乘以两个集合计数器

Python集合计数器好奇,如果有更好的方法来做到这一点.重写Counter类方法?内置乘法产生两个计数器的点积

from collections import Counter
a = Counter({'b': 4, 'c': 2, 'a': 1})
b = Counter({'b': 8, 'c': 4, 'a': 2})    
newcounter = Counter()
for x in a.elements():
    for y in b.elements():
        if x == y:
             newcounter[x] = a[x]*b[y]

$ newcounter
Counter({'b': 32, 'c': 8, 'a': 2})
Run Code Online (Sandbox Code Playgroud)

python collections counter dot-product python-collections

5
推荐指数
1
解决办法
804
查看次数

Rails 嵌套路线的简单形式

simple_form_for我正在尝试在 Rails 3.2 中构建一个@objects具有双重嵌套路线的 for ,例如:

/users/1/projects/2/objects
Run Code Online (Sandbox Code Playgroud)

形式为:

<%= simple_form_for @object, :url => user_project_objects_path(@user, @project), :html => { :class => 'form-horizontal' } do |f| %>
Run Code Online (Sandbox Code Playgroud)

在routes.rb中:

resources :users do
  resources :projects do
    resources :objects do
      collection { post :import }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我的问题是:控制器的new和动作是什么?createObject

到目前为止 - 我遇到了路由错误 - 我有:

def create
  @user = current_user
  @project = Project.find_by_user_id(@user)
  @object = @project.objects.build(params[:object])
  if @object.save
    flash[:notice] = "Object was successfully created"
    redirect_to user_project_objects_path
  else
    render 'new' …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-routes simple-form

4
推荐指数
1
解决办法
4711
查看次数

通过Web3获得ETH FIAT转换率

我是否遗漏了某些内容,或者即使在Metamask中显示ETH USD费率,也无法通过浏览器中的web3获取此信息.

在文档中找不到任何内容.

是使用交叉加密API的标准做法?

ethereum web3js

3
推荐指数
1
解决办法
1381
查看次数

Django 模板过滤器:将 floatformat 应用于 widthratio

widthformat 自动四舍五入。但是,如果可能,我想在模板标记中执行除法并四舍五入到 n 位小数。例如:

    <h4>Strike Rate: {% widthratio selected_replies user.projectreply_set.count 100 %}</h4>
Run Code Online (Sandbox Code Playgroud)

目前它返回一个整数。

我将如何在此处应用 floatformat,或者我是否需要在视图中执行此工作?

使用模型的替代方法

class UserProfile(models.Model):
    ....
    ....
    def get_strike_rate(self):
        selected_replies = self.user.projectreply_set.filter(is_selected_answer=True).count()
        my_replies = self.user.projectreply_set.count()
        if my_replies >0:
             return round((selected_replies/my_replies)*100.0,2)
        else:
             return 0
Run Code Online (Sandbox Code Playgroud)

django django-templates

2
推荐指数
1
解决办法
2332
查看次数