FAQ
Hi! Outside for silly questions!

Code Python:

import collections
a = [1,1,2,2,3,3]

print [x for x, y in collections.Counter(a).items() if y > 1]
[1, 2, 3]

How to translate into code Golang?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Gyepi SAM at Mar 24, 2014 at 9:52 am

    On Mon, Mar 24, 2014 at 02:06:36AM -0700, Игорь Авдошкин wrote:
    Hi! Outside for silly questions!

    Code Python:

    import collections
    a = [1,1,2,2,3,3]

    print [x for x, y in collections.Counter(a).items() if y > 1]
    [1, 2, 3]

    How to translate into code Golang?
    I am guessing that collections.Counter(a) counts the number of items in the
    collection and .items() returns it so

         collections.Counter([1,1,2,2,3,3]).items()

    returns something like

         ((1,2), (2,2), (3,))

    There are two ways to do this depending on whether the result order matters or
    not. If it does, then one needs to track the position of a value's first
    mention.

         http://play.golang.org/p/gk1I_f3HF9

    Note that the two functions return the same order because map[int] appears to iterate in
    order, but if order matters, you should not depend on that behaviour.

    -Gyepi

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Игорь Авдошкин at Mar 24, 2014 at 9:56 am
    A lot of code is obtained with respect to Python!

    понедельник, 24 марта 2014 г., 17:52:42 UTC+8 пользователь Gyepi SAM
    написал:
    On Mon, Mar 24, 2014 at 02:06:36AM -0700, Игорь Авдошкин wrote:
    Hi! Outside for silly questions!

    Code Python:

    import collections
    a = [1,1,2,2,3,3]

    print [x for x, y in collections.Counter(a).items() if y > 1]
    [1, 2, 3]

    How to translate into code Golang?
    I am guessing that collections.Counter(a) counts the number of items in
    the
    collection and .items() returns it so

    collections.Counter([1,1,2,2,3,3]).items()

    returns something like

    ((1,2), (2,2), (3,))

    There are two ways to do this depending on whether the result order
    matters or
    not. If it does, then one needs to track the position of a value's first
    mention.

    http://play.golang.org/p/gk1I_f3HF9

    Note that the two functions return the same order because map[int] appears
    to iterate in
    order, but if order matters, you should not depend on that behaviour.

    -Gyepi
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Mar 24, 2014 at 10:07 am

    On Mon, Mar 24, 2014 at 10:56 AM, Игорь Авдошкин wrote:
    A lot of code is obtained with respect to Python!
    That's okay. Go attempts to not hide the cost of magic capabilities:
    http://play.golang.org/p/2sU0fMAQN9

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Mar 24, 2014 at 10:09 am

    On Mon, Mar 24, 2014 at 11:06 AM, Jan Mercl wrote:
    That's okay. Go attempts to not hide the cost of magic capabilities:
    http://play.golang.org/p/2sU0fMAQN9
    Even shorter: http://play.golang.org/p/hLYQdcenZx

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Khomoutov at Mar 24, 2014 at 10:18 am

    On Mon, 24 Mar 2014 02:56:51 -0700 (PDT) Игорь Авдошкин wrote:

    A lot of code is obtained with respect to Python!
    1) I, for one, do not understand

        print [x for x, y in collections.Counter(a).items() if y > 1]

        without looking at the docs. So, yeah, I could contrive an
        even more obscure encantation in, say, Perl. Using Haskell
        presumably would gain an even more obscure solution.

        I mean, the property of being consice does not imply the
        properties of being lucid or even understandable.

    2) A simpler version of the code for count == 1:

        http://play.golang.org/p/NjYCsd1Vwc

    3) Why not return to Python then?

        No, I'm not really trolling but I'm having hard time making
        sense of the comment in the style of the one I'm replying to.

        If you're not feeling "right" when working with a particular
        language, do not use it. AFAIK, Go is not yet that ubuquitous
        as, say, Java or C++, so I don't beleive you're forced to
        program in Go for living.

        On the other hand, Go strives to have as few as possible places
        with *hidden performance costs* in the code, and it also strives
        to be simple. And have fast compilation speed at the same time.
        So the line has to be drawn somewhere, and IIUC why there's no
        list comprehensions and stuff like that.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Oleku Konko at Mar 24, 2014 at 9:43 pm
    Why not use *!ok <http://play.golang.org/p/H2zIt_tHFb> *
    On Monday, March 24, 2014 11:18:12 AM UTC+1, Konstantin Khomoutov wrote:

    On Mon, 24 Mar 2014 02:56:51 -0700 (PDT)
    Игорь Авдошкин <avdo...@gmail.com <javascript:>> wrote:
    A lot of code is obtained with respect to Python!
    1) I, for one, do not understand

    print [x for x, y in collections.Counter(a).items() if y > 1]

    without looking at the docs. So, yeah, I could contrive an
    even more obscure encantation in, say, Perl. Using Haskell
    presumably would gain an even more obscure solution.

    I mean, the property of being consice does not imply the
    properties of being lucid or even understandable.

    2) A simpler version of the code for count == 1:

    http://play.golang.org/p/NjYCsd1Vwc

    3) Why not return to Python then?

    No, I'm not really trolling but I'm having hard time making
    sense of the comment in the style of the one I'm replying to.

    If you're not feeling "right" when working with a particular
    language, do not use it. AFAIK, Go is not yet that ubuquitous
    as, say, Java or C++, so I don't beleive you're forced to
    program in Go for living.

    On the other hand, Go strives to have as few as possible places
    with *hidden performance costs* in the code, and it also strives
    to be simple. And have fast compilation speed at the same time.
    So the line has to be drawn somewhere, and IIUC why there's no
    list comprehensions and stuff like that.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyepi SAM at Mar 24, 2014 at 10:33 am

    On Mon, Mar 24, 2014 at 02:56:51AM -0700, Игорь Авдошкин wrote:
    A lot of code is obtained with respect to Python!
    Under the covers, the python code is doing exactly the same thing but more generically,
    which means it has way more code (and is slower).
    You have to think about the code the language is running on your behalf.
    The abstraction is not free, merely convenient.

    FWIW, if you were going from Go to C, you'd see the same thing. However,
    you could optimize your hash tables or replace them with heaps, count-mins or
    some other more specialized data structure. Not to say that you're precluded
    from doing so with Python or Go; it's just less common and is frequently unnecessary.

    -Gyepi

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Mar 24, 2014 at 10:28 am
    A quick peruse of the collections docs shows that order is not guaranteed, so a map[int]struct{} (or int if the count is actually wanted) in a loop does what the OP wants.

    http://play.golang.org/p/52hBWOUom4
    On 24/03/2014, at 8:22 PM, "Gyepi SAM" wrote:
    On Mon, Mar 24, 2014 at 02:06:36AM -0700, Игорь Авдошкин wrote:
    Hi! Outside for silly questions!

    Code Python:

    import collections
    a = [1,1,2,2,3,3]

    print [x for x, y in collections.Counter(a).items() if y > 1]
    [1, 2, 3]

    How to translate into code Golang?
    I am guessing that collections.Counter(a) counts the number of items in the
    collection and .items() returns it so

    collections.Counter([1,1,2,2,3,3]).items()

    returns something like

    ((1,2), (2,2), (3,))

    There are two ways to do this depending on whether the result order matters or
    not. If it does, then one needs to track the position of a value's first
    mention.

    http://play.golang.org/p/gk1I_f3HF9

    Note that the two functions return the same order because map[int] appears to iterate in
    order, but if order matters, you should not depend on that behaviour.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyepi SAM at Mar 24, 2014 at 10:56 am

    On Mon, Mar 24, 2014 at 10:28:10AM +0000, Dan Kortschak wrote:
    A quick peruse of the collections docs shows that order is not guaranteed, so a map[int]struct{} (or int if the count is actually wanted) in a loop does what the OP wants.

    http://play.golang.org/p/52hBWOUom4
    Your code doesn't filter for a min count. This matters because the output array
    wouldn't necessarily be the same length as the counter map. You need to "complexify"
    it a bit more ;)

    -Gyepi

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Mar 24, 2014 at 11:03 am
    I completely missed that. I'll claim that's a good reason to not use the kind of construction offered by the OP. Yes, that's what I'll do.
    On 24/03/2014, at 9:26 PM, "Gyepi SAM" wrote:

    Your code doesn't filter for a min count. This matters because the output array
    wouldn't necessarily be the same length as the counter map. You need to "complexify"
    it a bit more ;)
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dan Kortschak at Mar 24, 2014 at 11:41 am
    That's not really a huge issue. I guess it depends on the trade off you want to make.

    Single allocation possible waste: http://play.golang.org/p/xrnZ_mampb
    Single allocation more time: http://play.golang.org/p/K9Sy3E6Gzv
    Multiple allocation and waste, less code: http://play.golang.org/p/NHnCGa_Pe3

    and my favourite, maximal waste: http://play.golang.org/p/pa1tOaGPUf
    On 24/03/2014, at 9:34 PM, "Dan Kortschak" wrote:

    This matters because the output array wouldn't necessarily be the same length as the counter map.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Roger peppe at Mar 24, 2014 at 12:20 pm
    Slightly less working space used:

    http://play.golang.org/p/dL8uVMZrgM
    On 24 March 2014 11:41, Dan Kortschak wrote:
    That's not really a huge issue. I guess it depends on the trade off you want to make.

    Single allocation possible waste: http://play.golang.org/p/xrnZ_mampb
    Single allocation more time: http://play.golang.org/p/K9Sy3E6Gzv
    Multiple allocation and waste, less code: http://play.golang.org/p/NHnCGa_Pe3

    and my favourite, maximal waste: http://play.golang.org/p/pa1tOaGPUf
    On 24/03/2014, at 9:34 PM, "Dan Kortschak" wrote:

    This matters because the output array wouldn't necessarily be the same length as the counter map.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rjeczalik at Mar 24, 2014 at 10:03 am
    http://play.golang.org/p/BiPyY_T0_8

    On 24 March 2014 10:06, Игорь Авдошкин wrote:

    Hi! Outside for silly questions!

    Code Python:

    import collections
    a = [1,1,2,2,3,3]

    print [x for x, y in collections.Counter(a).items() if y > 1]
    [1, 2, 3]

    How to translate into code Golang?

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Mar 24, 2014 at 2:32 pm
    As a Python guy, I would write this differently. Readability is important:

    from collections import Counter

    a = [1, 1, 2, 2, 3, 3]

    counter = Counter()
    for x in a:
         counter[x] += 1

    print [i for i, count in counter.most_common() if count > 1]


    I have learned that even though the shorter version does the same thing and
    doesn't take much longer to read (and understand), you're going to be
    working with other people, perhaps less skilled, a lot. Also, as with
    anything else, it takes more of your own time and attention to understand
    it later, even when you wrote it.

    I know this doesn't contribute much to the thread, but I wanted to give a
    counter-example to the Perl-like use of Python, which isn't indicative of
    Pythonic code.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Gyepi SAM at Mar 24, 2014 at 3:16 pm

    On Mon, Mar 24, 2014 at 10:32:03AM -0400, Shawn Milochik wrote:
    As a Python guy, I would write this differently. Readability is important:

    from collections import Counter

    a = [1, 1, 2, 2, 3, 3]

    counter = Counter()
    for x in a:
    counter[x] += 1

    print [i for i, count in counter.most_common() if count > 1]
    Counter is altogether unnecessary here...

       a = [1, 1, 2, 2, 3, 3]

       counter = {}

       for v in a:
           c = counter.setdefault(v, 0)
           counter[v] += 1
           if c == 1:
             print v,

    Which seems more Pythonic and also more like some of the Go solutions seen
    earlier.
    I know this doesn't contribute much to the thread, but I wanted to give a
    counter-example to the Perl-like use of Python, which isn't indicative of
    Pythonic code.
    Careful with that implicit claim that Perl is inscrutable, unreadable or both.
    There perl folk lurking about; perhaps even amongst us.

    -Gyepi
    perl -pe 's/(\d{2})/pack(q{W},$1)/eg'<<<74658072

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Shawn Milochik at Mar 24, 2014 at 3:32 pm

    On Mon, Mar 24, 2014 at 11:15 AM, Gyepi SAM wrote:
    Careful with that implicit claim that Perl is inscrutable, unreadable or
    both.
    There perl folk lurking about; perhaps even amongst us.

    -Gyepi
    perl -pe 's/(\d{2})/pack(q{W},$1)/eg'<<<74658072

    I use Perl all the time for one-liners. I was referring to the tendency
    (you could even say predilection) Perl folk have for tending towards golf
    in everyday code. I used to use Perl quite a bit, but switched away to
    Python after being introduced to the language and finding it was a much
    better fit for my brain.

    You're right, of course, about the counter being unnecessary. I didn't
    think it through -- I just tried to simplify what was there.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michal at Mar 24, 2014 at 2:45 pm
    Very short, recursive and funny: http://play.golang.org/p/Ub6-NgXUCv

    W dniu poniedziałek, 24 marca 2014 10:06:36 UTC+1 użytkownik Игорь Авдошкин
    napisał:
    Hi! Outside for silly questions!

    Code Python:

    import collections
    a = [1,1,2,2,3,3]

    print [x for x, y in collections.Counter(a).items() if y > 1]
    [1, 2, 3]

    How to translate into code Golang?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Michal at Mar 24, 2014 at 2:49 pm
    sorry: http://play.golang.org/p/nLpUV2Y5zl

    W dniu poniedziałek, 24 marca 2014 10:06:36 UTC+1 użytkownik Игорь Авдошкин
    napisał:
    Hi! Outside for silly questions!

    Code Python:

    import collections
    a = [1,1,2,2,3,3]

    print [x for x, y in collections.Counter(a).items() if y > 1]
    [1, 2, 3]

    How to translate into code Golang?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 24, '14 at 9:06a
activeMar 24, '14 at 9:43p
posts19
users11
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase