我想知道是否有一种从字符串中调用函数的简单方法.我知道一种简单的方法,使用'if'和'else'.
int function_1(int i, int j) {
return i*j;
}
int function_2(int i, int j) {
return i/j;
}
...
...
...
int function_N(int i, int j) {
return i+j;
}
int main(int argc, char* argv[]) {
int i = 4, j = 2;
string function = "function_2";
cout << callFunction(i, j, function) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是基本方法
int callFunction(int i, int j, string function) {
if(function == "function_1") {
return function_1(i, j);
} else if(function == "function_2") { …Run Code Online (Sandbox Code Playgroud) 我正在研究网络中的检测社区.
我用的是igraph和Python
对于模块化度量方面的最佳社区数量:
from igraph import *
karate = Nexus.get("karate")
cl = karate.community_fastgreedy()
cl.as_clustering().membership
Run Code Online (Sandbox Code Playgroud)
为供应所需数量的社区:
from igraph import *
karate = Nexus.get("karate")
cl = karate.community_fastgreedy()
k=2
cl.as_clustering(k).membership
Run Code Online (Sandbox Code Playgroud)
但是,我喜欢使用networkx这样做.我知道在模块化度量方面获得最佳社区数量:
import community # --> http://perso.crans.org/aynaud/communities/
import fastcommunity as fg # --> https://networkx.lanl.gov/trac/ticket/245
import networkx as nx
g = nx.karate_club_graph()
partition = community.best_partition(g)
print "Louvain Modularity: ", community.modularity(partition, g)
print "Louvain Partition: ", partition
cl = fg.communityStructureNewman(g)
print "Fastgreed Modularity: ", cl[0]
print "Fastgreed Partition: ", cl[1]
Run Code Online (Sandbox Code Playgroud)
但我无法获得所需数量的社区.使用Networkx有一些算法吗?
如何从顺序值中快速创建矢量
例如.:
vector<int> vec (4, 100);
for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
cout << *it << endl;
}
Run Code Online (Sandbox Code Playgroud)
日期:
# 100
# 100
# 100
# 100
Run Code Online (Sandbox Code Playgroud)
我想要
vector<int> vec (100, "0 to N");
Run Code Online (Sandbox Code Playgroud)
我想知道实现这一结果的最有效方法.例如,不使用循环.
它是一个运行时变量.
从一个有向图上,给出两个顶点(v,u),我需要找到:共同的“外”邻居和共同的“内”邻居。
例如:
import networkx as nx
ghybrid = nx.DiGraph()
ghybrid.add_edge("A", "B")
ghybrid.add_edge("A", "C")
ghybrid.add_edge("B", "D")
ghybrid.add_edge("D", "C")
ghybrid.add_edge("E", "A")
ghybrid.add_edge("E", "D")
Run Code Online (Sandbox Code Playgroud)

我要这个:
# Common out Neighbors
B , E : set(['D'])
E , B : set(['D'])
A , D : set(['C'])
D , A : set(['C'])
# Common in Neighbors
A , D : set(['E'])
D , A : set(['E'])
Run Code Online (Sandbox Code Playgroud)
我可以有共同的“外出”邻居:
def common_out_neighbors(g, i, j):
return set(g.neighbors(i)).intersection(g.neighbors(j))
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何获得通用的“ Neigbors”。谁能帮我?
我有一个关于 Python 多线程的基本问题:我有一个列表,我需要在一个线程中修改它。我知道列表是可变类型:如何通过引用传递变量?
但是,当我使用线程时,列表的行为不像可变类型:
from multiprocessing import Process, Lock
def f(l, i, n):
l.acquire()
i.append(n)
l.release()
print "in:", i
if __name__ == '__main__':
lock = Lock()
i = []
for num in range(10):
p = Process(target=f, args=(lock, i, num))
p.start()
p.join()
print "out:", i
Run Code Online (Sandbox Code Playgroud)
输出
in: [0]
in: [1]
in: [2]
in: [3]
in: [4]
in: [5]
in: [6]
in: [7]
in: [8]
in: [9]
out: []
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解决这个问题吗?
我找不到问题,有谁知道解决?
码
#include <algorithm>?
int main(int argc, char* argv[]) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
警告
extra tokens at end of #include directive [enabled by default]
Run Code Online (Sandbox Code Playgroud) 我需要知道两个指向STL映射的指针之间的区别
例如:使用矢量很简单
vector<pair<int, int> > v;
v.push_back(make_pair(0, 1));
v.push_back(make_pair(2, 3));
v.push_back(make_pair(4, 5));
v.push_back(make_pair(6, 7));
v.push_back(make_pair(8, 9));
vector<pair<int, int> >::iterator itrBegin = v.begin();
vector<pair<int, int> >::iterator itrEnd = v.end();
cout << itrEnd - itrBegin << endl;
Run Code Online (Sandbox Code Playgroud)
产量 5
但是,我想使用STL映射执行相同的操作
map<int, int> m;
m[0] = 1;
m[2] = 3;
m[4] = 5;
m[6] = 7;
m[8] = 9;
map<int, int>::iterator itrB = m.begin();
map<int, int>::iterator itrE = m.end();
cout << ????????????? << endl;
Run Code Online (Sandbox Code Playgroud) 我用R来绘制统计图.我可以绘制简单的图表.但是,我不知道如何绘制复杂数据.
有人可以帮我把所有这些信息整理成图表吗?不包括N(计数).
我的数据集
$ df <- read.csv("database.csv", header=TRUE, sep=",")
$ df
# df is 'data frame'
# Gives count (N), mean, standard deviation (sd), standard error of the mean (se),
# and confidence interval (ci)
method N mean sd se ci
4 A 100 0.3873552 0.014513971 0.0014513971 0.002879887
6 B 100 0.3873552 0.014513971 0.0014513971 0.002879887
11 C 100 0.3873552 0.014513971 0.0014513971 0.002879887
12 D 100 0.3873552 0.014513971 0.0014513971 0.002879887
10 E 100 0.3757940 0.027337627 0.0027337627 0.005424378
1 F 100 0.3715910 …Run Code Online (Sandbox Code Playgroud)