【Python:更新字典中的值】教程文章相关的互联网学习教程文章

Python matplotlib – 在Conway的生命游戏中动画期间更新数据【代码】

下面的代码使用Python和matplotlib为Conway的生命游戏创建动画. 我不知道为什么要这样做:grid = newGrid.copy() mat.set_data(grid)而不是简单地:mat.set_data(newGrid)如何在不进行上述复制的情况下更新与绘图关联的阵列?import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animationN = 100 ON = 255 OFF = 0 vals = [ON, OFF]# populate grid with random on/off - more off than on grid...

python – 使用Web应用更新已发布的Google电子表格?【代码】

是否有可能(使用django和python)让Web应用程序将内容(包含数据的行)添加到已发布的电子表格中?解决方法:gspread link是一个很棒的Python程序包来操纵Google电子表格. 文档很全面,包很容易实现.它允许您通过直接单元名称或单元格坐标来寻址字段.# With name val = worksheet.acell('B1').value# With coords val = worksheet.cell(1, 2).value

python – 如何强制django立即保存,而不是在循环后进行批量更新【代码】

我有这个django views.py方法,旨在将许多数据插入到数据库中.它循环遍历模型数组,如果某个对象尚未在数据库中,则会插入它. 这就是代码的样子:def update_my_db(request):a_models = A_Model.objects.filter(my_flag=True)for a_model in a_models:b_model_array = [][...] # this is where b_model_array gets filledfor index in range(len(b_model_array)):current_b_model = b_model_array[index]try:b_model = B_Model.object...

python – QT系统托盘应用程序中的更新菜单【代码】

我需要更新系统托盘应用程序的现有菜单项.首先,当应用加载时,将有两个菜单项.稍后,当我单击一个按钮时,这些菜单项需要替换为新的菜单项.我怎样才能做到这一点?这是我的代码.from PySide.QtGui import * import sysclass MainWindow(QMainWindow):def __init__(self):super(MainWindow, self).__init__()self.tray = QSystemTrayIcon(QApplication.style().standardIcon(QStyle.SP_DriveDVDIcon), self)self.m = QMenu()self.m.add...

python – 在Theano中更新共享变量的一部分【代码】

