Closed Thread
Results 1 to 4 of 4

Thread: Python arrays...

  1. #1
    Sir_Rimo's Avatar
    Sir_Rimo is offline Newbie
    Join Date
    Jan 2007
    Posts
    18
    Rep Power
    0

    Arrow Python arrays...

    Hi EveryBody.
    I have a problem with arrays in Python. Can you please help me?
    the fact is that there is a program here (I don't know who has written it) that returns an array to me and I must show the results in a list box (QT).
    the problem is that I don't know anything about how to handle the array result. it may be [], [(0),] or [3].
    I really don't have an idea about the program, but would you please tell me how to work with arrays and convert their results into strings?
    I'd be thankful so much.
    thanks everybody...

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    In Python it's called "lists".
    You can access the lists using the index-operators, [].

    Lists starts at zero (0) like arrays, and they work almost in the same way. So if you want to get the first element in the list do this [0], the next [1] and so on. You can also use more advanced operators and do like this [0:2] which will return the two first elements in the list like a new list - and if you don't want the first element as a f.ex. string you can get it as a list like this [0:1]. You can also access it both ways using [-x] or [-x:-x], etc.

    Now you're probably confused if you don't know to all this, let's see an example:
    Code:
    l = ["apple", "banana", "orange"]
    
    l[0] # Returns "apple"
    l[1] # Returns "banana"
    
    l[0:1] # Returns ["apple"]
    l[0:2] # Returns ["apple", "banana"]
    l[1:3] # Returns ["banana", "orange"]

  4. #3
    Sir_Rimo's Avatar
    Sir_Rimo is offline Newbie
    Join Date
    Jan 2007
    Posts
    18
    Rep Power
    0
    Thank you dear vOid,
    I must say that sometimes when I print the return value of the program, it shows " zeros((0,),'i') "
    I know that it is a part of numarray module, but I don't find a way to recieve the result as a string. the tostring() method of array does not work here.
    Can you please help me?
    Thank you very much.

  5. #4
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    You don't need any functions to convert the numeric value to a string, you can use simple type-casting. It's possible in Python because it's a very type-weak language, so everything is almost possible with types.

    Here is an example to illustrate it (copy+pastet from the idle):
    Code:
    >>> my_num = 100
    >>> my_str = my_num # will be 'int'
    >>> type(my_num)
    <type 'int'>
    >>> type(my_str)
    <type 'int'>
    >>> my_str = str(my_num) # now it'll be 'str'
    >>> type(my_str)
    <type 'str'>
    Here it's illustrated with a list:
    Code:
    >>> my_numbers = [1, 2, 3, 1000, 20000, 30000]
    >>> type(my_numbers)
    <type 'list'>
    >>> type(my_numbers[0])
    <type 'int'>
    >>> type(str(my_numbers[0]))
    <type 'str'>
    >>> type(my_numbers[0:2])
    <type 'list'>
    >>> type(my_numbers[0:2][0])
    <type 'int'>
    >>> type(str(my_numbers[0:2][0]))
    <type 'str'>

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C Defining Data Arrays And Not BSS Arrays?
    By RhetoricalRuvim in forum C and C++
    Replies: 1
    Last Post: 09-02-2011, 07:59 AM
  2. Advanced Python, part 1: Extending Python with C
    By spyder in forum Python Tutorials
    Replies: 0
    Last Post: 07-31-2010, 09:36 AM
  3. Replies: 11
    Last Post: 07-31-2010, 12:52 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts