shorten this: if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":

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

    shorten this: if char in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":

    if char in "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz":

    cant i write something like:
    if char in "[A-Za-z]":

    ?
  • Brian Victor

    #2
    Re: shorten this: if char in

    cirfu wrote:
    if char in "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz":
    >
    cant i write something like:
    if char in "[A-Za-z]":
    Either of the following should do what you want, without resorting to
    regular expressions:

    import string
    if char in string.letters:

    or

    if char.isalpha():

    --
    Brian

    Comment

    • bruno.desthuilliers@gmail.com

      #3
      Re: shorten this: if char in "ABCDEFGHI JKLMNOPQRSTUVWX YZabcdefghijklm nopqrstuvwxyz&q uot;:

      On 24 juin, 20:32, cirfu <circularf...@y ahoo.sewrote:
      if char in "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz":
      >
      cant i write something like:
      if char in "[A-Za-z]":
      >
      Nope. But there are other solutions. Here are two:

      # 1
      import string

      if char in string.letters:
      print "yay"

      # 2
      import re
      exp = re.compile(r'[A-Za-z]')

      if exp.match(char) :
      print "yay"

      Comment

      • Guilherme Polo

        #4
        Re: shorten this: if char in

        On Tue, Jun 24, 2008 at 3:47 PM, bruno.desthuill iers@gmail.com
        <bruno.desthuil liers@gmail.com wrote:
        On 24 juin, 20:32, cirfu <circularf...@y ahoo.sewrote:
        >if char in "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz":
        >>
        >cant i write something like:
        >if char in "[A-Za-z]":
        >>
        >
        Nope. But there are other solutions. Here are two:
        >
        # 1
        import string
        >
        if char in string.letters:
        print "yay"
        >
        # 2
        import re
        exp = re.compile(r'[A-Za-z]')
        >
        if exp.match(char) :
        print "yay"
        >
        Let me post another one, and longer:

        if ord(somechar) in range(ord('A'), ord('Z') + 1) + range(ord('a'),
        ord('z') + 1):
        ...


        --
        -- Guilherme H. Polo Goncalves

        Comment

        • Walter Cruz

          #5
          Re: shorten this: if char in

          another way:

          import string

          if char in string.ascii_le tters:
          print('hello buddy!')

          []'s
          - Walter

          Comment

          • MRAB

            #6
            Re: shorten this: if char in &quot;ABCDEFGHI JKLMNOPQRSTUVWX YZabcdefghijklm nopqrstuvwxyz&q uot;:

            On Jun 24, 7:59 pm, "Guilherme Polo" <ggp...@gmail.c omwrote:
            On Tue, Jun 24, 2008 at 3:47 PM, bruno.desthuill i...@gmail.com
            >
            >
            >
            <bruno.desthuil li...@gmail.com wrote:
            On 24 juin, 20:32, cirfu <circularf...@y ahoo.sewrote:
            if char in "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz":
            >
            cant i write something like:
            if char in "[A-Za-z]":
            >
            Nope. But there are other solutions. Here are two:
            >
            # 1
            import string
            >
            if char in string.letters:
              print "yay"
            >
            # 2
            import re
            exp = re.compile(r'[A-Za-z]')
            >
            if exp.match(char) :
              print "yay"
            >
            Let me post another one, and longer:
            >
            if ord(somechar) in range(ord('A'), ord('Z') + 1) + range(ord('a'),
            ord('z') + 1):
                ...
            >
            And another:

            if 'A' <= somechar <= 'Z' or 'a' <= somechar <= 'z':
            ...

            Comment

            • John Machin

              #7
              Re: shorten this: if char in &quot;ABCDEFGHI JKLMNOPQRSTUVWX YZabcdefghijklm nopqrstuvwxyz&q uot;:

              On Jun 25, 4:32 am, cirfu <circularf...@y ahoo.sewrote:
              if char in "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz":
              >
              cant i write something like:
              if char in "[A-Za-z]":
              You can write that if you want to, but it's equivalent to
              if char in "zaZa]-[":
              i.e. it doesn't do what you want.

              This gives the same reuslt as your original code, unaffected by
              locale:

              if "A" <= char <= "Z" or "a" <= char <= "z":

              Comment

              • s0suk3@gmail.com

                #8
                Re: shorten this: if char in &quot;ABCDEFGHI JKLMNOPQRSTUVWX YZabcdefghijklm nopqrstuvwxyz&q uot;:

                On Jun 24, 5:36 pm, John Machin <sjmac...@lexic on.netwrote:
                On Jun 25, 4:32 am, cirfu <circularf...@y ahoo.sewrote:
                >
                if char in "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz":
                >
                cant i write something like:
                if char in "[A-Za-z]":
                >
                You can write that if you want to, but it's equivalent to
                if char in "zaZa]-[":
                i.e. it doesn't do what you want.
                >
                This gives the same reuslt as your original code, unaffected by
                locale:
                >
                if "A" <= char <= "Z" or "a" <= char <= "z":
                But doesn't that rely on the underlying character set? It's like
                performing math on C char's (maybe that's what the interpreter does
                internally?). If that's the case, using 'char.isalpha() ' or 'char in
                string.letters' or regex's would be better.

                Comment

                • John Machin

                  #9
                  Re: shorten this: if char in &quot;ABCDEFGHI JKLMNOPQRSTUVWX YZabcdefghijklm nopqrstuvwxyz&q uot;:

                  On Jun 25, 9:06 am, s0s...@gmail.co m wrote:
                  On Jun 24, 5:36 pm, John Machin <sjmac...@lexic on.netwrote:
                  >
                  On Jun 25, 4:32 am, cirfu <circularf...@y ahoo.sewrote:
                  >
                  if char in "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz":
                  >
                  cant i write something like:
                  if char in "[A-Za-z]":
                  >
                  You can write that if you want to, but it's equivalent to
                  if char in "zaZa]-[":
                  i.e. it doesn't do what you want.
                  >
                  This gives the same reuslt as your original code, unaffected by
                  locale:
                  >
                  if "A" <= char <= "Z" or "a" <= char <= "z":
                  >
                  But doesn't that rely on the underlying character set?
                  Unless there is a Python implementation using EBCDIC, different
                  underlying character set that differs from ASCII in A..Z and a..z is
                  *not* a practical concern.
                  It's like
                  performing math on C char's (maybe that's what the interpreter does
                  internally?). If that's the case, using 'char.isalpha() ' or 'char in
                  string.letters' or regex's would be better.
                  You should be asking the OP what he really wants ... precisely those
                  letters? alphabetic, in what locale?

                  Comment

                  • Nick Craig-Wood

                    #10
                    Re: shorten this: if char in

                    Guilherme Polo <ggpolo@gmail.c omwrote:
                    Let me post another one, and longer:
                    >
                    if ord(somechar) in range(ord('A'), ord('Z') + 1) + range(ord('a'),
                    ord('z') + 1):
                    ...
                    That is very inefficient! Every time it is run it creates two lists
                    then joins them then throws the whole lot away!

                    --
                    Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

                    Comment

                    Working...