设置为字符串.明显:
>>> s = set([1,2,3])
>>> s
set([1, 2, 3])
>>> str(s)
'set([1, 2, 3])'
Run Code Online (Sandbox Code Playgroud)
要设置的字符串?也许是这样的?
>>> set(map(int,str(s).split('set([')[-1].split('])')[0].split(',')))
set([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)
非常难看.是否有更好的序列化/反序列化集的方法?
我正在从ORACLE迁移.目前我正在尝试移植此电话:
lkstat := DBMS_LOCK.REQUEST(lkhndl, DBMS_LOCK.X_MODE, lktimeout, true);
Run Code Online (Sandbox Code Playgroud)
此函数尝试获取锁定lkhndl
,如果在timeout
几秒钟后无法获取锁定,则返回1 .
在postgresql我用
pg_advisory_xact_lock(lkhndl);
Run Code Online (Sandbox Code Playgroud)
但是,它似乎永远等待锁定.pg_try_advisory_xact_lock
如果失败则立即返回.有没有办法实现锁定获取的超时版本?
有lock_timeout
设置,但我不确定它是否适用于咨询锁定以及pg_advisory_xact_lock
超时后的行为方式.
目录结构:
main.py
my_modules/
module1.py
module2.py
Run Code Online (Sandbox Code Playgroud)
module1.py:
class fooBar():
....
class pew_pew_FooBarr()
....
...
Run Code Online (Sandbox Code Playgroud)
如何将所有类从module*添加到没有前缀的main(即使用它们像foo = fooBar(),而不是foo = my_modules.module1.fooBar()).
一个明显的决定是在main.py中写这样的东西:
from my_modules.module1 import *
from my_modules.module2 import *
from my_modules.module3 import *
...
Run Code Online (Sandbox Code Playgroud)
但是我不想在创建新模块N时更改main.py.那有解决方案吗?
我知道导入这样的类不是一个好主意,但我仍然对此感到好奇.
UPD:这个问题不同于这个问题在Python中加载一个文件夹中的所有模块,因为我的问题是加载没有命名空间的模块.
我正在使用Elasticsearch v2.3.0.假设我有一个带映射的索引:
{
"mappings": {
"post": {
"dynamic": false,
"_source": {
"enabled": true
},
"properties": {
"text": {
"norms": {
"enabled": false
},
"fielddata": {
"format": "disabled"
},
"analyzer": "text_analyzer",
"type": "string"
},
"subfield": {
"properties": {
"subsubfield": {
"properties": {
"subtext": {
"index": "no",
"analyzer": "text_analyzer",
"type": "string",
"copy_to": "text"
}
}
}
}
}
},
"_all": {
"enabled": false
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以,从文本subfield.subsubfield.subtext
复制到文本field
因copy_to
.如果你查询信息,然后从纯数据text
显示在text
现场,因为_source
不被修改.如果要获取所有文本,则应聚合客户端上的所有字段.如果存在许多子场,则这可能是不方便的.
是否有一个神奇的查询,它允许获取text
所有文本复制到它的字段?
我读了几个主题,但我迷路了.我对此很陌生.我想存储巨大的稀疏矩阵并有几个想法,但可以在它们之间进行选择.这是我的需求:
所以,这是我的想法:
请帮助我选择或提供更好的决定.
如果我在某处估计错了,请纠正我.
我想改造
l = ['a','b','c','d']
Run Code Online (Sandbox Code Playgroud)
至
d = {'a': 0, 'b': 1, 'c': 2, 'd': 3}
Run Code Online (Sandbox Code Playgroud)
我到目前为止最好的解决方案是:
d = {l[i]:i for i in range(len(l))}
Run Code Online (Sandbox Code Playgroud)
有没有更优雅的方式来做到这一点?
我需要一个具有三个操作的线程安全数据结构:remove,getRandom,reset.我现在只有两个想法.
第一个:同步变量中的Seq.
val all: Array[String] = ... //all possible.
var current: Array[String] = Array.empty[String]
def getRandom(): = {
val currentAvailable = current
currentAvailable(Random.nextInt(currentAvailable.length))
}
def remove(s: String) = {
this.syncronized {
current = current diff Seq(s)
}
}
def reset(s: String) = {
this.syncronized {
current = all
}
}
Run Code Online (Sandbox Code Playgroud)
第二:保持一些Map [String,Boolean],当元素当前存在时,bool为true.主要问题是制作一个快速的getRandom方法(在最坏的情况下不是O(n)).
有没有更好的方法来实现这个?
我使用了几个场景,目前每个场景都有一种方法,比如
void setScene1() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/main2.fxml"));
Parent root = FXMLLoader.load();
Scene scene = new Scene(root);
loader.<Controller1>getController().callMethod();
primaryStage.setScene(scene);
}
Run Code Online (Sandbox Code Playgroud)
但是我想要记住Scene并且那样做
void setScene1() {
FXMLLoader loader = scene1.getLoaderSomehow(); // < ---- ????
loader.<Controller1>getController().callMethod();
primaryStage.setScene(scene1);
}
Run Code Online (Sandbox Code Playgroud) 我想编写一个方法,None
如果集合为空,则返回该方法Some(collection)
.
我能得到的最好的是
implicit class CollectionExtensions[A, Repr](self: TraversableLike[A, Repr]){
def toOption: Option[Repr] = if (self.nonEmpty) Some(self.asInstanceOf[Repr]) else None
}
Run Code Online (Sandbox Code Playgroud)
但铸造.asInstanceOf[Repr]
似乎是错误的.什么是正确的方法?
给定一个Tuple3,例如("one", "two", "three")
我想获得tuple2,它只包含前两个元素("one", "two")
.
一种方法是这样的:
val (one, two, _) = ("one", "two", "three")
val result = (one, two)
Run Code Online (Sandbox Code Playgroud)
但是,如果我想从tuple16获取tuple14,那该怎么办呢?样板.
更新:
更具体的用例(func2和func3不能更改).
def func3(one: String, two: String, three: String) = println("Do something")
def func2(one: String, two: String) = println("Do something 2")
val originalTuple = ("one", "two", "three")
val newTuple = ???
(func3 _).tupled(originalTuple)
(func2 _).tupled(newTuple)
Run Code Online (Sandbox Code Playgroud) 根据Java的substring()的时间复杂度,java的子串需要线性时间.有更快的方式(可能在某些情况下)?我可能会建议迭代器,但怀疑它也需要O(n).
val s1: String = s.iterator.drop(5).mkString
Run Code Online (Sandbox Code Playgroud)
但迭代器上的几个操作比字符串上的相同操作更快,对吧?