ShuraBB 1,151 Posted March 24, 2016 Имеем такой вот простой код: St1 = {} St2 = {} Sections = ('Current', 'Custom', 'Overall') print 'St1:', St1 print 'St2:', St2 temp = {'Start': 12233} for i in Sections: St1[i] = temp St2[i] = {'Start': 12233} print 'St1:', St1 print 'St2:', St2 for i in Sections: St1[i]['Counter'] = St1[i].get('Counter', 0) + 1 St2[i]['Counter'] = St2[i].get('Counter', 0) + 1 print 'St1:', St1 print 'St2:', St2 И вот такой странный результат: INFO: St1: {} INFO: St2: {} INFO: St1: {'Current': {'Start': 12233}, 'Overall': {'Start': 12233}, 'Custom': {'Start': 12233}} INFO: St2: {'Current': {'Start': 12233}, 'Overall': {'Start': 12233}, 'Custom': {'Start': 12233}} INFO: St1: {'Current': {'Start': 12233, 'Counter': 3}, 'Overall': {'Start': 12233, 'Counter': 3}, 'Custom': {'Start': 12233, 'Counter': 3}} INFO: St2: {'Current': {'Start': 12233, 'Counter': 1}, 'Overall': {'Start': 12233, 'Counter': 1}, 'Custom': {'Start': 12233, 'Counter': 1}} Я чего-то не понимаю? Quote Share this post Link to post Short link Share on other sites
spoter 1,402 #320547 Posted March 24, 2016 (edited) сделай for i in Sections: print i увидишь ответ. А ещё лучше сразу напиши что хотел в итоге получить. т.к. нипанятнааа.... Edited March 24, 2016 by spoter Quote Share this post Link to post Short link Share on other sites
StranikS_Scan 4,212 #320549 Posted March 24, 2016 Я чего-то не понимаю? St1 = temp - это присваивание ссылки на словарь, лежащий в temp, и так с каждой итерацией St2 = {'Start': 12233} - а это создание нового словаря при каждой итерации 1 Quote Share this post Link to post Short link Share on other sites
ktulho 5,793 #320552 Posted March 24, 2016 @ShuraBB Если нужен результат как у St2, то: import copy St1[i] = copy.copy(temp) 1 Quote Share this post Link to post Short link Share on other sites
ShuraBB 1,151 #320554 Posted March 24, 2016 >St1 = temp - это присваивание ссылки на словарь, лежащий в temp, и так с каждой итерациейСемен Семеныч! Блин. Есть всё-таки случаи когда предыдущие знания и опыт бывают не полезными ;-)>Если нужен результат как у St2, тоМеня устраивает и St2 = {'Start': 12233}Просто в одном месте было так а в другом так, а в результате грабли.Спасибо всем! Quote Share this post Link to post Short link Share on other sites
SkepticalFox 1,445 #320555 Posted March 24, 2016 Если нужен результат как у St2, то:у словаря есть метод copy(), зачем усложнять? 1 Quote Share this post Link to post Short link Share on other sites
ktulho 5,793 #320560 Posted March 24, 2016 (edited) у словаря есть метод copy(), зачем усложнять? Я в python абсолютный новичек, приведите пожалуйста пример. P.S. разобрался. Edited March 24, 2016 by ktulho Quote Share this post Link to post Short link Share on other sites