티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

What is the difference between dict.items() and dict.iteritems() in Python2?

파이썬2에서 dict.items()와 dict.iteritems()의 차이점은 무엇입니까?

 문제 내용 

Are there any applicable differences between dict.items() and dict.iteritems()?

dict.items()와 dict.iter items() 사이에 적용 가능한 차이점이 있습니까?

 

From the Python docs:

Python 문서:

 

dict.items(): Return a copy of the dictionary’s list of (key, value) pairs.

dict.iteritems(): Return an iterator over the dictionary’s (key, value) pairs.

dict.items(): 딕셔너리의 (키, 값) 쌍 목록 복사본을 반환합니다.
dict.iteritems(): 딕셔너리의  (키, 값) 쌍에 대해 반복기를 반환합니다.

 

If I run the code below, each seems to return a reference to the same object. Are there any subtle differences that I am missing?

아래 코드를 실행하면 각각 동일한 개체에 대한 참조를 반환하는 것 같습니다. 제가 놓치고 있는 미묘한 차이가 있나요?
#!/usr/bin/python

d={1:'one',2:'two',3:'three'}
print 'd.items():'
for k,v in d.items():
   if d[k] is v: print '\tthey are the same object' 
   else: print '\tthey are different'

print 'd.iteritems():'   
for k,v in d.iteritems():
   if d[k] is v: print '\tthey are the same object' 
   else: print '\tthey are different'   

 

Output:

출력:
d.items():
    they are the same object
    they are the same object
    they are the same object
d.iteritems():
    they are the same object
    they are the same object
    they are the same object

 

 

 높은 점수를 받은 Solution 

It's part of an evolution.

진화의 일부입니다.

 

Originally, Python items() built a real list of tuples and returned that. That could potentially take a lot of extra memory.

원래 파이썬 items()은 튜플의 실제 리스트를 만들고 그것을 반환했습니다. 잠재적으로 많은 메모리가 필요할 수 있습니다.

 

Then, generators were introduced to the language in general, and that method was reimplemented as an iterator-generator method named iteritems(). The original remains for backwards compatibility.

그후, 생성자들은 일반적으로 언어에 도입되었고 해당 메서드는 iteritems()라는 반복자-생성기 메서드로 다시 구현되었습니다. 원본은 이전 버전과의 호환성을 위해 남아 있습니다.

 

One of Python 3’s changes is that items() now return views, and a list is never fully built. The iteritems() method is also gone, since items() in Python 3 works like viewitems() in Python 2.7.

Python 3의 변경 사항 중 하나는 이제 items()가 뷰를 반환하고 리스트가 완전히 빌드되지 않는다는 것입니다. Python 3의 items()가 Python 2.7의 viewitems()처럼 작동하기 때문에 iteritems() 메서드도 사라졌습니다.

 

 

 

 가장 최근 달린 Solution 

dict.iteritems is gone in Python3.x So use iter(dict.items()) to get the same output and memory alocation

dict.iteritems는 Python3.x에서 사라졌습니다. 따라서 동일한 출력과 메모리 할당을 얻으려면 iter(dict.items())를 사용하세요.

 

 

 

출처 : https://stackoverflow.com/questions/10458437/what-is-the-difference-between-dict-items-and-dict-iteritems-in-python2

반응형
댓글
공지사항
최근에 올라온 글