1

I am trying to count each letter up without using count() or dict().

I did write something but I am still having issues with my code.

myString = []
#countList = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
myString ="pynativepynvepynative"
countList = [len(myString)+1]
for i in range(len(myString)):
        #print("Here 0")
        for j in range(len(countList)):
            #print("Here 1")
            if i == countList[j]:
                #print("Here 1.1")
                countList[j+1] = (countList[j+1] + 1)
                break
            else:
                #print("Here 2")
                countList.append(myString[i])
                countList.append(1)
                break
print(countList)

Expected output:

['p', 3, 'y', 3, 'n', 3, 'a', 2, 't', 2, 'i', 2, 'v', 3, 'e', 3]

Actual output:

[22, 'p', 1, 'y', 1, 'n', 1, 'a', 1, 't', 1, 'i', 1, 'v', 1, 'e', 1, 'p', 1, 'y', 1, 'n', 1, 'v', 1, 'e', 1, 'p', 1, 'y', 1, 'n', 1, 'a', 1, 't', 1, 'i', 1, 'v', 1, 'e', 1]
9
  • 2
    Are you trying to count the occurrence of each character? What is your expected output for this example? Commented Jun 29, 2020 at 22:42
  • 2
    So you would like to output a dictionary without using a dictionary? Commented Jun 29, 2020 at 22:46
  • Yes, if that is possible, just through manipulating a list Commented Jun 29, 2020 at 22:47
  • Your output is a dictionary @NoajmIsMyName you might want to change it to a list of tuples or something similar if you absolutely do not want to use a dict. Commented Jun 29, 2020 at 22:49
  • I don't see the point of manipulating a list if you would like to return a dictionary. Why not just use a dictionary from the beginning? Commented Jun 29, 2020 at 22:52

3 Answers 3

2

what can you do is get the unique letters from the string and the for each unique letter loop through the string to count its frequency.

def func_count(string):
    letter = []
    for char in string:
        if char not in letter:
            letter.append(char)
    res = []
    for let in letter:
        count = 0
        for char in string:
            if let == char:
                count+=1
        res.extend([let, count])
    # res = {a:b for a,b in zip(res[::2], res[1::2])} 
    return res

string = "pynativepynvepynative"

solution = func_count(string)
print(solution)

output

['p', 3, 'y', 3, 'n', 3, 'a', 2, 't', 2, 'i', 2, 'v', 3, 'e', 3]  

edit, if you want solution in dict form add res = {a:b for a,b in zip(res[::2], res[1::2])} in function

Sign up to request clarification or add additional context in comments.

Comments

0

Using my question code, I was able to get the right answer modifying the question's code

My problem was that I did not know how to initiate countList properly

countList = []
myString ="pynativepynvepynative"
for i in range(len(myString)):
        #print("Here 0")
        for j in range(len(countList)):
            #print("Here 1")
            #print(myString[i])
            #print(j)
            #print(countList[j])
            if myString[i] == countList[j]:
                #print("Here 1.1")
                #print(myString[i])
                countList[j+1] = (countList[j+1] + 1)
                break
        else :
            #print("Here 2")
            countList.append(myString[i])
            countList.append(1)
print(countList)

Actual output:

['p', 3, 'y', 3, 'n', 3, 'a', 2, 't', 2, 'i', 2, 'v', 3, 'e', 3]

Comments

0

Use collections.Counter, the dict subclass for counting objects, which makes this a one-liner:

from collections import Counter
c = Counter('pynativepynvepynative')

Counter({'p': 3, 'y': 3, 'n': 3, 'v': 3, 'e': 3, 'a': 2, 't': 2, 'i': 2})

(Technically this isn't a dict, it's a subclass of dict.)

You can get a list-of-tuple from it:

>>> c.most_common()
[('p', 3), ('y', 3), ('n', 3), ('v', 3), ('e', 3), ('a', 2), ('t', 2), ('i', 2)]

Lists or tuples are undesirable for counting things, because you want to be able to separately access/sort by the keys (objects that you're counting) and the values (counts). In theory you can do that on list-of-list/tuple, but it's a pain, and Counter alrady defines several of the methods you'll need.

1 Comment

I truly do understand your point. I am only doing this example just to be better at looping and alternating elements inside a list... I am still learning!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.