我需要一个JavaScript函数,它可以获取一个值并将其填充到给定的长度(我需要空格,但任何事情都可以).我找到了这个:
码:
String.prototype.pad = function(l, s, t){
return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
+ 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
+ this + s.substr(0, l - t) : this;
};
Run Code Online (Sandbox Code Playgroud)
例:
<script type="text/javascript">
//<![CDATA[
var s = "Jonas";
document.write(
'<h2>S = '.bold(), s, "</h2>",
'S.pad(20, "[]", 0) = '.bold(), s.pad(20, "[]", 0), "<br />",
'S.pad(20, "[====]", 1) …Run Code Online (Sandbox Code Playgroud) 在高流量应用中使用TensorFlow进行实时预测的正确方法是什么.
理想情况下,我将有一个服务器/集群运行tensorflow监听端口,我可以从应用服务器连接,并获得类似于数据库使用方式的预测.培训应该由cron作业通过网络将培训数据提供给同一服务器/集群来完成.
如何在生产中实际使用张量流?我应该构建一个python作为服务器运行的设置并使用python脚本来获取预测吗?我还是新手,但我觉得这样的脚本需要打开会话等......这是不可扩展的.(我说的是每秒100次预测/秒).
任何指向相关信息的指针都将受到高度赞赏.我找不到任何东西.
我想转这个:
mystr = ' foo1 (foo2 foo3 (foo4))'
Run Code Online (Sandbox Code Playgroud)
进入:
['foo1','foo2 foo3 (foo4)']
Run Code Online (Sandbox Code Playgroud)
所以基本上我必须根据一些空格/制表符和括号进行拆分。
我已经看到 re package split 函数可以处理多个分隔符(Python: Split string with multiple delimiters),但我无法理解解析这种字符串的正确方法。
哪个是最好的-最pythonic-和简单的方法?
请考虑以下代码:
>>> class A(object):
... def __init__(self, a):
... self.a = a
... def __eq__(self, other):
... return self.a==other.a
...
>>> a=A(1)
>>> b=A(1)
>>> c=A(2)
>>> a==b
True # because __eq__ says so
>>> a==c
False # because __eq__ says so
>>> a is b
False # because they're different objects
>>> l = [b,c]
>>> a in l
True # seems to use __eq__ under the hood
Run Code Online (Sandbox Code Playgroud)
因此,in似乎用于__eq__确定某个容器中是否有东西.
in使用对象标识,即a in …我正在尝试运行张量板,但它一直显示相同的错误。
tensorboard --logdir=tensorflow/logdir
File "<stdin>", line 1
SyntaxError: can't assign to operator
Run Code Online (Sandbox Code Playgroud)
我使用的是 Ubuntu 16.04 并通过 virtualenv 安装了tensorflow-gpu。
我正在学习如何使用websockets带有asyncio.
使用Websockets Getting Started示例,这里是我的服务器和客户端代码(都使用 运行在两个单独的控制台中python <script>)
wsserver.py
import asyncio
import websockets
msg_queue = asyncio.Queue()
async def consumer_handler(websocket):
global msg_queue
while True:
message = await websocket.recv()
print("Received message {}".format(message))
await msg_queue.put("Hello {}".format(message))
print("Message queued")
async def producer_handler(websocket):
global msg_queue
while True:
print("Waiting for message in queue")
message = await msg_queue.get()
print("Poped message {}".format(message))
websocket.send(message)
print("Message '{}' sent".format(message))
async def handler(websocket, path):
print("Got a new connection...")
consumer_task = asyncio.ensure_future(consumer_handler(websocket))
producer_task = asyncio.ensure_future(producer_handler(websocket))
done, pending …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个清单,
my_list=["one two","three four"]
Run Code Online (Sandbox Code Playgroud)
我想要的输出是,
output_list=['one', 'two', 'three', 'four']
Run Code Online (Sandbox Code Playgroud)
我试过下面的代码,
my_list=" ".join(my_list)
output_list=my_list.split()
output_list,
['one', 'two', 'three', 'four']
Run Code Online (Sandbox Code Playgroud)
它工作正常,我得到我的输出,我希望,解决方案比这更短的解决方案可以实现相同,在此先感谢!
我有以下内容string:
s = 'sd sdasd sas sas zxxx df xx de '
Run Code Online (Sandbox Code Playgroud)
当我使用时,
s.strip('x')我得到以下结果:
'sd sdasd sas sas zxxx df xx de '
Run Code Online (Sandbox Code Playgroud)
为什么strip()不删除所有'x'字符?
我需要编写一个函数,给定一个输入列表,列表中的所有相邻元素都相互交换。如果列表的长度是奇数,则最后一个元素保持不变。我迭代地编写函数,如下所示:
>>>def swap(nums):
for i in range(0,len(nums),2):
try:
nums[i],nums[i+1] = nums[i+1], nums[i]
except:
pass
return nums
>>>swap([1,2,3,4,5])
[2, 1, 4, 3, 5]
Run Code Online (Sandbox Code Playgroud)
我对递归版本使用了与以前完全相同的逻辑:
def swap(nums, c=0):
try:
nums[c], nums[c+1] = nums[c+1], nums[c]
return swap(nums, c+2)
except:
return nums
Run Code Online (Sandbox Code Playgroud)
虽然两者都有效,但我觉得我在使用这些try/except块时有点作弊,而且我不会一直使用它们而成为一个更好的程序员。有人可以就如何在不依赖try/except块的情况下解决这些问题给我建议吗?
我正在尝试使用从 python 文件到.kv文件的变量,所以我搜索了类似的问题并找到了使用Property和编码的方式:
# in python file
class Test2App(App):
abcd = StringProperty('test')
def build(self):
return presentation
# in kv file
<MyButton@Button>:
text: "contents (%s)"%(app.abcd)
background_color: (255, 255, 255,1)`
Run Code Online (Sandbox Code Playgroud)
并出现错误。
AttributeError: 'NoneType' object has no attribute 'bind'
File "/usr/local/lib/python2.7/dist-packages/kivy/lang/builder.py", line 249, in create_handler
return eval(value, idmap), bound_list
File "/root/Desktop/hi/t2.kv", line 61, in <module>
text: "contents (%s)"%(app.abcd)
File "/usr/local/lib/python2.7/dist-packages/kivy/lang/parser.py", line 75, in __getattribute__
object.__getattribute__(self, '_ensure_app')()
File "/usr/local/lib/python2.7/dist-packages/kivy/lang/parser.py", line 70, in _ensure_app
app.bind(on_stop=lambda instance:
File "/usr/local/lib/python2.7/dist-packages/kivy/lang/builder.py", line 615, in …Run Code Online (Sandbox Code Playgroud) python ×9
list ×2
regex ×2
tensorflow ×2
javascript ×1
kivy ×1
listview ×1
python-2.7 ×1
python-3.6 ×1
recursion ×1
string ×1
strip ×1
tensorboard ×1
websocket ×1