我有一个函数从某个站点获取当前美元货币:
def update_currency(request):
import urllib2
import ast
from django.conf import settings
s = urllib2.urlopen('https://api.privatbank.ua/p24api/pubinfo?exchange=&json&coursid=11').read(1000)
data = ast.literal_eval(s) #parse string list to list
settings.CURRENCY_USD = float(data[2]['sale'])
return redirect(reverse('manager_page'))
Run Code Online (Sandbox Code Playgroud)
经理按下按钮更新货币.但是,如果经理忘记这样做,那么货币就不是实际的了.所以我需要在某个时间每天调用一次这个函数.怎么实现呢?
我正在节省货币settings.py,因为这种方法update_currency需要几秒钟才能从服务器获得货币,而且每次客户购买时都不需要它
np.random.random_integers(5, size=(3.,2.))
Run Code Online (Sandbox Code Playgroud)
我不明白"." 在"3"之后
我试过了
np.random.random_integers(5, size=(3,2))
Run Code Online (Sandbox Code Playgroud)
它似乎与"."完全相同.
我错过了什么?
我已经看到这个问题,我怀疑如何在运行时将var转换为unicode?是不是正确使用unicode功能?还有其他方法可以在运行时转换字符串吗?
print(u'Cami\u00f3n') # prints with right special char
name=unicode('Cami\u00f3n')
print(name) # prints bad ===> Cami\u00f3n
name.encode('latin1')
print(name.decode('latin1')) # prints bad ===> Cami\u00f3n
encoded_id = u'abcd\xc3\x9f'
encoded_id.encode('latin1').decode('utf8')
print encoded_id.encode('latin1').decode('utf8') # prints right
Run Code Online (Sandbox Code Playgroud)
我在stackoverflow上看到了很多python unicode问题,但我无法理解这种行为.
我在python中有一个列表列表.我想将类似的列表组合在一起.也就是说,如果每个列表的前三个元素相同,那么这三个列表应该放在一个组中.例如
[["a", "b", "c", 1, 2],
["d", "f", "g", 8, 9],
["a", "b", "c", 3, 4],
["d","f", "g", 3, 4],
["a", "b", "c", 5, 6]]
Run Code Online (Sandbox Code Playgroud)
我希望这看起来像
[[["a", "b", "c", 1, 2],
["a", "b", "c", 5, 6],
["a", "b", "c", 3, 4]],
[["d","f", "g", 3, 4],
["d", "f", "g", 8, 9]]]
Run Code Online (Sandbox Code Playgroud)
我可以通过运行迭代器并手动比较两个连续列表中的每个元素然后根据这些列表中相同的元素来执行此操作,我可以决定将它们组合在一起.但我只是想知道是否有任何其他方式或pythonic方式来做到这一点.
我正在尝试阅读此文本文件。
A B C D
1 5 6 7
2 8 9 10
3 .......
4 .......
Run Code Online (Sandbox Code Playgroud)
该字母作为一行引入,然后我将所有值作为浮点数引入
with open('file.txt', 'r') as f:
headings = f.readline()
numbers = [float(n) for n in f.read().split()] #read values to the 'numbers' as a list
print numbers
Run Code Online (Sandbox Code Playgroud)
所以我有一长串所有整数。
但我想要这种格式的字典:
my_dict( { 1: [5,6,7], 2:[8,9,10] } )
Run Code Online (Sandbox Code Playgroud)
所以文件编号的第一列是键,其余的是与它们各自的键相关的列表。
我将每 4 个值设置为带有循环的键,但是如何轻松地将其余值作为列表放入相应的键中。
我在django-allauth包中禁用密码检查(password1 with password2)时出现问题.因为现在,注册表单永远不会在表单中获得两个密码.
我有两个包含数字列表的列表(m1和m2).我正在尝试进行元素乘法和求和,包括交叉乘积以获得最终列表(结果),如下所示:
m1 = [[1, 2, 3], [4, 5, 6]]
m2 = [[7, 9, 2], [8, 1, 3]]
[[1*7+2*9+3*2,1*8+2*1+3*3],[4*7+5*9+6*2,4*8+5*1+6*3]]
result = [[31,19],[85,55]]
Run Code Online (Sandbox Code Playgroud) 给定一个字符串中datetime的格式strftime('%b %d, %Y %I:%M %p')
如:'Apr 17, 2016 02:00 AM'
有没有办法检查这段时间是否已经过去了?喜欢可能比较
datetime.datetime.now().strftime('%b %d, %Y %I:%M %p')
Run Code Online (Sandbox Code Playgroud)
不知何故到字符串 'Apr 17, 2016 02:00 AM'
由于服务器GPU限制,我们无法使用 K 渲染 上的隐式方程VPS,下面是我们用来从 中的方程生成 3D 模型的示例代码mayavi:
import numpy as np
from mayavi import mlab
mlab.clf()
x, y, z = np.mgrid[-2.5:2.5:.125, -2.5:2.5:.125, -2.5:2.5:.125]
a,b,c = 0.0,-5.0,11.8
values = x**4+y**4+z**4+a*(x**2+y**2+z**2)**2+b*(x**2+y**2+z**2)+c
mlab.contour3d(x, y, z, values, contours=[0], color=(.1,1,.5))
mlab.savefig("shape.obj")
Run Code Online (Sandbox Code Playgroud)
以 3D 形式输出此形状
我的问题是,我怎样才能完成同样的事情vispy另一个 python 模块(如果存在)完成同样的事情
我们没有显卡VPS,我们在 Windows Server 2008 上使用 python 3.5.1 (anaconda)(不可能使用 linux),并且我们已替换opengl32.dll为 system32 中的 mesa(GL 版本 3.0 mesa 10.2.8)(VTK 抛出错误 GL 版本 2.1 及其gpu_shader4扩展不受支持...)
好吧,我正在编写一个代码,它可以为您输入的加扰字母提供所有可能的组合.这里是:
import random, math
words = []
original = raw_input("What do you need scrambled? ")
def word_scramble(scrambled):
original_length = len(scrambled)
loops = math.factorial(original_length)
while loops > 0:
new_word = []
used_numbers = []
while len(new_word) < original_length:
number = random.randint(0, original_length - 1)
while number in used_numbers:
number = random.randint(0, original_length - 1)
while number not in used_numbers:
used_numbers.append(number)
new_word.append(scrambled[number])
if new_word not in words:
words.append(("".join(str(x) for x in new_word)))
loops -= 1
word_scramble(original)
print ("\n".join(str(x) for x in words)) …Run Code Online (Sandbox Code Playgroud) python ×10
list ×3
django ×2
python-2.7 ×2
python-3.x ×2
3d ×1
datetime ×1
dictionary ×1
group-by ×1
loops ×1
mayavi ×1
numpy ×1
unicode ×1
vispy ×1