小编bra*_*orm的帖子

特殊情况的4字母单词的正则表达式

我对正则表达式感兴趣,因为它找到了以下模式.

I或E表示第1个字母,N或S表示第2个,F表示第3个,第4个表示J或P表示.

这将识别ISTJ,ESTP,ENTP但不识别EJPT.

谢谢

php regex

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

在javascript中这种函数赋值给变量有用吗?

我正在读这本书.Javascript,道格拉斯·克罗克福德的优秀作品.书中提供了一些例子,但我无法理解这些例子在实践中的用处和方式.为简单起见,我在这里修改了代码.这里有两种方法,我可以对变量进行函数赋值.

例1:

var test= function(ex) {
    alert(ex);
};
test(5);
Run Code Online (Sandbox Code Playgroud)

这会生成值为5的警报框

例2:

var test1 = function test2(ex) {
    alert(ex);
};
test1(7); //this produces alert box with value of 7
test2(8)//this does not give a alert box
Run Code Online (Sandbox Code Playgroud)

我已经定义了函数test2但是将它分配给了test1.为什么我不能通过调用test2(8)直接访问test2.此外,我没有看到示例2中的任何大优势超过示例1.如果您有一些差异,其中一个是优越的,我想听到.

谢谢

javascript function object

0
推荐指数
2
解决办法
135
查看次数

为什么italor对象不是用Java中的列表集合的new关键字创建的

我遇到了这段代码,从给定的链表中删除了偶数长度的字符串我不明白为什么迭代器对象itr没有用new关键字实例化.这是代码..

public static void removeEvenLength(List<String> list) {
     Iterator<String> itr= list.iterator();
     while (itr.hasNext()) {
         String element=itr.next();
         if (element.length()%2==0) {
             i.remove();
     }
   }
}       
Run Code Online (Sandbox Code Playgroud)

这是否意味着迭代器方法是static,它只返回一个新iterable objectlistas作为它field.有人可以提供一个或多个例子,其中我在Java中遇到类似的实例化方法而不是单例构造函数.谢谢

java iterator object instance new-operator

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

Java中用于生产者 - 消费者代码的mutithreading没有提供正确的输出?

我正在使用多线程来解决经典的生产者 - 消费者问题.我正在使用wait()notifyAll()在代码中.我的问题是,当notifyAll通知其他等待线程恢复时,它不会立即恢复.这是为什么?代码如下

public class ConsumerProducer {

 private  int count;

 public synchronized void consume() {
  while (count == 0) {  // keep waiting if nothing is produced to consume
   try {
    wait(); // give up lock and wait
   } catch (InterruptedException e) {
    // keep trying
   }
  }

  count--;              // consume
  System.out.println(Thread.currentThread().getName() + " after consuming " + count);
 }

