Count number of characters for each line pyspark
<p>I am able to count total number of each character in the entire document.</p>

<p>My document:</p>

<pre><code>ATATCCCCGGGAT
ATCGATCGATAT
</code></pre>

<p>Calculating total number of each characters in the document:</p>

<pre><code>data=sc.textFile("data.txt")
counts=data.flatMap(lambda x:[(c,1) for c in x]).reduceByKey(add)
</code></pre>

<p>Result:</p>

<pre><code>[(u'A', 7), (u'C', 6), (u'T', 7), (u'G', 5)]
</code></pre>

<p>My Implementation<br></p>

<pre><code>counts=data.map(lambda x:[(c,1)for c in x])
for row in counts.collect():
    print sc.parallelize(row).reduceByKey(lambda x,y:x+y).collect()
</code></pre>

<p>Is there a better way to do it?</p>