我是 networkx 的新手,请帮忙。
我有一组数据,我已经处理过这些数据来生成节点和边。大约有 5000 组节点,其中包含 2 个以上的链接(组中总共最多 10 个节点)。但问题是还有几千对节点之间只有一条边,即节点 a 链接到节点 b 但都没有链接到任何其他节点。
我想从图表中删除这些配对节点。
有没有办法过滤掉这些?
我已经投影了一个二部图并制作了一个新的加权图。我想绘制图形,并显示边缘权重。
这是我的尝试。
import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms import bipartite
g=nx.Graph()
g.add_nodes_from(['s1','s2','s3','s4','s5'],bipartite=0)
g.add_nodes_from(['t1','t2','t3','t4'],bipartite=1)
g.add_edges_from([('s1','t1'),('s1','t4'),('s2','t1'),('s2','t2'),('s3','t1'),('s3','t4'),('s4','t3'),('s5','t2'),('s5','t3')])
l=bipartite.weighted_projected_graph(g,['s1','s2','s3','s4','s5'])
nx.draw(l, with_labels=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
节点标签出现,但不出现边权重。如何显示边权重?
Python 中有很多用于导入模块的标准缩写。我经常看到
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx
Run Code Online (Sandbox Code Playgroud)
我注意到所有这些都是小写的。我想不出有什么例外。然而,它是区分大小写的,所以我们当然可以使用大写字母。有这方面的 PEP 标准吗?特别是,创建具有大写名称的模块并以大写形式导入它们会出现任何问题吗?
例如:
import MyClass as MC
import TomAndJerry as TaJ
Run Code Online (Sandbox Code Playgroud)
(请注意 - 我对个人意见并不真正感兴趣 - 而是是否有官方标准)
我有一个带有节点的字典:
supernodes = list(nx.connected_components(G1))
Run Code Online (Sandbox Code Playgroud)
结果print(supernodes)是:
[{1, 2, 3, 5}, {8, 6}, {7, 9, 10, 12, 13}, {4}, {11}, {14}, {15}]
Run Code Online (Sandbox Code Playgroud)
如何将每个列表合并到一个节点?我找到了这个函数,nx.contracted_nodes(G, (1, 3))但是我如何放置{1,2,3,5}, {8,6}等并创建 7 个签约节点?
我没有编程经验,正在学习Python课程.我现在正在经历条件限制并且遇到一个我不理解其原因的问题.
我基本上只是想让某人在x的期望间隔内绘制x ^ 2,其中x不是负的限制.如果用户输入负输入,他们将收到一条消息,说明不允许这样做.否则,应绘制x与x ^ 2.
N = input('Enter upper limit of x:');
if N < 0:
print "Negative input isn't allowed."
else:
x = arange(N)
y = x*x
plot(x,y)
Run Code Online (Sandbox Code Playgroud)
以上给出了错误:
NameError: name 'arange' is not defined
Run Code Online (Sandbox Code Playgroud)
我不明白,因为arange()通常是根据我(很少)的理解来定义的.
我有两个词.他们分享一些钥匙.我想迭代这些共享密钥.这有三种方法.
x={1:'a', 2:'b', 3:'c'}
y={2:'beta', 3:'gamma', 4:'delta'}
for n in (key for key in x if key in y):
print n
for n in x:
if n in y:
print n
for n in x:
if n not in y:
continue
print n
Run Code Online (Sandbox Code Playgroud)
第一个看起来将来很难阅读.
第二个将在循环中有很多缩进.
第三种用途continue,我发现使代码更难以遵循.
我应该使用什么标准?它是这三个中的一个吗?
我正在爬行slideshare.net图,从我的节点开始,跟踪BFS中的所有用户,直到访问的节点数为1000。我按以下方式执行BFS:
from urllib.request import urlopen
from collections import deque
import sys
import json
import codecs
import csv
import io
import hashlib
import time
import xml.etree.ElementTree as etree
queue = deque(["himanshutyagi11"])
while len_visitedset < 1ooo:
vertex = queue.pop()
if vertex in visited:
continue
visited.add(vertex)
length_visited = len(visited)
print(vertex,length_visited)
crawl(vertex)
Run Code Online (Sandbox Code Playgroud)
crawl() 是一个函数,我在其中进行幻灯片 api 查询,如此处所述, 使用我的shared_secret 和 api_key(在 api 注册时给出)创建查询有效负载,发送查询并解析存储在变量“response”中的 XML 响应'。解析后,我将当前节点的联系人添加到队列中。
request_timestamp = int(time.time())
request_hash_string = shared_secret+str(request_timestamp)
request_hash_value = hashlib.sha1(request_hash_string.encode('utf-8')).hexdigest()
request_url = 'https://www.slideshare.net/api/2/get_user_contacts?username_for='+username+'&api_key='+api_key+'&hash='+request_hash_value+'&ts='+str(request_timestamp)
response = etree.parse(urlopen(request_url)).getroot()
# Append all the adjacent …Run Code Online (Sandbox Code Playgroud) Chrome api 中有多种类型的资源类型,\n例如:文档、样式表、图像、媒体、字体、脚本、TextTrack、XHR、Fetch、EventSource、WebSocket、Manifest、其他。
\n\n现在我想知道resourceType包含哪些mime类型。\n我尝试自己划分,但是mimeType太多了,有没有办法通过代码获取它?\n例如:document\xe2\x86\x92text/html ...\n xhr\xe2\x86\x92application/json...\n 图像->image/png,image/jepg...\n 字体->application/font-woff...
\n\n非常感谢!!!
\n我有两个点X = (x1,y1)和Y=(x2,y2)在笛卡尔平面上。我需要找到第三个点Z = (x,y),使这三个点形成一个等边三角形。
我使用以下代码示例计算两点之间的欧几里德距离:
def distance(points, i, j):
dx = points[i][0] - points[j][0]
dy = points[i][1] - points[j][1]
return math.sqrt(dx*dx + dy*dy)
Run Code Online (Sandbox Code Playgroud)
理论上,我需要使XZ和YZ的距离相等XY。这给了我们两个可能的答案,我也需要它们。Z但我在代码中启动这一点时遇到困难。有人可以帮我解决这个问题吗?以下是我尝试过的示例。
L = [0, 6] #known two points
d= distance(points, L[0], L[1])
x = Symbol('x')
y = Symbol('y')
newpoint = x,y #coordintes of the third point of the triangle
f1 = distance(points, L[0], newpoint)
f2 = distance(points, L[1], newpoint)
print(nsolve((f1, f2), (x, …Run Code Online (Sandbox Code Playgroud) 尝试在 Python 2.7.8 中实现以下目标
class X:
pass
a = X()
b = X()
t={1: {1: a, 2: b}}
map(lambda x: setattr(x, 'test', 'wow'), t[1].values())
Run Code Online (Sandbox Code Playgroud)
给出:[None,None],虽然我希望它将每个属性“test”设置为“wow”,如果我在map()之外调用setattr(a,“test”,“wow”)那么一切正常
我有以下字符串:
input = "I love programming with Python-3.3! Do you? It's great... I give it a 10/10. It's free-to-use, no $$$ involved!"
Run Code Online (Sandbox Code Playgroud)
所有标点符号都应与"/","'"," - ","+"和"$"之类的单词"EXCEPT"分开.
所以输出应该是:
"I love programming with Python-3 . 3 ! Do you ? It's great . . . I give it a 10/10. It's free-to-use , no $$$ involved !"
Run Code Online (Sandbox Code Playgroud)
我使用了以下代码:
for x in string.punctuation:
if x == "/":
continue
if x == "'":
continue
if x == "-":
continue
if x == "+":
continue
if …Run Code Online (Sandbox Code Playgroud) 我在 github 上有一个存储库:
https://github.com/springer-math/Mathematics-of-Epidemics-on-Networks
里面有一个文件
文档/SIR_2dgrid.mp4
其中包含我想出现在 readthedocs 文档中的动画(特别是在此页面:http://epidemicsonnetworks.readthedocs.io/en/latest/Simulation_Investigation.html)此外,还有一个文件
文档/Simulation_Investigation.rst
它对该页面进行编码并尝试引用该 mp4。我尝试了很多变体(尝试复制https://groups.google.com/d/msg/sphinx-users/_z00m3zoRAY/Ryx4D5sxgtcJ上的说明),但没有运气。我能让 mp4 出现在所需的 readthedocs 页面上的唯一方法是链接到 github 上的 mp4(但加载有点延迟,而且并不完美)。
我似乎无法通过 readthedocs 将 mp4 复制到可以建立相对链接的目录中。
有没有办法让 readthedocs 将该文件复制到我可以链接到的地方?
或者,除了将 mp4 放在另一个网站上之外,还有其他方法可以将视频嵌入到 readthedocs 中吗?
python ×9
networkx ×4
python-3.x ×2
algorithm ×1
dictionary ×1
graph ×1
mime-types ×1
python-2.7 ×1
regex ×1