1) First I am aware that sets and dictionaries don't have an order, the question confuses me and I am trying to figure this out, you can tell from below my efforts to figure out how to solve the problem. 2) I know that the look of the question is poor so sorry in advance, not sure how to make it more tidy here, have spent over half an hour trying to make it in the proper format etc :-) and it is still not as I would like it to be.
Summary: My difficulty is that the question is requiring me to produce a dictionary with values in a certain order so while I can get the answer of specific values. I can do it without the order it asks unless I have misunderstood.
This is the course question I am referring to, I have tried some of the stuff which is not successful according to the required answer:
(if you don't want to check the link the answer I get from the code is here:
{' ': 8, 'e': 8, 'i': 5, 'a': 4, 't': 4, 'l': 3, 'u': 3, 'h': 2, 'n': 2, 's': 2, 'r': 2, 'J': 1, 'c': 1, 'b': 1, 'd': 1, 'g': 1, 'f': 1, 'k': 1, 'm': 1, 'o': 1, 'q': 1, 'p': 1, 'w': 1, 'v': 1, 'y': 1, 'x': 1, 'z': 1})
The lower and upper case letters of the English alphabet should stored as the string variable alphabet.
Consider the sentence 'Jim quickly realized that the beautiful gowns are expensive'. Create a dictionary count_letters with keys consisting of each unique letter in the sentence and values consisting of the number of times each letter is used in this sentence. Count upper case and lower case letters separately in the dictionary.
What is the 3rd value of the dictionary? Hint: remeber that Python is zero indexed!
my answer 1, correct (YAY really dopamine hit)
What is the 8th value of the dictionary? Hint: remeber that Python is zero indexed!
3 correct (YAY I think... why this was after a lot of attempts looking at my answer which then did not make sense)
Note that I accidentally got the 2nd answer correct, and do not know why.
Apparently the order of the Keys in the dictionary need to be in {J :value, i : value} I have tried counter if you see the link which you can see from my initial attempts which fails to provide the required answer, I have tried set(sentence)&set(alphabet) which provides the following 'order' : {'J', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
I have tried for several days on and off for hours trying things out to no avail, I got hopeful when I saw a few pages ago that from a stack overflow answer see code below gave me hope.... It really did work ONCE in the command line (J,i,m...) but after for some reason it went back to Jabcde....)
I have also found this useful and very short but still I have the problem of the 'order' of the set being as before:
a = sentence
b = dict.fromkeys(a[i], 0)
for i in a[i]:
b[i] += 1
Counting the occurrence of unique letters in a string in Python 3
Here is more code too.
as you can see at some point I am trying to force an order of some sort which I don't think is possible for the key and value for the question and answer to be happy .
here is more code: in my jungle of confusion.
import string
alphabet = string.ascii_letters
alphabet
sentence = 'Jim quickly realized that the beautiful gowns are expensive'
Python maintain order in intersection of lists
This allows the list to be in the same order as the sentence. I use A as Alphabet and B as the list Hooray!!! Thanks Kasramvd but alas it only worked in the command line when I readjusted the order (ONCE). But gave me hope to keep pushing. As you can see I have commented out code in case I want to use it. I am using python3 canopy interface.
a = sentence
b = dict.fromkeys(a[i], 0)
for i in a[i]:
b[i] += 1
#A = set(alphabet)
#A = list(A)
#B = set(sentence)
#B = list(B)
#maybe = set(B)&set(A)
#letter_set = set(A) & set(B)
letter_list = list(letter_set)
common = sorted(set(A).intersection(set(B)) ,key=lambda x:B.index(x))
letter_list = common #it is already a list
count_letters = {}
cnt_lowercase = 0
cnt_uppercase = 0
count = 0
#iterate over the letter_list
for i in range (len(letter_list)-1):
#count matches for each letter of the sentence
for j in range(len(sentence)-1):
gr = letter_list[i] in sentence[j]
print i,j
print("sentence letter : " , letter_list[i], "sentence letter: " + sentence[j])
print gr
if gr == True:
count +=1;
if j == len(sentence)-1:
print("count= " , count)
count_letters[i] = count
print(count_letters)
if j > len(sentence):
print("reached the last letter of the sentence: " , sentence[j])
count = 0
print j
print ("length of the sentence is : " , len(sentence))
print count_letters
print letter_list
common = sorted(set(A).intersection(set(B)) ,key=lambda x:B.index(x))
so this basically still is set(A) & set(B)
Any ideas??? I am too stubborn to let this go, after this I simply will turn it into a function so I can answer the next question as that is required for a much larger string of over 5 lines. I probably will be back if I get stuck again.
hope there was enough information.