0

I was trying to make a python program that will help me code faster for mysql thru python and I put the shortforms in a dictionary so it can be referenced and replace the values, but I came across a problem that is weird. Isn't the loop supposed to iterate thru the dictionary to find the value but this seems to find the first possible value in the dictionary?

dict1 = {
"selall" : "select * ",
"from" : "from ",
"where" : "where ",
"seldist" : "SELECT DISTINCT ",
"selcount" : "select count ",
"ordby" : "order by ",
"sel" : "select "
}


user = input(">>>> ").split(' ')
command_list = []
a = [i for i in user if i not in dict1]
print(a)

for k,v in dict1.items():
   for i in user:
       if i==k :
        command_list.append(v)
    
command = ''.join(command_list)
print(command)
print(command_list)

# I have to figure out a way to put the values between the commands 

This is the output this the 'sel' key is at the bottom:

>>>> sel this,that from here ordby That
['this,that', 'here', 'That']
from order by select
['from ', 'order by ', 'select ']

But if I put the key above then i.e:- putting the sel key above from

>>>> sel this,that from here ordby That
['this,that', 'here', 'That']
select from order by
['select ', 'from ', 'order by ']

could anyone explain what's happening?

Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
nub
  • 3
  • 2
  • FWIW… is this *really* worth it? `sel from ordby` instead of `SELECT FROM ORDER BY`? That's hardly any less typing… – deceze Jan 20 '21 at 08:22
  • the dictionary in Python is unordered, see: https://stackoverflow.com/questions/20192950/why-items-order-in-a-dictionary-changed-in-python – Tom Chen Jan 20 '21 at 08:23
  • if you need dict1.items() produce ordered tuples, use OrderedDict – Tom Chen Jan 20 '21 at 08:25
  • yea then i can make the shortcuts more smaller but the point being i just wanted to try smth and for my 12th using python mysql together and i can increase my knowledge bro thats all – nub Jan 20 '21 at 08:26

0 Answers0