:sad: Please could some please help me out here . I have an array. I want to count how my 'a' appear and the a must have its quotes 'a'

[(' ', ' ', [('a', 'Scott'), ('9', 'vth')]), (' ', ' ', [('a', 'Jenny'), ('9', 'vth')])]

Like this one has 'a' = 2

Is there any reason why your array is so complex? You have a list containing a tuple containing a list containing several tuples containing lists.....

Anyway, try this (no guarantees, don't have an intepreter atm...you may also need to fix the indents):

def count_char(array, char="a", total=0):
for element in array: #indent-1
array_=False #indent-2. different from "array" above.
try:#indent-2
temp=element[0]#indent-3
array_=True#indent-3
if array_:#indent-2
for element_ in element:#indent-3
return total+count_chars(element_, char=char, total=total)#indent-4
else:#indent-2
for element in array:#indent-3
if element==char:#indent-4
total+=1#indent-5
return total#indent-3

You need to realise that i don't have access to an IDLE now to test this, so you're going to have to figure out the indentation on your own (use your discretion). Another thing, if the code crashes or something like that, you should use your discretion fix it if its a minor bug.

NOTES:
Logically, all it does is check if the array conatins another array. If it does, it checks if that array contains another array...and s on. Finally, when it reaches the last array, it checks if each element in that array is an "a", or whichever other character you may want to search for. This uses recursion...it may be frowned upon if you hand it in for homework, or use it to check large arrays.

Happy coding...

As scru says, this is not an easy task if you have such a nested structure ;)

I once wrote a function which "flattens" these monsters :)

def flatten(lst):
    for elem in lst:
        if type(elem) in (tuple, list):
            for i in flatten(elem):
                yield i
        else:
            yield elem

With this, the rest is easy:

nested = [(' ', ' ', [('a', 'Scott'), ('9', 'vth')]), (' ', ' ', [('a', 'Jenny'), ('9', 'vth')])]
flattened = list( flatten(nested) )
print flattened.count("a")

Regards, mawe

:sad: Please could some please help me out here . I have an array. I want to count how my 'a' appear and the a must have its quotes 'a'

[(' ', ' ', [('a', 'Scott'), ('9', 'vth')]), (' ', ' ', [('a', 'Jenny'), ('9', 'vth')])]

Like this one has 'a' = 2

For your particular example, you can convert your list to a string, then find 'a'

>>> a = [(' ', ' ', [('a', 'Scott'), ('9', 'vth')]), (' ', ' ', [('a', 'Jenny'), ('9', 'vth')])]
>>> b = str(a)
>>> print b.count("'a'")
2

Thank you so much it works :)

b=str(a)
print b.count("'a'")
This question has already been answered. Start a new discussion instead.