Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: Convert list to another form but providing same information Date: Mon, 21 Mar 2016 19:21:21 +0000 Lines: 53 Message-ID: References: <1010f2cb-21f9-495b-8af4-03ad209b4c1e@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de YdFHuxyYUAuCe6L6mcMb0gtRlUoS1Tsk6r/mEWnAxJ8g== 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; 'resulting': 0.04; 'from:addr:yahoo.co.uk': 0.05; 'subject:form': 0.07; '[0,': 0.09; 'collections': 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; 'received:194.126': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:Convert': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'element': 0.18; 'language': 0.19; '>>>': 0.20; 'keys': 0.22; 'lawrence': 0.22; 'seems': 0.23; 'advance.': 0.23; 'elements': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'subject:list': 0.26; 'header:X -Complaints-To:1': 0.26; 'be:': 0.29; 'index,': 0.29; 'convert': 0.29; "i'm": 0.30; 'work.': 0.30; 'skip:[ 10': 0.31; 'certain': 0.31; 'language.': 0.32; 'similar': 0.33; 'skip:d 20': 0.34; 'list': 0.34; 'item': 0.35; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'list.': 0.37; 'someone': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'hello,': 0.40; 'hope': 0.61; 'received:194': 0.61; 'charset:windows-1252': 0.62; 'our': 0.64; 'therefore': 0.67; 'day': 0.67; 'counts': 0.81; 'dict.': 0.84; 'pythonistas,': 0.84; 'dealt': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 248.80.126.194.pool.dsl.daisyplc.net User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 In-Reply-To: <1010f2cb-21f9-495b-8af4-03ad209b4c1e@googlegroups.com> 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:105394 On 21/03/2016 18:26, 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. > > 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? > > Thanks in advance. > >>> from collections import Counter >>> counts = Counter([6,19,19,21,21,21]) >>> counts Counter({21: 3, 19: 2, 6: 1}) >>> weird_list = [0]*32 >>> weird_list [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >>> for index, count in counts.items(): ... weird_list[index] = count ... >>> weird_list [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence