小编Wag*_*agh的帖子

如何基于python中的列表对字典进行排序

我有一本字典

a = {'ground': obj1, 'floor 1': obj2, 'basement': obj3}
Run Code Online (Sandbox Code Playgroud)

我有一份清单.

a_list = ['floor 1', 'ground', 'basement']
Run Code Online (Sandbox Code Playgroud)

我想基于列表使用其键对字典a进行排序.有可能吗?

即:

sort(a).based_on(a_list) #this is wrong. But I want something like this. 
Run Code Online (Sandbox Code Playgroud)

输出不必是另一个字典,我不介意将字典转换为元组然后对它们进行排序.

python sorting dictionary list

14
推荐指数
2
解决办法
4964
查看次数

在 Django 类基本视图中创建自定义方法

我想使用 django 1.9 使用通用类基视图 我想理解的是

from django.views.generic import CreateView
from braces.views import LoginRequiredMixin
from .models import Invoice

class InvoiceCreateView(LoginRequiredMixin,CreateView):
    model = Invoice

    def generate_invoice(self):
        ...
        return invoice
Run Code Online (Sandbox Code Playgroud)

现在我想将此自定义方法绑定到 url。我怎样才能做到这一点?我知道使用函数基视图很简单,但我想使用类基视图来做到这一点。

帮助将不胜感激。

python django django-class-based-views

6
推荐指数
1
解决办法
6911
查看次数

Highlight specific words in textarea angular 8

I am trying to select a few words from the textarea and create bootstrap chips.

在此处输入图片说明

I am able to create the chips for selected words. I am trying to highlight the selected words with different background colors.

export class SearchComponent implements OnInit {

  constructor() { }

  selectedText = [];
  regexFromMyArray: RegExp;

  // tslint:disable-next-line:typedef
  showSelectedText() {
    let text = '';
    if (window.getSelection) {
      text = window.getSelection().toString();
      if (text) {
        // console.log(this.selectedText.includes(text));
        if (this.selectedText.includes(text) === false)
        {
          this.selectedText.push(text);
          this.regexFromMyArray = new RegExp(this.selectedText.join('|'), …
Run Code Online (Sandbox Code Playgroud)

javascript textarea angular angular8

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