Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Convert list to another form but providing same information Date: Mon, 21 Mar 2016 20:22:12 +0100 Organization: None Lines: 66 Message-ID: References: <1010f2cb-21f9-495b-8af4-03ad209b4c1e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de BpqnzygIl+/ba8RnIaXIZwSTwOymGnL5rzeQHgbS60TQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'modify': 0.04; 'resulting': 0.04; '[0]': 0.07; 'rewrite': 0.07; 'subject:form': 0.07; 'collections': 0.09; 'mutable': 0.09; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:[ 30': 0.09; 'subject:same': 0.09; 'index': 0.13; '(meaning': 0.16; '...]': 0.16; 'immutable,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Convert': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'element': 0.18; 'fix': 0.21; 'keys': 0.22; 'seems': 0.23; 'elements': 0.23; 'second': 0.24; 'import': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X-Complaints-To:1': 0.26; 'values': 0.28; 'interface': 0.29; 'be:': 0.29; 'behaviour': 0.29; 'key,': 0.29; 'convert': 0.29; "i'm": 0.30; 'work.': 0.30; 'skip:[ 10': 0.31; 'certain': 0.31; 'problem': 0.33; 'similar': 0.33; 'skip:d 20': 0.34; 'list': 0.34; 'could': 0.35; 'replace': 0.35; 'item': 0.35; 'should': 0.36; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'list.': 0.37; 'someone': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'hello,': 0.40; 'hope': 0.61; 'skip:n 10': 0.62; 'times': 0.63; 'therefore': 0.67; 'day': 0.67; 'clearer': 0.84; 'dict.': 0.84; 'dealt': 0.91; 'lists:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8f8e.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:105395 Maurice wrote: > Hello, hope everything is okay. I think someone might have dealt with a > similar issue I'm having. > > Basically I wanna do the following: > > I have a list such [6,19,19,21,21,21] (FYI this is the item of a certain > key in the dictionary) > > And I need to convert it to a list of 32 elements (meaning days of the > month however first element ie index 0 or day zero has no meaning - > keeping like that for simplicity's sake). Therefore the resulting list > should be: > [0,0,0,0,0,0,1,0,0,0...,2,0,3,0...0] > > So the list index should tell how many occurrences of a certain day in the > original list. > > My attempt: > > weirdList = [[0]*32]*len(dict_days) #list's length should be how many keys > in the dict. Rewrite the above as inner = [0] * 32 weirdList = [inner] * len(dict_days) Now it should be clearer that weirdList is [inner, inner, inner, ...] i. e. len(dict_days) times the same list of 32 zeros. When you modify weirdList[0] you also modify weirdList[1] etc. because these are the actually the same list. For the inner list this is not a problem because 0 is immutable, so you can only replace it. If the inner list contained a mutable object you'd see the same "weird" behaviour you see for the outer list. The fix is to use multiple inner lists: not_so_weirdList = [[0]*32 for _ in range(len(dict_days))] > counter = 0 > k = 0 > for key in dict_days.keys(): > for i in range(1,32): > if i in dict_days[key]: > counter = dict_days[key].count(i) > weirdList[k][i] = counter > dict_days[key] = weirdList[k] > k+=1 > > However it does not work. weirdList seems to be always the same? Instead of the list you could use a counter and get basically the same interface as long as you ensure that the second index satisfies 0<=index<32: import collections for key, values in dict_days.items(): dict_days[key] = collections.Counter(values)