#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12

    An array of problem!!!


    Hi,
    Wanted to know is there any way to generate an array of alphabets ...ummm just like eg.

    digit=range(0,10)
    wld generate a sequence of all the digits

    I wanted a similar way for lowercase and uppercase letters...
    Can anyone help me out??

    Thanks & Rgds,
    Subha
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You are looking for the built-in ord() and chr() functions, before i go and mess around with this, you want a range() function that can handle iteration over a range or characters... say 'a' to 'z'.

    Code:
    >>> for each in range(33, 256):
    	print chr(each),
    
    	
    ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~   チ            ヘ  マ ミ             ン   *            *                                                                                  
    >>>
    Have fun,

    Mark.
    Last edited by netytan; October 28th, 2004 at 07:42 AM. Reason: Quote instead of Code
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Thanks Mark!
    But I don't want it that way...
    as I said a list [a..z] and [A..Z]
    something of that sort

    thnaks & rgds,
    Subha
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Heres the basic function, you might want to add to it i.e. auto convert int charaters etc.

    Code:
    >>> def stringRange(startChar, stopChar):
    	startChar = ord(startChar)
    	stopChar = ord(stopChar) + 1
    	for symbol in xrange(startChar, stopChar):
    		yield chr(symbol)
    
    		
    >>> for letter in stringRange('a', 'z'):
    	print letter,
    
    	
    a b c d e f g h i j k l m n o p q r s t u v w x y z
    >>> for letter in stringRange('0', '9'):
    	print letter,
    
    	
    0 1 2 3 4 5 6 7 8 9
    >>> atoz = []
    >>> for letter in stringRange('a', 'z'):
    	atoz.append(letter)
    
    	
    >>> atoz
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    >>>
    I will also do a list comprehension for this in a minute . Just give me a few, this what you wanted anyway?

    Mark.
    Last edited by netytan; October 28th, 2004 at 07:41 AM. Reason: Quote instead of Code
    programming language development: www.netytan.com Hula

  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    As promised, the function converted to a list comprehension:

    Code:
    >>> [chr(s) for s in xrange(ord('a'), ord('z') + 1)]
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    >>> [chr(s) for s in xrange(ord('0'), ord('9') + 1)]
    ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    >>> atoz = []
    >>> atoz = [chr(s) for s in xrange(ord('a'), ord('z') + 1)]
    >>> atoz
    ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    >>>
    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    There is also this option:

    Code:
    >>> import string
    >>> string.ascii_lowercase
    'abcdefghijklmnopqrstuvwxyz'
    >>> string.ascii_uppercase
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    >>> string.ascii_letters
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    >>> string.ascii_lowercase[:13]
    'abcdefghijklm'
    >>> string.ascii_lowercase[13:]
    'nopqrstuvwxyz'
    >>>
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Thanks sfb and Mark!!
    Mark that was a superb list comprehension u came up with. Simple and perfect!!!!

    Got to learn abt how to use the string module. I was fiddling with that too but cldn't come up with a good answer to my query.

    Now I've learnt 2 new things...

    xrange --->
    whats the diff between xrange and range...I cldn't decipher the explanation given in the documentation

    Code:
    >>> range(0,10)
    
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    >>> xrange(0,10)
    xrange(10)
    Next is the 'yield' ----->

    cldn't get the yield too...why is it necessary n why only generator functions...cld anyone pick up these topics to explain!!!

    Thanks & Rgds,
    Subha
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Iterators and Generators are generally used because they are more memory efficient, so the difference between xrange() and range() is that the range() function creates the entire list in memory and xrange() does not; it creates and yields the values as needed .

    This should help you understand:

    Code:
    >>> range(1000000000L)
    
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in -toplevel-
        range(1000000000L)
    MemoryError
    >>> xrange(1000000000L)
    xrange(1000000000)
    >>>
    Next: yield is a bit like return except that it doesn't cause the function to exit. This means that you can return/yield multiple values from the function . Which can be very useful! This is the main difference between a normal function and a generator function.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Gee thanks Mark...that was very clear....thanks a million!!!

    Subha

IMN logo majestic logo threadwatch logo seochat tools logo