我有这样的情况,我需要循环遍历两个对象列表并找到相等,然后循环其字段并更改一些属性.看起来像这样
for new_product in products_and_articles['products']:
for old_product in products_for_update:
if new_product.article == old_product.article:
for old_field in old_product._meta.get_all_field_names():
for new_field in new_product._meta.get_all_field_names():
if old_field == new_field and old_field != 'id' and old_field != 'slug':
setattr(old_product, old_field, getattr(new_product, old_field))
Run Code Online (Sandbox Code Playgroud)
显然,这远非好或甚至不可接受.所以我正在寻求建议如何避免这么多循环并增强算法
我有以下数据的txt文件:
3455155, 117465776, 29972373, 27226577, 64735238
Run Code Online (Sandbox Code Playgroud)
我需要删除此文件中的条目,例如: 3455155
我正在尝试以下内容:
def remove_entry_from_file(entry_id):
f = open(file.txt, 'w+')
data = f.read()
new_data = re.sub(entry_id + '[, ]*', '' ,data)
f.write(new_data)
f.close()
Run Code Online (Sandbox Code Playgroud)
结果 - 文件将被完全清除.哪里错了?
我有一个表格的字典{'a': [1, 2, 3], 'b': [1, 2, 3, 4], 'c': [13. 11]}.我想找到dict中最长值的长度.在这种情况下将是4.我知道如何使用数组执行此操作,如下所示:
maxSoFar = 0;
for key in dict.keys():
if dict[key] > maxSoFar :
maxSoFar = len(dict[key])
Run Code Online (Sandbox Code Playgroud)
我的问题是,有一个神奇的一个班轮来做这个吗?
我有两个方法max(x,y)和min(x,y),它们分别返回传递给它们的参数的最大值和最小值.
我需要使用lambda函数调用它们中的每一个...
funWithNum = [lambda x: funWithNum[0], max , lambda y: funWithNum[1], min]
print funWithNum[0](1, 2)
print funWithNum[1](1, 2)
Run Code Online (Sandbox Code Playgroud)
当我使用时print funWithNum[0](1, 2),我应该得到最大数量,当我使用时print funWithNum[1](1, 2),我应该得到最小数量.我该如何实现这一目标?
我在这里有一个任务:
给定一组int,返回数组中9的数量.
array_count9([1, 2, 9]) ? 1
array_count9([1, 9, 9]) ? 2
array_count9([1, 9, 9, 3, 9]) ? 3
Run Code Online (Sandbox Code Playgroud)
我有2个想法,一个是:
def array_count9(nums):
count = 0
list1 = [x for x in nums if x==9]
return len(list1)
Run Code Online (Sandbox Code Playgroud)
和另外一个:
def array_count9(nums):
count = 0
for n in nums:
if n==9:
count +=1
return count
Run Code Online (Sandbox Code Playgroud)
但我想知道在性能,清晰度方面,哪种方式更像Pythonic ......?非常感谢你
我在js中有以下字符串。
*"form-uploads/2015 Perry's Awärds Letter.jpg"*
Run Code Online (Sandbox Code Playgroud)
它有一个ä符号。
当我使用btoa(在chrome中)在js中对其进行编码时,我得到以下信息: “ Zm9ybS11cGxvYWRzLzIwMTUgUGVycnkncyBBd + RyZHMgTGV0dGVyLmpwZw ==”
当我尝试用python对其进行解码时,我得到以下信息:
In[16]: base64.b64decode('Zm9ybS11cGxvYWRzLzIwMTUgUGVycnkncyBBd+RyZHMgTGV0dGVyLmpwZw==')
Out[16]: "form-uploads/2015 Perry's Aw\xe4rds Letter.jpg"
Run Code Online (Sandbox Code Playgroud)
因此,ä迷路了,如果我尝试对该字符串进行解码,则会utf-8出错。
In[18]: base64.b64decode('Zm9ybS11cGxvYWRzLzIwMTUgUGVycnkncyBBd+RyZHMgTGV0dGVyLmpwZw==').decode('utf-8')
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe4 in position 28: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)
解码后如何i获得正确utf-8 ä的python代码?
我理解的是如何直觉bool([])是False.
但为什么bool([[]]),bool([[[]]])等等,都是True?这背后的逻辑是什么?
有两个像这样的数组
x = [a,b]
y = [p,q,r]
Run Code Online (Sandbox Code Playgroud)
我需要将此乘以一个c类似这样的乘积
c = [a*p, a*q, a*r, b*p, b*q, b*r]
Run Code Online (Sandbox Code Playgroud)
但是x*y,出现以下错误,
ValueError: operands could not be broadcast together with shapes (2,) (3,)
Run Code Online (Sandbox Code Playgroud)
我可以做这样的事情
for i in range(len(x)):
for t in range(len(y)):
c.append(x[i] * y[t]
Run Code Online (Sandbox Code Playgroud)
但真正的长度我x和y相当大有啥做出这样的乘法没有循环的最有效方式。
例如,我有数字n=5作为输入,我想打印我的输出像这样: -
1
22
333
4444
Run Code Online (Sandbox Code Playgroud)
但是使用for循环一次而不在程序中的任何地方使用字符串
也许这之前被问过,在这种情况下我会删除这个问题,但我有两个列表:
occurence_list = [1, 2, 3, 4, 5]
value_list = [10, 20, 30, 40, 50]
Run Code Online (Sandbox Code Playgroud)
我希望每个值看起来与另一个列表中相同索引的值相同:
result = [10, 20, 20, 30, 30, 30, 40, 40, 40, 40, 50, 50, 50, 50, 50]
Run Code Online (Sandbox Code Playgroud)
如何才能做到这一点?
为什么max()Python 3.4 中的函数在值列表中给出的值小于预期值?
示例1,其中期望值为'marco':
>>> max('zara', 'marco')
'zara'
Run Code Online (Sandbox Code Playgroud)
示例2,其中期望值为'cherries':
>>> max('apples', 'oranges', 'cherries', 'banana')
'oranges'
Run Code Online (Sandbox Code Playgroud) python ×11
dictionary ×1
django ×1
encoding ×1
file ×1
for-loop ×1
lambda ×1
list ×1
max ×1
nested-loops ×1
numpy ×1
performance ×1
python-2.x ×1
python-3.x ×1
repeat ×1
string ×1
unicode ×1
utf-8 ×1