如何在Theano中更新共享变量的一部分? 例如.而不是做:gradient_W = T.grad(cost,W) updates.append((W, W-learning_rate*gradient_W)) train = th.function(inputs=[index], outputs=[cost], updates=updates,givens={x:self.X[index:index+mini_batch_size,:]})我只想更新部分W,例如只有第一列:updates.append((W[:, 0], W[:, 0]-learning_rate*gradient_W))但这样做会给出错误TypeError :(‘update target必须是SharedVariabl...

Python:局部变量神秘地更新全局变量【代码】

我有一个函数,我使用局部变量,然后在函数完成后传回最后一个变量.我想记录该变量在函数之前的含义,但全局变量与局部变量一起更新.这是我的代码的缩写版本(很长)def Turn(P,Llocal,T,oflag):#The function here changes P, Llocal and T then passes those values backreturn(P, Llocal, T, oflag)#Later I call the function #P and L are defined here, then I copy them to other variables to save #the initial valuesP=Piniti...

python – 完整更新pip包时出错【代码】

pip用命令更新所有包pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs pip install -Uvboxapi的pip打印错误Downloading/unpacking vboxapiCould not find any downloads that satisfy the requirement vboxapiSome externally hosted files were ignored (use --allow-external vboxapi to allow).Some insecure and unverifiable files were ignored (use --allow-unverified vboxapi to allow). Cleaning up... No...

python – 更新PIL中的像素颜色后图像不保存【代码】

我使用HuBarcode以PNG格式生成条形码图像,并修改它们以添加rgb(150,150,150)的边框,希望访问该颜色的像素以更改它们.我可以访问像素并通过打印确认颜色正在改变,但是当我打开图像文件时,没有任何改变.像素仍为rgb(150,150,150).这是我正在使用的代码片段.如果它有用,我可以添加更多代码:def add_colored_border(self, barcode):img = Image.open(barcode)img = img.convert('RGB')px = img.load()for x in range(img.size[0]):for...

python – 绑定到StringVar的Tkinter标签在更新时只需点击一下即可【代码】

我遇到的问题是,当我点击列表框中的不同文件名时,标签会在我正在点击的任何内容后面点击一次. 我在这里错过了什么?import Tkinter as tkclass TkTest: def __init__(self, master):self.fraMain = tk.Frame(master)self.fraMain.pack()# Set up a list box containing all the paths to choose fromself.lstPaths = tk.Listbox(self.fraMain)paths = ['/path/file1','/path/file2','/path/file3',]for path in paths:self.lstPath...

python – 尝试使用matplotlib更新3D图形坐标【代码】

我有一个函数,它将在tkinter中使用matplotlib绘制3D球体.然而,每次连续的时间我都会在旋转球体时调用该函数的性能下降.此图仅在我尝试围绕球体运行后更新. self.A是一个调整球体大小的变量. 我的功能:def draw_fig(self):self.ax = Axes3D(self.fig)u = numpy.linspace(0, 2 * numpy.pi, 100)v = numpy.linspace(0, numpy.pi, 100)x = self.A * numpy.outer(numpy.cos(u), numpy.sin(v))y = self.A * numpy.outer(numpy.sin(u), n...

python – 树视图中的复选框缓慢更新【代码】

我为树视图实现了一个cutom模型,在树视图中有复选框.如果我检查父节点,则应自动对所有子节点进行chekced.这基本上有效,但在检查父节点和更新子节点之间缺少时间.from PyQt4 import QtCore, QtGui import sysclass Node(object): def __init__(self, name, parent=None, checked=False):self._name = nameself._children = []self._parent = parentself._checked = checkedif parent is not None:parent.addChild(self)def addChi...

使用python-requests模块更新会话中的Cookie【代码】

我正在使用python-requests模块来处理oAuth请求和响应.我想在requests.session.cookies对象中设置收到的access_token(响应内容为dict). 如何使用从服务器收到的响应更新会话的现有cookie? [编辑]self.session = requests.session(auth=self.auth_params) resp = self.session.post(url, data=data, headers=self.headers) content = resp.content我想做的事情如下:requests.utils.dict_from_cookiejar(self.session.cookies).upd...

如何使用python中的现有hasher更新hashlib.md5 hasher?【代码】

我有缓存的缓存实例:m1 = hashlib.md5() m1.update(b'very-very-long-data') cached_sum = m1我想用以前缓存的总和来更新外部哈希:def append_cached_hash(external_hasher):# something like thisexternal_hasher.update(cached_sum)不幸的是,它不起作用,因为update()需要字节.我可以再次传递相同的“非常非常长的数据”字节,但它拒绝为常见的长数据对象预缓存md5和的整个想法. 我可以做类似以下的事情:external_hasher.update...

python – 更新模型中的新字段【代码】

我正在尝试更新我的数据库中关于我的“卡”模型的新字段,该模型已经有上面的字段,但是我遇到了阻碍我执行此过程的问题: 当我运行./manage.py syncdb时收到此消息:Your models have changes that are not yet reflected in a migration, and so won't be applied.Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.所以我运行了makemigrations命令,但……You are tryin...

python – 更新theano函数中的参数【代码】

以这种方式调用时,“更新”参数有什么作用?f_grad_shared = theano.function([x, mask, y], cost, updates=zgup + rg2up,name='adadelta_f_grad_shared')我在theano函数中看到的关于“updates”参数的所有文档都讨论了形式的对(共享变量,用于更新共享变量的表达式).但是,这里只有一个表达式,所以如何知道哪个共享变量被更新? 我猜共享变量是以某种方式隐式的,但zgup和rg2up都取决于不同的共享变量:zipped_grads = [theano.share...

字典 - 相关标签