ShuraBB Posted March 24, 2016 Share 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 Link to comment Short link Share on other sites More sharing options...
spoter Posted March 24, 2016 Share Posted March 24, 2016 (edited) сделай for i in Sections: print i увидишь ответ. А ещё лучше сразу напиши что хотел в итоге получить. т.к. нипанятнааа.... Edited March 24, 2016 by spoter @ Quote Link to comment Short link Share on other sites More sharing options...
StranikS_Scan Posted March 24, 2016 Share Posted March 24, 2016 Я чего-то не понимаю? St1 = temp - это присваивание ссылки на словарь, лежащий в temp, и так с каждой итерацией St2 = {'Start': 12233} - а это создание нового словаря при каждой итерации 1 @ Quote Link to comment Short link Share on other sites More sharing options...
ktulho Posted March 24, 2016 Share Posted March 24, 2016 @ShuraBB Если нужен результат как у St2, то: import copy St1[i] = copy.copy(temp) 1 @ Quote Link to comment Short link Share on other sites More sharing options...
ShuraBB Posted March 24, 2016 Author Share Posted March 24, 2016 >St1 = temp - это присваивание ссылки на словарь, лежащий в temp, и так с каждой итерациейСемен Семеныч! Блин. Есть всё-таки случаи когда предыдущие знания и опыт бывают не полезными ;-)>Если нужен результат как у St2, тоМеня устраивает и St2 = {'Start': 12233}Просто в одном месте было так а в другом так, а в результате грабли.Спасибо всем! @ Quote Link to comment Short link Share on other sites More sharing options...
SkepticalFox Posted March 24, 2016 Share Posted March 24, 2016 Если нужен результат как у St2, то:у словаря есть метод copy(), зачем усложнять? 1 @ Quote Link to comment Short link Share on other sites More sharing options...
ktulho Posted March 24, 2016 Share Posted March 24, 2016 (edited) у словаря есть метод copy(), зачем усложнять? Я в python абсолютный новичек, приведите пожалуйста пример. P.S. разобрался. Edited March 24, 2016 by ktulho @ Quote Link to comment Short link Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.