我最近了解了collections.Counter()类,因为它是一种简洁(快速??)的计算方法,我开始使用它.
但是我最近在我的程序中发现了一个错误,因为当我尝试用元组更新计数时,它实际上将它视为一个序列并更新元组中每个项目的计数,而不是计算我插入的次数那特定的元组.
例如,如果您运行:
import collections
counter = collections.Counter()
counter.update(('user1', 'loggedin'))
counter.update(('user2', 'compiled'))
counter.update(('user1', 'compiled'))
print counter
Run Code Online (Sandbox Code Playgroud)
你会得到:
Counter({'compiled': 2, 'user1': 2, 'loggedin': 1, 'user2': 1})
Run Code Online (Sandbox Code Playgroud)
结果是.有没有办法用Counter()计算元组?我可以连接字符串,但这是......丑陋.我可以使用命名元组吗?实现我自己非常简单的字典计数器?不知道什么是最好的.
可能是我在恶劣的C++书籍中学习的另一个愚蠢的问题(我打算纠正这个问题).
我正在使用sstream并尝试以下功能:
template <class num> num fromString(const std::string& str) {
std::istringstream ss(str);
num temp;
ss >> temp;
return temp;
}
Run Code Online (Sandbox Code Playgroud)
当我称之为:
int i = fromString<int>("123");
Run Code Online (Sandbox Code Playgroud)
它工作正常.但如果我称之为:
int i = fromString("123");
Run Code Online (Sandbox Code Playgroud)
我收到了一个编译错误:
error: no matching function for call to ‘fromString(std::string&)’
Run Code Online (Sandbox Code Playgroud)
我认为编译器会理解,如果我将值赋给a,int那么我必须谈论fromString<int>,但似乎并非如此.
我错过了什么吗?我应该总是指定模板化函数的类型吗?或者只是模板类型是返回类型?或者只是当模板类型无法通过输入类型确定时?
考虑以下程序:
import Queue
from multiprocessing import Queue as Q
from multiprocessing import Process, Value, Array
from time import time
from numpy import sum, zeros
from numpy.random import normal
def enche(queue, cnts, tims, t0, proc):
while True:
try:
queue.get(False)
x = sum(normal(size = 1000))
cnts[proc] = cnts[proc] + 1
except Queue.Empty:
tims[proc] = time() - t0.value
break
def main(m, nth):
queue = Q()
tmp = [queue.put(0) for i in xrange(m)]
cnts = Array('i', zeros(nth, dtype = "int"))
tims = Array('d', zeros(nth)) …Run Code Online (Sandbox Code Playgroud) 我刚刚在Linux Mint 12上安装了一个新的haskell平台安装程序apt-get.每次我尝试使用cabal-install安装一些hackage包时,我得到一个:
couldn't read caba file xxxx.cabal
Run Code Online (Sandbox Code Playgroud)
其中xxxx是我正在安装的软件包或软件包本身的依赖项.基于haskell cafe上的这个帖子和SO中的其他问题,我从索引中删除了bytestring包:
tar -f ~/.cabal/packages/hackage.haskell.org/00-index.tar --delete bytestring/0.9.2.0
tar -f ~/.cabal/packages/hackage.haskell.org/00-index.tar --delete bytestring/0.9.2.1
Run Code Online (Sandbox Code Playgroud)
但错误仍然存在.
我的cabal安装版本是:
$ cabal --version
cabal-install version 0.10.2
using version 1.10.1.0 of the Cabal library
Run Code Online (Sandbox Code Playgroud)
错误是这样的:
$ cabal install yesod
Resolving dependencies...
cabal: Couldn't read cabal file "fsnotify/0.0.5/fsnotify.cabal"
Run Code Online (Sandbox Code Playgroud)
有谁知道可能会发生什么?
我有一个doc_type,其映射类似于这个非常简化的映射:
{
"test":{
"properties":{
"name":{
"type":"string"
},
"long_searchable_text":{
"type":"string"
},
"clearances":{
"type":"object"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
该字段clearances应该是一个对象,带有一系列字母数字标识符,用于过滤目的.典型的文档将具有以下格式:
{
"name": "Lord Macbeth",
"long_searchable_text": "Life's but a walking shadow, a poor player, that..."
"clearances": {
"glamis": "aa2862jsgd",
"cawdor": "3463463551"
}
}
Run Code Online (Sandbox Code Playgroud)
问题在于,有时在索引期间,对象字段内的新字段的第一个索引内容clearances将完全是数字的,如上面的情况.这会导致Elasticsearch将此字段的类型推断为long.但这是一个意外.该字段可能是另一个文档中的字母数字.当该字段中包含字母数字值的后一个文档到达时,我得到一个解析异常:
{"error":"MapperParsingException[failed to parse [clearances.cawdor]]; nested: NumberFormatException[For input string: \"af654hgss1\"]; ","status":400}%
Run Code Online (Sandbox Code Playgroud)
我尝试用这样定义的动态模板来解决这个问题:
{
"test":{
"properties":{
"name":{
"type":"string"
},
"long_searchable_text":{
"type":"string"
},
"clearances":{
"type":"object"
}
}
},
"dynamic_templates":[
{
"source_template":{
"match":"clearances.*",
"mapping":{
"type":"string",
"index":"not_analyzed"
}
} …Run Code Online (Sandbox Code Playgroud) 想象一下,我有以下html:
<div id='0'>
stuff here
</div>
<div id='1'>
stuff here
</div>
<div id='2'>
stuff here
</div>
<div id='3'>
stuff here
</div>
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法可以使用BeautifulSoup 提取div具有该属性的所有属性id,而与其值无关?我意识到用xpath做这件事是微不足道的,但似乎没有办法在BeautifulSoup中进行xpath搜索.
我是一个全新的C++,我有一个非常愚蠢的问题.
我有一个Graph类,我需要为它创建一个复制构造函数.这是我的班级:
#include <igraph.h>
#include <iostream>
using namespace std;
class Graph {
public:
Graph(int N); // contructor
~Graph(); // destructor
Graph(const Graph& other); // Copy constructor
igraph_t * getGraph();
int getSize();
private:
igraph_t graph;
int size;
};
Run Code Online (Sandbox Code Playgroud)
有一个功能int igraph_copy(igraph_t * to, const igraph_t * from),igraph.h可以igraph_t充分复制一种类型.
构造函数和析构函数是微不足道的,并且工作正常,我有以下复制构造函数:
Graph :: Graph(const Graph& other) {
igraph_t * otherGraph = other.getGraph();
igraph_copy(&graph, otherGraph);
size = other.getSize();
}
igraph_t * Graph :: getGraph(){
return &graph;
}
int Graph :: …Run Code Online (Sandbox Code Playgroud) 我对C++有些新意,所以我想这是一个非常基本的问题.
假设我有这个课程:
// file Graph.h
class Graph {
public:
Graph(int N); // contructor
~Graph(); // destructor
Graph& operator=(Graph other);
private:
int * M;
int N;
};
// file Graph.cpp
Graph :: Graph(int size) {
M = new int [size];
N = size;
}
Graph :: ~Graph() {
delete [] M;
}
Run Code Online (Sandbox Code Playgroud)
我想创建一个赋值运算符,它将复制数组M [] 的内容,但是当我在复制后更改它时不会覆盖它(我认为这是通过不复制实际指针但只复制内容来实现的,不知道如果我是对的).这就是我尝试过的:
Graph& Graph::operator=(Graph other) {
int i;
N = other.N;
M = new int [N];
for (i = 0; i < N; i++)
M[i] …Run Code Online (Sandbox Code Playgroud) 在python中解析由空格分隔的浮点数列表的最佳原因是什么?
我有这样的行将来自一个来源:
string = " 4 1.6 8.29 0 0 3.55e-15 -1.28e-13 "
Run Code Online (Sandbox Code Playgroud)
具有未知数量的空格,用于分隔数字以及字符串的开头或结尾.
我通常只是numbers = map(float, string.split(" "))在我可以保证数字之间只有一个空格时使用,并且在字符串的开头或结尾没有空格.我不熟练正则表达式,但有人建议我使用,re.split("\s+", string)但这不起作用,因为有时我在结果列表的开始时得到空字符串.
我现在正在使用:re.split("\s+")但是这不起作用,因为有时我在结果列表的开头有空字符串.
我现在在用:
res = map(float, re.findall("\d+\S*\d*", string)
Run Code Online (Sandbox Code Playgroud)
这是以某种方式工作,但对我来说看起来很脆弱和丑陋.它可以匹配许多无法生成数字的字符串.
什么是最好的正则表达式模式,它总是匹配带有或不带指数表示法的整数和浮点数,以便我可以使用re.findall(patt, string)并安全地恢复数字列表?
我正在尝试学习 Scala,并认为我会从阅读“不耐烦的 Scala”开始。在那里,他通过使用以下类引用了构造顺序问题:
class Animal {
val range: Int = 10
val env: Array[Int] = new Array[Int](range)
}
class Ant extends Animal {
override val range: Int = 2
}
Run Code Online (Sandbox Code Playgroud)
然后他解释了为什么env最终是一个空的 Array[Int] 并继续解释防止这种情况的方法,包括早期定义语法。
但是......我不能通过这样做来阻止它:
class Animal(val range: Int = 10) {
val env: Array[Int] = new Array[Int](range)
/* do animal stuff */
}
class Ant(override val range: Int = 2) extends Animal(range) {
/* do ant stuff */
}
Run Code Online (Sandbox Code Playgroud)
???为什么早期定义语法真的很有必要?
python ×4
c++ ×3
parsing ×2
cabal ×1
collections ×1
const ×1
constructor ×1
counter ×1
html-parsing ×1
inheritance ×1
multicore ×1
operators ×1
regex ×1
scala ×1
schemaless ×1
syntax ×1
templates ×1
types ×1
xpath ×1