Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105395
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Convert list to another form but providing same information |
| Date | 2016-03-21 20:22 +0100 |
| Organization | None |
| Message-ID | <mailman.458.1458588144.12893.python-list@python.org> (permalink) |
| References | <1010f2cb-21f9-495b-8af4-03ad209b4c1e@googlegroups.com> |
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)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Convert list to another form but providing same information Maurice <mauricioliveiraguarda@gmail.com> - 2016-03-21 11:26 -0700
Re: Convert list to another form but providing same information Maurice <mauricioliveiraguarda@gmail.com> - 2016-03-21 11:30 -0700
Re: Convert list to another form but providing same information Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-21 19:24 +0000
Re: Convert list to another form but providing same information anantguptadbl@gmail.com - 2016-03-22 07:23 -0700
Re: Convert list to another form but providing same information Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-21 19:21 +0000
Re: Convert list to another form but providing same information Peter Otten <__peter__@web.de> - 2016-03-21 20:22 +0100
Re: Convert list to another form but providing same information Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-21 20:03 +0000
Re: Convert list to another form but providing same information Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 14:12 -0600
Re: Convert list to another form but providing same information Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-03-22 00:31 +0000
Re: Convert list to another form but providing same information Chris Angelico <rosuav@gmail.com> - 2016-03-22 11:58 +1100
Re: Convert list to another form but providing same information Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-22 15:34 +1100
Re: Convert list to another form but providing same information Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-21 14:14 -0600
Re: Convert list to another form but providing same information Steven D'Aprano <steve@pearwood.info> - 2016-03-22 11:13 +1100
Re: Convert list to another form but providing same information Paul Rubin <no.email@nospam.invalid> - 2016-03-21 18:35 -0700
Re: Convert list to another form but providing same information Steven D'Aprano <steve@pearwood.info> - 2016-03-22 21:49 +1100
csiph-web