Combining digit in a list to make an integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harlin Seritt

    Combining digit in a list to make an integer

    I have the following:

    num1 = ['1', '4', '5']

    How can I combine the elements in num1 to produce an integer 145?

    thanks,

    Harlin Seritt

  • Dan Bishop

    #2
    Re: Combining digit in a list to make an integer

    Harlin Seritt wrote:[color=blue]
    > I have the following:
    >
    > num1 = ['1', '4', '5']
    >
    > How can I combine the elements in num1 to produce an integer 145?[/color]


    int(''.join(num 1))

    Comment

    • Harlin Seritt

      #3
      Re: Combining digit in a list to make an integer

      If anyone has time, would you mind explaining the code that Dan Bishop
      was so kind as to point out to me:

      int(''.join(num 1))

      This worked perfectly for me, however, I'm not sure that I understand
      it very well.

      Thanks,

      Harlin Seritt

      Comment

      • Joel

        #4
        Re: Combining digit in a list to make an integer


        Harlin Seritt wrote:[color=blue]
        > If anyone has time, would you mind explaining the code that Dan[/color]
        Bishop[color=blue]
        > was so kind as to point out to me:
        >
        > int(''.join(num 1))
        >
        > This worked perfectly for me, however, I'm not sure that I understand
        > it very well.
        >
        > Thanks,
        >
        > Harlin Seritt[/color]

        ''.join(list of strings) is a python idiom for fast string
        concatenation. ''.join(num1) would give "145". The function int() is
        then used to convert the resulting string into an integer.

        Comment

        • Dan Bishop

          #5
          Re: Combining digit in a list to make an integer

          Harlin Seritt wrote:[color=blue]
          > If anyone has time, would you mind explaining the code that Dan[/color]
          Bishop[color=blue]
          > was so kind as to point out to me:
          >
          > int(''.join(num 1))
          >
          > This worked perfectly for me, however, I'm not sure that I understand
          > it very well.[/color]

          join(...)
          S.join(sequence ) -> string

          Return a string which is the concatenation of the strings in the
          sequence. The separator between elements is S.

          For example:
          [color=blue][color=green][color=darkred]
          >>> '<'.join(['1', '2', '3'])[/color][/color][/color]
          '1<2<3'

          If you don't want a separator, simply let S be the empty string ('').

          Comment

          • Facundo Batista

            #6
            Re: Combining digit in a list to make an integer

            On 1 Apr 2005 03:21:12 -0800, Harlin Seritt <harlinseritt@y ahoo.com> wrote:
            [color=blue]
            > num1 = ['1', '4', '5']
            >
            > How can I combine the elements in num1 to produce an integer 145?[/color]
            [color=blue][color=green][color=darkred]
            >>> num1 = ['1', '4', '5']
            >>> int(''.join(num 1))[/color][/color][/color]
            145

            .. Facundo

            Blog: http://www.taniquetil.com.ar/plog/
            PyAr: http://www.python.org/ar/

            Comment

            Working...