Jump to content
Korean Random
ShuraBB

Почему результат разный?

Recommended Posts

Имеем такой вот простой код:
 

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}}

Я чего-то не понимаю?

Share this post


Link to post

Short link
Share on other sites

сделай 

for i in Sections:
    print i

 

увидишь ответ.

 

А ещё лучше сразу напиши что хотел в итоге получить. т.к. нипанятнааа....

Edited by spoter

Share this post


Link to post

Short link
Share on other sites

 

 

Я чего-то не понимаю?

 

St1 = temp - это присваивание ссылки на словарь, лежащий в temp, и так с каждой итерацией

St2 = {'Start': 12233} - а это создание нового словаря при каждой итерации

  • Upvote 1

Share this post


Link to post

Short link
Share on other sites

>St1 = temp - это присваивание ссылки на словарь, лежащий в temp, и так с каждой итерацией
Семен Семеныч! Блин. Есть всё-таки случаи когда предыдущие знания и опыт бывают не полезными ;-)

>Если нужен результат как у St2, то
Меня устраивает и St2 = {'Start': 12233}
Просто в одном месте было так а в другом так, а в результате грабли.

Спасибо всем!

Share this post


Link to post

Short link
Share on other sites

Если нужен результат как у St2, то:

у словаря есть метод copy(), зачем усложнять?
  • Upvote 1

Share this post


Link to post

Short link
Share on other sites
у словаря есть метод copy(), зачем усложнять?

Я в python абсолютный новичек, приведите пожалуйста пример.

 

P.S. разобрался.

Edited by ktulho

Share this post


Link to post

Short link
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...