我是python的新手,所以这个问题可能有点基础.我有一个名为的元组values,其中包含以下内容:
('275', '54000', '0.0', '5000.0', '0.0')
Run Code Online (Sandbox Code Playgroud)
我想更改275此元组中的第一个值(即),但我知道元组是不可变的,所以values[0] = 200不起作用.我怎样才能做到这一点?
可能重复:
解释切片表示法
我正在尝试理解以下代码:
# node list
n = []
for i in xrange(1, numnodes + 1):
tmp = session.newobject();
n.append(tmp)
link(n[0], n[-1])
Run Code Online (Sandbox Code Playgroud)
具体来说,我不明白索引-1所指的是什么.如果索引0引用第一个元素,那么什么-1引用?
如何检查字符串是表示长,双,还是只是常规字符串?我需要这样做,因为这个值需要根据其类型在数据库中建立索引.目前我这样做是通过尝试解析字符串并检查异常但由于代码被频繁调用,我想知道是否有更有效的方法来执行它.我的代码目前看起来像这样:
String value = ...;
// For example, could be "213678", "654.1236781", or "qwerty12345"
try {
Long longValue = Long.parseLong(value);
// Index 'longValue' in the database
} catch (NumberFormatException parseLongException) {
try {
Double doubleValue = Double.parseDouble(value);
// Index 'doubleValue' in the database
} catch (NumberFormatException parseDoubleException) {
// Index 'value' in the database
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我只是根据@ user949300建议使用正则表达式模式进行了快速基准测试,它的表现略好于上面的异常处理代码.这是代码,以防其他人发现它有用:
Pattern longPattern = Pattern.compile("^[-+]?[0-9]+$");
Pattern doublePattern = Pattern.compile("^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$");
// Check for long regex pattern before the double regex pattern
// since …Run Code Online (Sandbox Code Playgroud) 我正在尝试从具有以下形式的字符串中捕获键值对:
a0=d235 a1=2314 com1="abcd" com2="a b c d"
Run Code Online (Sandbox Code Playgroud)
使用这篇文章的帮助,我能够编写以下正则表达式来捕获键值对:
Pattern.compile("(\\w*)=(\"[^\"]*\"|[^\\s]*)");
Run Code Online (Sandbox Code Playgroud)
问题是此模式中的第二组也捕获引号,如下所示:
a0=d235
a1=2314
com1="abcd"
com2="a b c d"
Run Code Online (Sandbox Code Playgroud)
如何排除引号?我想要这样的东西:
a0=d235
a1=2314
com1=abcd
com2=a b c d
Run Code Online (Sandbox Code Playgroud)
编辑:
可以通过根据是否存在引号来捕获不同组中的值来实现上述目的.我正在为解析器编写此代码,因此出于性能原因,我试图提出一个可以返回相同组号中的值的正则表达式.
我正在尝试创建一个动态图并使用Gephi工具包对其进行流式处理.到目前为止,我已经按照工具包和流媒体插件教程创建了一个普通的图形并将其流式传输到Gephi GUI.
我很难弄清楚如何使图形动态化 - 我已经设法TimeInterval使用了这些列添加到Node和Edge表中,AttributeModel但是当我在GUI中打开时间轴窗口时,它表示图形不是动态的.模型/控制器对我来说有点混乱.
这是我现在的代码:
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
AttributeController attributeController = Lookup.getDefault().lookup(AttributeController.class);
AttributeModel attributeModel = attributeController.getModel();
AttributeColumn nodeTimeColumn = attributeModel.getNodeTable().addColumn(DynamicModel.TIMEINTERVAL_COLUMN, AttributeType.TIME_INTERVAL, AttributeOrigin.PROPERTY);
AttributeColumn edgeTimeColumn = attributeModel.getEdgeTable().addColumn(DynamicModel.TIMEINTERVAL_COLUMN, AttributeType.TIME_INTERVAL, AttributeOrigin.PROPERTY);
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
GraphModel graphModel = graphController.getModel();
DirectedGraph graph = graphModel.getDirectedGraph();
// At this point, I want to make the graph dynamic so that I can use
// the Timeline feature when I stream to the GUI.
StreamingServer server = …Run Code Online (Sandbox Code Playgroud)