나는 다음과 같은 규칙에 따라, dicts의 목록을 정리할 :삭제 dicts - 파이썬
이1) dicts의 목록이 이미 정렬되어 있으므로 이전 dicts가 선호됩니다.
2) 하위 명령에서 ['name']
및 ['code']
문자열 값이 목록의 상위에있는 동일한 상위 키 값과 일치하고 두 dict 사이의 의 차이 절대 값이 < 2
인 경우; 해당 사전은 이전 dict의 복제본으로 간주되며 목록에서 삭제됩니다.
{
'name':"ItemName",
'code':"AAHFGW4S",
'from':"NDLS",
'to':"BCT",
'cost':str(29.95)
}
이 같은 중복을 제거하는 가장 좋은 방법은 무엇입니까 : 여기
은 dicts의 목록에서 하나 딕셔너리입니까?
이for i, d in enumerate(dictList):
# iterate through the list of dicts, starting with the first
for k,v in d.iteritems():
# for each key-value pair in this dict...
for d2 in dictList[i:]:
# check against all of the other dicts "beneath" it
# eg,
# if d['name'] == d2['name'] and d['code'] == d2['code']:
# --check the cost stuff here--
더 나은 기술적 방법 (특히 큰 목록에서 메모리 사용량을 줄이기 위해'yield '를 사용하는 Jochen의 대답)이 있지만, 나는 당신의 방법을 더 잘 읽을 수있다. – Pranab