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...![]()
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"]
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.
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):
Here it's illustrated with a list: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'>
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'>
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks