Turning a string into a dictionary - python

Having had a python class behind me, I was presented with this question in the next class and I seem to be having a mental block on how to even get started.
"Write a python program that asks the user for a string, then create the following dictionary: The values are the letters in the string, with the corresponding key being the place in the string. For example if the user entered the string “ABC123” then the dictionary would be: D = {‘A’:0, ‘B’:1, ‘C’:2, ‘1’:3, ‘2’:4, ‘3’:5}
I started with asking for the user input with something simple like
s = input('Enter string: ')
however, I do not know how to proceed to the next step. Any help would be much appreciated.

In [55]: s = input("Enter a string: ")
Enter a string: ABC123
In [56]: d = {char:i for i,char in enumerate(s)}
In [57]: d
Out[57]: {'C': 2, '1': 3, '2': 4, '3': 5, 'B': 1, 'A': 0}
Note however, that if there are repeated characters in the user's input, d will have the index of the last occurrence of each character:
In [62]: s = input("Enter a string: ")
Enter a string: ABC123A
In [63]: d = {char:i for i,char in enumerate(s)}
In [64]: d
Out[64]: {'C': 2, '1': 3, '2': 4, '3': 5, 'B': 1, 'A': 6}

this?
def dict():
user_input = input("Please enter a string")
dictionary = {}
for i, j in enumerate(user_input):
dictionary[j] = i
print(dictionary)
dict("ABC123")

Are you sure you need this output: D = {‘A’:0, ‘B’:1, ‘C’:2, ‘1’:3, ‘2’:4, ‘3’:5} and not D = {0:'A', 1:'B', 2:'C'...}? You can flip the key:values, but they will be unordered then (e.g. you would get something like: D = {‘B’:1, ‘3’:5, ‘A’:0, ‘C’:2, ‘1’:3, ‘2’:4 } or any other random combination).
It sounds like you are getting started with learning python. Welcome to a beautiful programming language. People are very helpful here, but you need to show some effort and initiative. This is not a place to get quick fix solutions. People may provide them to you, but you'll never learn.
I assume this was a HW related question? Unless I'm mistaken (someone please feel free to correct me), the output you are seeking is difficult, if not impossible to create (e.g in that specific order you want). I encourage you to read about python dictionaries.
Try and run this:
#user = input("Give me a string:")
#Just for demo purpose, lets
#stick with your orig example
user = "ABC123"
ls =[] #create empty list
d = {} #create empty dictionary
#Try to figure out and understand
#what the below code does
#what is a list, tuple, dict?
#what are key:values?
for l in enumerate(user):
ls.append(l)
for k,v in ls:
d[k] = v
print('first code output')
print(ls)
print(d)
#What/how does enumerate work?
#the below will generate a
#ordered dict by key
for k,v in enumerate(user):
d[k] = v
print('2nd code output')
print(d)
#you want to flip your key:value
#output, bases on origibal question
#notice what happens
#run this a few times
print('3rd code output')
print(dict(zip(d.values(), d.keys())))
#You can get the order you want,
#but as a list[(tuple)], not as
#a dict at least in the output
#from your orig question
print(list(zip(d.values(), d.keys())))
Unless Im wrong, and someone more experienced can chime in, you cant get the "ordered" output in the format you want for your dictionary.
I'm on a mobile device, so anyone please feel free to correct things.

Related

How can I make a conditional expression?

