Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string = "a11aawww1cccertgft1tzzzzzz1ggg111"
- mylist_char = []
- mylist_count = []
- char = None
- count = 0
- for ch in string:
- temp_char = ch
- temp_count = 0
- for ch1 in string:
- if temp_char == ch1:
- temp_count += 1
- if temp_count > count:
- count = temp_count
- char = temp_char
- mylist_char.append(char)
- mylist_count.append(count)
- for x in range(len(string)):
- for ch in string:
- temp_char = ch
- temp_count = 0
- for ch1 in string:
- if temp_char == ch1:
- temp_count += 1
- if temp_count == count and not(temp_char in mylist_char):
- mylist_char.append(temp_char)
- mylist_count.append(temp_count)
- for x in range(len(mylist_char)):
- print("Character", mylist_char[x], "occurred", mylist_count[x], "times")
- string = "a11aawww1cccertgft1tzzzzzz1ggg111"
- dictionary = {}
- for character in list(string):
- if character in dictionary:
- dictionary[character] += 1
- else:
- dictionary[character] = 1
- results = []
- for key, value in dictionary.items():
- results.append((value, key))
- for value, key in reversed(sorted(results)):
- print("Character", key, "occurred", value, "times")
- # Most frequent Character
- def most_frequent(a_string):
- # Create a string of symbols to exclude from counting.
- symbols = ' ,.-/?'
- characters = []
- characters_count = []
- # Check each individual character in the string.
- for ch in a_string:
- # Check that the character is not one of the symbols.
- if ch not in symbols:
- # If its not and we haven't seen it already,
- # append it to the characters list.
- if ch not in characters:
- characters.append(ch)
- # And in the same index in the characters_count list
- characters_count.append(1)
- else:
- # If it is in the characters list, find its index
- # and add 1 to the same index at characters_count
- position = characters.index(ch)
- characters_count[position] = characters_count[position] + 1
- # find the largest value in the character_count list, it's index
- # and show the character at the same index at the characters list.
- print(characters[characters_count.index(max(characters_count))])
- def main():
- # Get a string from the user.
- text = input('Give me some text and I will find you the most frequent character: ')
- most_frequent(text)
- # Call main
- main()
Add Comment
Please, Sign In to add comment