per9000 wrote:
> A good idea could be to change the header of the first one (if you want
> the option to start counting from zero) into:
> def dict_add(inlist,indict={},init=1):
make that
def dict_add(inlist, indict=None, init=1):
if indict is None:
indict = {}
See "5. Mutable defau
Dear counters,
I have also encountered the above problem, and I frequently use the two
blocks of code below.
The first one is commented already - except that this version does not
start from an empty dict.
The second "inverts" the first one. Meaning that a dict in word2hits
style:
my_dict['the'
Justin Azoff a écrit :
> Bruno Desthuilliers wrote:
>
>> My solution seems to be faster than Paul's
>>one (but slower than bearophile's), be it on small, medium or large lists.
>
>
> Your version is only fast on lists with a very small number of unique
> elements.
>
> changing mklist to have
>
Justin Azoff a écrit :
> Bruno Desthuilliers wrote:
>
>>And of course, I was right. My solution seems to be faster than Paul's
>>one (but slower than bearophile's), be it on small, medium or large lists.
>
>
> Your version is only fast on lists with a very small number of unique
> elements.
>
>
Paul Rubin a écrit :
> Bruno Desthuilliers <[EMAIL PROTECTED]> writes:
>
>>And of course, I was right. My solution seems to be faster than Paul's
>>one (but slower than bearophile's), be it on small, medium or large
>>lists.
>>
>>nb: A is mine, B is Paul's and C is bearophile's, and the number aft
Bruno Desthuilliers wrote:
> And of course, I was right. My solution seems to be faster than Paul's
> one (but slower than bearophile's), be it on small, medium or large lists.
Your version is only fast on lists with a very small number of unique
elements.
changing mklist to have
items = range(64
Bruno Desthuilliers <[EMAIL PROTECTED]> writes:
> And of course, I was right. My solution seems to be faster than Paul's
> one (but slower than bearophile's), be it on small, medium or large
> lists.
>
> nb: A is mine, B is Paul's and C is bearophile's, and the number after
> is the size of the li
Bruno Desthuilliers a écrit :
> Kent Johnson a écrit :
>
>> sophie_newbie wrote:
>>
>>> Hey Bruno,
>>>
>>> I got an invalid syntax error when i tried using your "str_counts =
>>> dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe
>>> there is a missing bracket or comma? Or mayb
Kent Johnson a écrit :
> sophie_newbie wrote:
>
>> Hey Bruno,
>>
>> I got an invalid syntax error when i tried using your "str_counts =
>> dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe
>> there is a missing bracket or comma? Or maybe I need to import
>> something.
>
>
>
With CPython this is probably the faster version, the version with
list.count() has to be used only if you are sure to always have a short
input list, because it's O(n^2):
str_list = ['StringA', 'StringC', 'StringB',
'StringA', 'StringC', 'StringD',
'StringA' ]
str_count
sophie_newbie a écrit :
> Hey Bruno,
>
> I got an invalid syntax error when i tried using your "str_counts =
> dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe
> there is a missing bracket or comma?
Yes, sorry, see Kent's post for the correction.
> Or maybe I need to impor
sophie_newbie wrote:
> Hey Bruno,
>
> I got an invalid syntax error when i tried using your "str_counts =
> dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe
> there is a missing bracket or comma? Or maybe I need to import
> something.
It should be
str_counts = dict((s, str_l
Hi Paul,
Ur bit of code works, thanks so much, just for anyone reading this use
'h = {}' instead of h = 0.
Thanks
--
http://mail.python.org/mailman/listinfo/python-list
Hey Bruno,
I got an invalid syntax error when i tried using your "str_counts =
dict((s, str_list.count(s) for s in set(str_list))" bit of code? Maybe
there is a missing bracket or comma? Or maybe I need to import
something.
Thanks so much for your help.
--
http://mail.python.org/mailman/listinf
sophie_newbie a écrit :
> I have a list a little something like this:
>
> StringA
> StringC
> StringB
> StringA
> StringC
> StringD
> StringA
> ...
> etc.
>
> Basically I was wondering if there was an easy way to return how many
> of each string are in the list, something like this:
>
> StringA
"sophie_newbie" <[EMAIL PROTECTED]> writes:
> I suppose that the easiest way to do that is to convert it to a 2
> dimensional array? Is there any easy way?
Use a hash table. Untested:
h = 0
for x in your_list:
h[x] = h.get(x, 0) + 1
You can then use something like sorted(h.items())
I have a list a little something like this:
StringA
StringC
StringB
StringA
StringC
StringD
StringA
...
etc.
Basically I was wondering if there was an easy way to return how many
of each string are in the list, something like this:
StringA - 3
StringB - 1
StringC - 2
StringD - 1
I suppose that
17 matches
Mail list logo