I want to see the modeling output with two data frames.
One data frame has a target value of 1 to 8 and another has only 1,2,3,5,6,7
I made a dictionary to map the values, and I made a code as below to make the probability.
my_dict ={1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g', 8:'f'}
def func(val):
for key, value in my_dict.items():
if val == key:
return value
return "There is no such Key"
inputData = [1, 2, 3, 4, 5]
inputData2 = np.array([inputData])
index = 1;
result_data = OrderedDict()
for x in xgb_model.predict_proba(inputData2,ntree_limit=None, validate_features=False,base_margin=None)[0]:
result_data[func(index)] = round(x,2)
index += 1
print("result_name : ", max(result_data.items(), key=operator.itemgetter(1))[0])
print("result_value : ", max(xgb_model.predict_proba(inputData2, ntree_limit=None, validate_features=False, base_margin=None)[0]))
print(result_data)
But in the second data frame, the key value is pushed back.
For example, a: 0.2, b:0.2, c:0.1, e:0.1, f:0.1 g:0.3 should appear, but in real data, the data should be:
a:0.2, b:0.2, c:0.1, d:0.1, e:0.1, f:0.3
I don’t know what I should do.
So I've been working on the code below.
Only a:0.2, b:0.2, c:0.1 comes out and ends.
for x in xgb_model.predict_proba(inputData2,ntree_limit=None, validate_features=False,base_margin=None)[0]:
if index not in y.target.unique().tolist():
continue
result_data[func(index)] = round(x,2)
index += 1
please let me know if you can't understand the code.
hope for help. Thank you.
In the second model that has 8 coefficients, you overwrite the value for f since it is defined both for the 6th as well as for the 8th element. Your dict should be defined as:
my_dict ={1:'a', 2:'b', 3:'c', 4:'d', 5:'e', 6:'f', 7:'g', 8:'h'}
But you could make the code much simpler by just using a string ("_abcdefgh") to get the correct letter for each index. You could, then, just use result_data[mystring[i]]= and drop the function.

Print meaning of first letter

If I have a list like:
Q = Quiet
J = John
I want to print the meaning of the first letter in:
Queer
Johnson
How do I do that? I want to take the meaning of Q from Queer and J from Johnson and print it in list form.
Output:
Quiet
John
handleNameChange(name):
if(name[0] == 'Q'):
print("Quiet");
else:
print("John");
a = input()
b = input()
handleNameChange(a);
handleNameChange(b)
Input:
Queer
Johnson
Output:
Quiet
John
meaning = {'Q': 'Quiet', 'J': 'John'}
user_input = 'Queer Johnson'
result_list = list(map(lambda x: meaning[x[0]], user_input.split()))
print(result_list)
Output
['Quiet', 'John']
Your question is very unclear, but I'll make an assumption that you actually need a dictionary, rather than a list, in order to create a "pseudo" translator of sorts
dictionary = {'Q': 'Quiet', 'J':'John'}
Subsequently, I assume you'd like to extract capital letters from your string, which can be done by using regex (one of many solutions)
import re
string = "Queer Johnson"
capital_letters = re.findall(r'[A-Z]', string)
Output:
['Q','J']
You can then use the dictionary to "translate" extracted letters
results = [v for i in capital_letters for k,v in dictionary.items() if i == k]
Output:
['Quiet', 'John']
P.S. Make sure your questions are properly constructed. It will help both you and the community.
Cheers!

Program is repeating itself -can't figure out why

This is my program so far...it takes a message (input from user) and tells the user how many A's are in the program, how many B's, etc. Except when I input a message such as "Dad", it'll tell me how many D's there are twice instead of just saying everything once.
It says:
D ... 2
A ... 1
D ... 2
I want it to say:
A ... 1
D ... 2
How do I fix this without using zip, and without importing anything?
message=input("what is your message?").upper()
alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
count=[0]*len(alphabet)
for i in message:
if i in alphabet:
count[alphabet.index(i)]+=1
for i in message:
print (i,"...",count[alphabet.index(i)])
(Thanks to Uriel Eli for helping me get the program this far btw).
I don't agree with your approach here. You have actually over complicated this. The proper way to solve this is to actually use a dictionary to keep track of all the letters in the string and keep a count every time the same character comes up. Note, this also sticks to the rule of not importing anything.
Furthermore, this removes the necessity to have a list of letters to check against as well.
Also, if you need to count upper and lower case characters separtely, do not call upper at the end of your input. Just remove it. If you have to count upper and lower case as the same character, then you can leave it.
message=input("what is your message?").upper()
d = {}
for c in message:
if c in d:
d[c] += 1
else:
d[c] = 1
Demo
what is your message?thisisastringofthings
{'H': 1, 'F': 0, 'O': 0, 'R': 0, 'G': 1, 'S': 3, 'T': 2, 'A': 0, 'I': 3, 'N': 1}
To provide an output similar to what you are expecting, you just need to iterate through your final result and print:
for character, count in d.items():
print("{} ... {}".format(character, count))
Finally, just for the sake of showing the best way to do this, is to actually use Counter from collections:
>>> from collections import Counter
>>> Counter("thisisastring")
Counter({'s': 3, 'i': 3, 't': 2, 'h': 1, 'n': 1, 'a': 1, 'r': 1, 'g': 1})
Just for future reference, and I know that you CANT import anything now. The best way probably would be:
from collections import Counter
message=input("what is your message?").upper()
print(Counter(message))
# Counter({'D': 2, 'A': 1})
Your second for loop is iterating through message, so if the user input DAD (well... after upper casing it), you're gonna get:
message == DAD
i = D --> Shows 2
i = A --> Shows 1
i = D --> Shows 2 (again)
Maybe you'd want to iterate through count, keeping the index that you are iterating (to use it latter to match it with the alphabet list). Something like that:
for index, num_occurences in enumerate(count):
if num_occurences > 0:
print("Match found at i=%s which corresponds with alphabet[%s]=%s" %
(index, index, alphabet[index]))
print(alphabet[index], "...", num_occurences)
You should check what enumerate does.
If you still want to iterate through message, you can do it, keeping track of what letter did you already display using an auxiliary set (so you don't show the same letter again)
already_shown_letters = set()
for i in message:
if i not in already_shown_letters:
print (i,"...",count[alphabet.index(i)])
already_shown_letters.add(i)

Choose Variable With Highest Value (Python)

How do I make Python choose the variable with the hightest integer value?
E.g
a = 1
b = 2
c = 3
d = 0
I want the output to be "
c". I know the max() command does something similar, but instead of choosing the variable itself, it will choose the hightest number.
If I wasn't clear enough about this please tell me and I'll try and reexplain, but it seems to be a simple enough matter of not knowing the command. Thanks in advance!
This is a job for a dictionary:
data = {
'a': 1,
'b': 2,
'c': 3,
'd': 4
}
print max(data, key=data.get)
d
Python does not make a point of tracking variable names.

my str(float) gets broken on histogram dictionary conversion, how do I stop this?

When attempting to histogram a list of numbers(in str formats) all of my numbers get broken up
for instance
a = ['1','1.5','2.5']
after running my histogram function
my dictionary looks like
{'1': 2, '2': 1, '5': 2, '.': 2}
my histogram function is
def histogram(a):
d = dict()
for c in a:
d[c] = d.get(c,0)+1
return d
I'm doing a project for school and have everything coded in, but when I get to doing the mode portion and I use numbers that aren't specifically int I get the above returns
How can I adjust/change this so it accepts the strings exactly as typed
Python 2.7 on Windows 7x64
You can convert each string element to a float before passing it your histogram function.
a = ['1','1.5','2.5']
a = [float(i) for i in a]
def histogram(a):
d = dict()
for c in a:
d[c] = d.get(c,0)+1
return d
print histogram(a)
There might be an error in your list definition. Running your code I get
{'1': 1, '1.5': 1, '2.5': 1}
If I change the definition of a from
a = ['1','1.5','2.5']
to
a = '1' '1.5' '2.5'
I get the output you showed us.
So please double check how your list is defined.
You can use something like this:
>>> a = ['1','1.5','2.5']
>>> dict.fromkeys(a, 0)
{'1': 0, '1.5': 0, '2.5': 0}
Now you can iterate over keys to set the corresponding value.
I have used the following dict comprehension to reduce my work.
>>> {key: float(key)+1 for key in a}
{'1': 2.0, '1.5': 2.5, '2.5': 3.5}
enjoy :)
The histgram function does work as it's written. If however you you inadvertently .join() your list your histogram with then histogram the resulting object. For instance... t = ['1.0','2.0','2.5'] and
s = s.join(t) s will then be == '1.02.02.5' and histogram(s) will count the decimals as values in the slice. My problem was that I had placed a .join() prior to calling histogram.
My appologies to anyone that wasted any real time on this.

Categories