Checking homogeneity of Array using List in Python

Dave Angel davea at davea.name
Sun Aug 25 16:49:57 EDT 2013


sahil301290 at gmail.com wrote:

> I am unable to check homogeneity of Array.
> I have take Array type Int to be default for my code.
>
> Instead of getting Error on NON-INT Values.

But none of them below are int values.

> I want to take input as string.
> Then check if all input is in (0-9) form, I typecast it into int and Accept.
> Else, I would like to skip that input.
>
> eg. my input is ['1', ' ', 'asdasd231231', '1213asasd', '43242']
> I want it to be interpreted as:
> [1, [None], [None], [None], 43242]
>
> NOTE: NO INBUILT FUNCTION BE USED. Thank you in advance.

I don't see any Arrays.  You show a list, however.  Lists don't have any
type for the whole list, only for each element.

Seems to me like the minimum functions you'll need are int(), type(),
isinstance().  They're built-in, not inbuilt, so perhaps that's okay. 
Or perhaps you're going to write your own equivalents.  But your example
contradicts your description enough that I could be wrong about any of
the three.

> > I want to take input as string.

So why do you show it as a list?

Do you know how to write a function?  Try writing one that takes  a
string and returns True if all the characters are digits, and False if
either:
   1) there are no characters
   2) there are some non-digit characters

Try writing another one that takes a string made up entirely of digit
characters, and produces an int from them.  Note you'll be doing some
multiplies by 10, since presumably you're working in decimal.  You could
call this function my_int()

Write some code that actually illustrates what you're trying to do, show
some data being fed into it, and indicate where you ran out of ideas.
Then somebody could perhaps help.  As it stands, you're referring to
types that don't fit, and making assumptions that make no sense.



-- 
DaveA





More information about the Python-list mailing list