 public synchronized void produce() {
  count++;             //produce
  System.out.println(Thread.currentThread().getName() + " after producing " + …
Run Code Online (Sandbox Code Playgroud)

java multithreading notify wait

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

在python中使用"all"和"any"关键字会产生意外输出?

我有以下代码来计算素数

def isPrime(n):
   if (n==2):
      return True
   elif n<=1 or n%2==0:
      return False
   else:
      for i in xrange(3,n/2, 2):
          if n%i:
            return False
   return True


mylist = [6,9]

mylist2= [3,5,7,11,12]

if not any(isPrime(x) for x in mylist):
       print "No primes in list"

if not all(isPrime(x) for x in mylist2):
       print "Not all are primes numbers"
Run Code Online (Sandbox Code Playgroud)

当我运行这个程序时,我明白了

python calculate_primes.py 
Not all are primes numbers
Run Code Online (Sandbox Code Playgroud)

我没有得到的输出No primes in list.但如果我删除元素9mylist,只有有6,它工作正常.

python calculate_primes.py 
No primes in …
Run Code Online (Sandbox Code Playgroud)

python if-statement any

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

有没有一种方法可以在Java中的BitSet中打开位数?

我希望在a中打开位数BitSet.这是一个计算偶数的程序.当然,有更简单的方法来计算偶数,这只是为了理解如何使用BitSets.这是代码:

Public class Test {

public static void main(String[] args) {


        BitSet b = new BitSet();
        for (int i=0; i<10;i++){
            b.set(i);
        }
        System.out.println(b);
        System.out.println("even numbers ");
        int i =0;
        while(i<10){
            if (i%2!=0){
                b.clear(i);
            }
            i++;
        }
        System.out.println(b);
        System.out.println(b.length());
    }
}

output:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
even numbers 
{0, 2, 4, 6, 8}
9
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以打开比特数,例如,在上面的例子中它应该是5.我总是可以遍历BitSet并检查是否(b.set(i))会这样o(n).有没有更快的方法来获得开启位数?

谢谢

java variable-length bitset

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

在python中实现自定义类的add和iadd?

我正在编写一个Queue包含大多数操作列表的类.但是我不会从中升级list,因为我不想提供所有的list API's.我的代码粘贴在下面.该add方法似乎工作正常,但iadd似乎出错,它打印没有.这是代码:

import copy
from iterator import Iterator
class Abstractstruc(object):
    def __init__(self):
        assert False
    def __str__(self):
        return "<%s: %s>" %(self.__class__.__name__,self.container)

class Queue(Abstractstruc,Iterator):

    def __init__(self,value=[]):
        self.container=[]
        self.size=0
        self.concat(value)

    def add(self, data):
            self.container.append(data)
    def __add__(self,other):
        return Queue(self.container + other.container)


    def __iadd__(self,other):
        for i in other.container:
            self.add(i)

    def  remove(self):
        self.container.pop(0)


    def peek(self):
        return self.container[0]


    def __getitem__(self,index):
        return self.container[index]


    def __iter__(self):
        return Iterator(self.container)

    def concat(self,value):
        for i in value:
            self.add(i)

    def __bool__(self):
        return len(self.container)>0 …
Run Code Online (Sandbox Code Playgroud)

python class add object in-place

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

system.exit()在Java中的用途在哪里?

看来return status code从你的角度来看的一种方式main.但我想知道这int是怎么回事?我在eclipse上尝试过,但是我没有在控制台上看到它.我怎么得到的status code

public static void main(String[] args){
         int status = 123;
          System.exit(status);
     }
Run Code Online (Sandbox Code Playgroud)

java program-entry-point exit system.exit

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

django以表单的形式获取登录用户以构建queryset?

我有这个模型:

class Searches(models.Model):
    user = models.ForeignKey(User)
    .....other fields
Run Code Online (Sandbox Code Playgroud)

每个user都有某些保存的搜索。我想显示一个form(a choiceSelect widget)以选择多个搜索之一,然后单击go。我只想filterform中显示searches特定的User。这就是我尝试过的。但request.user下面不起作用。如何解决这个问题?

这是我的表格:

class SearchesForm(forms.Form):
    #get the queryset for the searches model of logged in user
    qset=Searches.objects.filter(user=request.user) ----> error
    #get the most recent entry
    default=Searches.objects.latest('id')
    choices = forms.ModelChoiceField(queryset=qset,label='select search',initial=default)
Run Code Online (Sandbox Code Playgroud)

编辑:我的看法:

def display_search_user(request):
    form=SearchesdiffForm(request)
    if request.method=='POST':
        search=SearchesdiffForm(request,data=request.POST)
        print search
        return HttpResponse(search)
    return render(request,'search_list.html',{'form':form})
Run Code Online (Sandbox Code Playgroud)

django django-models django-forms django-users

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

如何使用 Jquery 将新的 div 覆盖在整个 HTML 正文上?

我遇到了与此处发布的类似问题。。在Ajax请求期间,我试图放置加载图标和灰色背景的背景图像,不知何故,灰色背景不会延伸到页面底部。当我滚动时,它就消失了。我尝试了该帖子中建议的补救措施,但没有任何帮助。我想重新插入一个全新的 div,覆盖整个 html 正文,并使用加载图标将背景灰色化。但我真的不知道该怎么做。所以问题是使用Jquery

  1. 我的 HTML 主体包含各种元素。

  2. 使当前内容不可见或变灰,放置一个新的 div 覆盖整个 HTML 正文(基本上在当前 HTML 正文的顶部)并用灰色背景填充它

有什么帮助吗?

html css jquery

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

为什么来自Guava的MultiMap的size方法返回(un)预期结果?

我使用MultiMapGuava库.我创建了一个示例MultiMap.当我打印地图的大小时,我得到4但实际值(我认为)应该是2.为什么这个差异?

   import java.util.Collection;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

public class MultiMapTest {
    public static void main(String... args) {
  Multimap<String, String> myMultimap = ArrayListMultimap.create();

  // Adding some key/value
  myMultimap.put("Fruits", "Bannana");
  myMultimap.put("Fruits", "Apple");
  myMultimap.put("Fruits", "Pear");
  myMultimap.put("Vegetables", "Carrot");

  System.out.println(myMultimap);

  // Getting the size
  int size = myMultimap.size();
  System.out.println(size);  // 4

  // Getting values
  Collection<String> fruits = myMultimap.get("Fruits");
  System.out.println(fruits); // [Bannana, Apple, Pear]

  Collection<String> vegetables = myMultimap.get("Vegetables");
  System.out.println(vegetables); // [Carrot]

 }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Fruits=[Bannana, Apple, Pear], Vegetables=[Carrot]}
4
[Bannana, …
Run Code Online (Sandbox Code Playgroud)

java map guava

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

给定矩阵中最大孤岛的算法

给定2×2矩阵,返回可能的不同岛尺寸

例如,应返回以下矩阵[5, 7].

  1 0 0 0 1
  1 1 1 1 1
  0 0 0 0 0
  1 1 1 1 1
Run Code Online (Sandbox Code Playgroud)

这是一个相当简单的问题.我使用相同大小的布尔访问矩阵并以DFS方式遍历矩阵.我在这里实现了它.出于某种原因,我得到了输出[1].我试过调试,但我的思绪现在停止了工作.我相信我错过了一些愚蠢的东西.

public class IslandConnectedCell {

    public static void main(String[] args) {

        int[][] input = {
                {1,0,0,0,1},
                {1,1,1,1,1},
                {0,0,0,0,0},
                {1,1,0,1,1}
        };

        dfsIsland(input);

    }


    public static void dfsIsland(int[][] input) {
        int rows = input.length;
        int cols = input[0].length;
        List<Integer> countList = new ArrayList<>();

        boolean visited[][] = new boolean[rows][cols];

        for (int row = 0; row < …
Run Code Online (Sandbox Code Playgroud)

java algorithm depth-first-search data-structures

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

如何从python中的url请求将unicode文件(jpg)写入另一个文件

我试图.jpg使用requestspython中的模块从URL 下载文件.这是我试过的.没有错误.但是我无法打开输出文件.

>>> import requests
>>> l = requests.get("http://www.mosta2bal.com/vb/imgcache/2/9086screen.jpg")
>>> l
<Response [200]>
>>> l.text
u'\ufffd\ufffd\ufffd\ufffd\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\ufffd\ufffd\x12EExif\x00\x00MM\x00*\x00\x00\x00\x08\x00\x07\x01\x12\x00\x03\x......long text
>>> l.encoding
>>> import codecs
>>> f = codecs.open('out.jpg', mode="w", encoding="utf-8")
>>> f.write(l.text)
Run Code Online (Sandbox Code Playgroud)

python unicode file-io python-requests

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