我试图通过以下代码生成数据:
for Gnodes in G.nodes() # Gnodes iterates over 10000 values
Gvalue = someoperation(Gnodes)
for Hnodes in H.nodes() # Hnodes iterates over 10000 values
Hvalue =someoperation(Hnodes)
score = SomeOperation on (Gvalue,Hvalue)
dic_score.setdefault(Gnodes,[]).append([Hnodes, score, -1 ])
Run Code Online (Sandbox Code Playgroud)
由于字典很大(10000个键X 10000个列表,每个包含3个元素),因此很难将其保存在内存中.我正在寻找一个解决方案,它存储密钥:值(以列表的形式)对生成后立即.这里建议,以特定格式(Python)编写和阅读字典,以将ZODB与Btree结合使用.
如果这太天真,请耐心等待,我的问题是,何时应该调用transaction.commit()提交数据?如果我在内部循环结束时调用它,则生成的文件非常大(不确定原因).这是一个片段:
storage = FileStorage('Data.fs')
db = DB(store)
connection = db.open()
root = connection.root()
btree_container = IOBTree
root[0] = btree_container
for nodes in G.nodes()
btree_container[nodes] = PersistentList () ## I was loosing data prior to doing this
for Gnodes in …Run Code Online (Sandbox Code Playgroud) 此查询与之前的排序有些相关
在哪里,需要对以下列表进行排序,
data = [[1, .45, 0], [2, .49, 2], [3, .98, 0], [4, .82, 1], [5, .77, 1], [6, .98, 2] ]
首先通过内部列表的最后一个成员的值,这样,
[[1, .45, 0], [3, .98, 0],[4, .82, 1], [5, .77, 1], [2, .49, 2], [6, .98, 2]]
然后在子列表中排序,即首先将列表与其最后一个成员排序为'0'使用中间成员作为键,按降序排序,然后将最后一个成员的子列表排序为'1'依此类推.
现在,我想要根据外部列表中存在的这些元素的顺序进行排序,而不是首先按最后一个成员的值进行排序.即外部列表是否为List_1
`List_1 = [2, 0, 1]`
Run Code Online (Sandbox Code Playgroud)
排序应该产生
[[2, .49, 2], [6, .98, 2] [1, .45, 0], [3, .98, 0], [4, .82, 1], [5, .77, 1]]
Run Code Online (Sandbox Code Playgroud)
最后,按降序排列基于中间元素的子列表应该产生:
[ [6, .98, 2],[2, .49, 2], …Run Code Online (Sandbox Code Playgroud) 我试图在Python中完成以下逻辑操作,但进入内存和时间问题.既然,我是python的新手,那么如何以及在哪里优化问题的指导将不胜感激!(我明白以下问题有点抽象)
import networkx as nx
dic_score = {}
G = nx.watts_strogatz_graph(10000,10,.01) # Generate 2 graphs with 10,000 nodes using Networkx
H = nx.watts_strogatz_graph(10000,10,.01)
for Gnodes in G.nodes()
for Hnodes in H.nodes () # i.e. For all the pair of nodes in both the graphs
score = SomeOperation on (Gnodes,Hnodes) # Calculate a metric
dic_score.setdefault(Gnodes,[]).append([Hnodes, score, -1 ]) # Store the metric in the form a Key: value, where value become a list of lists, pair in a dictionary
Run Code Online (Sandbox Code Playgroud)
然后根据此处提到的条件sorted_criterion …
说我有以下列表:
my_list = [2, 3, 4, 1, 44, 222, 43, 22]
Run Code Online (Sandbox Code Playgroud)
如何在不使用for循环的情况下为子列表中的所有元素分配常量值?就像是:
my_list[0:5:1] = 1 # Assign 1 to first 5 elements. This code is wrong since list requires an iterator
Run Code Online (Sandbox Code Playgroud)
具体来说,我想为所有元素分配一个常量值,从索引开始,i直到end列表,即说
my_list[i:end] = 1 # What I would like to do. The code itself is wrong
Run Code Online (Sandbox Code Playgroud)
关于如何在python中以最干净的方式做到这一点的任何建议?
说我有一个numpy数组x:
x = array([[ 3, 2, 1],
[ 3, 25, 34],
[ 33, 333, 3],
[ 43, 32, 2]])
Run Code Online (Sandbox Code Playgroud)
我想在没有显式写入for循环的情况下执行以下操作,即说一个在内置循环中使用自动的方法;
1)将所有1列的第2列替换为ie
x = array([[ 3, 1, 1],
[ 3, 1, 34],
[ 33, 1, 3],
[ 43, 1, 2]])
Run Code Online (Sandbox Code Playgroud)
2)在原始数组中,将第3列替换为第2列和第3列的乘积
x = array([[ 3, 2, 1*2],
[ 3, 25, 34*25],
[ 33, 333, 3*333],
[ 43, 32, 2*32]])
Run Code Online (Sandbox Code Playgroud)
3)最后,我想基于一个条件替换原始数组中的第二列
x[1] = 0 if x[0] > 5 else 4
Run Code Online (Sandbox Code Playgroud)
即阵列现在看起来像:
x = array([[ 3, 4, 1],
[ …Run Code Online (Sandbox Code Playgroud) 我有一个名为的两个numpy数组,dec_pair并dec_community在一个名为config.pyinitial 的模块中初始化为零:
dec_pair = numpy.zeros(200)
dec_community = numpy.zeros(200)
Run Code Online (Sandbox Code Playgroud)
现在,我试图从其他模块访问它们,比如说roc.py,它们的名称是根据输入变量形成的,即
import config
def dosomething(name):
local_name = 'config.py'+name
eval(local_name)[i:] += 1
Run Code Online (Sandbox Code Playgroud)
哪里name可以pair或community.问题是,eval(local_name)返回numpy数组的长度,即200这里而不是数组本身,这给了我这个错误:
ValueError:无法切片0-d数组
但是,当我在python解释器上做同样的事情时,它运行顺利:
>>> dec_pair = numpy.zeros(5)
>>> name = 'pair'
>>> local_name = 'dec_'+name
>>> eval(local_name)
array([ 0., 0., 0., 0., 0.])
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么,以及做正确的方法是什么?
也许这个问题太天真了,但给我带来了困难!我想csv在循环中将2个float值和一个int列表写入文件中的一行。尝试写入文件之前,该文件可能存在也可能不存在。如果没有,则应创建一个新文件。这就是我在做什么:
f = open('stat.csv','a')
try:
writer=csv.writer(f,delimiter=' ',quoting=csv.QUOTE_MINIMAL)
writer.writerow((some_float1,some_float2,alist))
finally:
f.close()
Run Code Online (Sandbox Code Playgroud)
在哪里alist = [2,3,4,5]。我得到以下输出:
some_float1 some_float2 "[2,3,4,5]"
Run Code Online (Sandbox Code Playgroud)
我想要的是:
some_float1 some_float2 2 3 4 5
Run Code Online (Sandbox Code Playgroud)
即我想摆脱""方括号,并使定界符始终保持一致。有什么建议么 ?