Pulling any information from a dictionary with a user input - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Pulling any information from a dictionary with a user input (/thread-6418.html) Pages:
1
2
|
Pulling any information from a dictionary with a user input - Darmanus - Nov-21-2017 Hi all! I have recently started using Python 2 with no previous experience in the language. I have created a dictionary (or a dictionary of dictionaries) which is below of the entire periodic table, and I would like to be able to pull any data from this that I want from a user input. My issue is that I can only get it to print a single element and all of the attached data (and only using the interpreter on this site but not the actual python interpreter), whereas I'd like to be able to print everything (in a readable way) or be able to print certain parts, such as only radioactive elements or certain types of element. If anyone can provide assistance, that would be amazing! elements = {} elements['H'] = {'name': 'Hydrogen', 'number': 1, 'Mass': 1.008, 'Type': 'Nonmetal', 'Radioactive': False} elements['He'] = {'name': 'Helium', 'number': 2, 'Mass': 4.003, 'Type': 'Noble Gas', 'Radioactive': False} elements['Li'] = {'name': 'Lithium', 'number': 3, 'Mass': 6.941, 'Type': 'Alkali Metal', 'Radioactive': False} elements['Be'] = {'name': 'Beryllium', 'number': 4, 'Mass': 9.012, 'Type': 'Alkaline Earth', 'Radioactive': False} elements['B'] = {'name': 'Boron', 'number': 5, 'Mass': 10.811, 'Type': 'Semimetal', 'Radioactive': False} elements['C'] = {'name': 'Carbon', 'number': 6, 'Mass': 12.011, 'Type': 'Nonmetal', 'Radioactive': False} elements['N'] = {'name': 'Nitrogen', 'number': 7, 'Mass': 14.007, 'Type': 'Nonmetal', 'Radioactive': False} elements['O'] = {'name': 'Oxygen', 'number': 8, 'Mass': 15.999, 'Type': 'Nonmetal', 'Radioactive': False} elements['F'] = {'name': 'Flourine', 'number': 9, 'Mass': 18.998, 'Type': 'Halogen', 'Radioactive': False} elements['Ne'] = {'name': 'Neon', 'number': 10, 'Mass': 20.180, 'Type': 'Noble Gas', 'Radioactive': False} elements['Na'] = {'name': 'Sodium', 'number': 11, 'Mass': 22.990, 'Type': 'Alkali Metal', 'Radioactive': False} elements['Mg'] = {'name': 'Magnesium', 'number': 12, 'Mass': 24.305, 'Type': 'Alkaline Earth', 'Radioactive': False} elements['Al'] = {'name': 'Aluminium', 'number': 13, 'Mass': 26.982, 'Type': 'Basic Metal', 'Radioactive': False} elements['Si'] = {'name': 'Silicon', 'number': 14, 'Mass': 28.086, 'Type': 'Semimetal', 'Radioactive': False} elements['P'] = {'name': 'Phosphorus', 'number': 15, 'Mass': 30.974, 'Type': 'Nonmetal', 'Radioactive': False} elements['S'] = {'name': 'Sulphur', 'number': 16, 'Mass': 32.066, 'Type': 'Nonmetal', 'Radioactive': False} elements['Cl'] = {'name': 'Chlorine', 'number': 17, 'Mass': 35.453, 'Type': 'Halogen', 'Radioactive': False} elements['Ne'] = {'name': 'Argon', 'number': 18, 'Mass': 39.948, 'Type': 'Noble Gas', 'Radioactive': False} elements['K'] = {'name': 'Potassium', 'number': 19, 'Mass': 39.098, 'Type': 'Alkali Metal', 'Radioactive': False} elements['Ca'] = {'name': 'Calcium', 'number': 20, 'Mass': 40.078, 'Type': 'Alkaline Earth', 'Radioactive': False} elements['Sc'] = {'name': 'Scandium', 'number': 21, 'Mass': 44.956, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Ti'] = {'name': 'Titanium', 'number': 22, 'Mass': 47.867, 'Type': 'Transistion Metal', 'Radioactive': False} elements['V'] = {'name': 'Vanadium', 'number': 23, 'Mass': 50.942, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Cr'] = {'name': 'Chromium', 'number': 24, 'Mass': 51.996, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Mn'] = {'name': 'Manganese', 'number': 25, 'Mass': 54.938, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Fe'] = {'name': 'Iron', 'number': 26, 'Mass': 55.845, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Co'] = {'name': 'Cobalt', 'number': 27, 'Mass': 58.693, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Ni'] = {'name': 'Nickel', 'number': 28, 'Mass': 58.693, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Cu'] = {'name': 'Copper', 'number': 29, 'Mass': 63.546, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Zn'] = {'name': 'Zinc', 'number': 30, 'Mass': 65.38, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Ga'] = {'name': 'Gallium', 'number': 31, 'Mass': 69.723, 'Type': 'Basic Metal', 'Radioactive': False} elements['Ge'] = {'name': 'Germanium', 'number': 32, 'Mass': 72.631, 'Type': 'Semimetal', 'Radioactive': False} elements['As'] = {'name': 'Arsenic', 'number': 33, 'Mass': 74.922, 'Type': 'Semimetal', 'Radioactive': False} elements['Se'] = {'name': 'Selenium', 'number': 34, 'Mass': 78.972, 'Type': 'Nonmetal', 'Radioactive': False} elements['Br'] = {'name': 'Bromine', 'number': 35, 'Mass': 79.904, 'Type': 'Halogen', 'Radioactive': False} elements['Kr'] = {'name': 'Krypton', 'number': 36, 'Mass': 84.798, 'Type': 'Noble Gas', 'Radioactive': False} elements['Rb'] = {'name': 'Rubidium', 'number': 37, 'Mass': 85.468, 'Type': 'Alkali Metal', 'Radioactive': False} elements['Sr'] = {'name': 'Strontium', 'number': 38, 'Mass': 87.62, 'Type': 'Alkaline Earth', 'Radioactive': False} elements['Y'] = {'name': 'Yttrium', 'number': 39, 'Mass': 88.906, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Zr'] = {'name': 'Zirconium', 'number': 40, 'Mass': 91.224, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Nb'] = {'name': 'Niobium', 'number': 41, 'Mass': 92.906, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Mo'] = {'name': 'Molybdenum', 'number': 42, 'Mass': 95.95, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Tc'] = {'name': 'Technetium', 'number': 43, 'Mass': 98.907, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Ru'] = {'name': 'Ruthenium', 'number': 44, 'Mass': 101.07, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Rh'] = {'name': 'Rhodium', 'number': 45, 'Mass': 102.906, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Pd'] = {'name': 'Palladium', 'number': 46, 'Mass': 106.42, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Ag'] = {'name': 'Silver', 'number': 47, 'Mass': 107.868, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Cd'] = {'name': 'Cadmium', 'number': 48, 'Mass': 112.411, 'Type': 'Transistion Metal', 'Radioactive': False} elements['In'] = {'name': 'Indium', 'number': 49, 'Mass': 114.818, 'Type': 'Basic Metal', 'Radioactive': False} elements['Sn'] = {'name': 'Tin', 'number': 50, 'Mass': 118.711, 'Type': 'Basic Metal', 'Radioactive': False} elements['Sb'] = {'name': 'Antimony', 'number': 51, 'Mass': 121.760, 'Type': 'Semimetal', 'Radioactive': False} elements['Te'] = {'name': 'Tellurium', 'number': 52, 'Mass': 127.6, 'Type': 'Semimetal', 'Radioactive': False} elements['I'] = {'name': 'Iodine', 'number': 53, 'Mass': 126.904, 'Type': 'Halogen', 'Radioactive': False} elements['Xe'] = {'name': 'Xenon', 'number': 54, 'Mass': 131.294, 'Type': 'Noble Gas', 'Radioactive': False} elements['Cs'] = {'name': 'Cesium', 'number': 55, 'Mass': 132.905, 'Type': 'Alkali Metal', 'Radioactive': False} elements['Ba'] = {'name': 'Barium', 'number': 56, 'Mass': 137.328, 'Type': 'Alkaline Earth', 'Radioactive': False} elements['La'] = {'name': 'Lanthanum', 'number': 57, 'Mass': 138.905, 'Type': 'Lanthanide', 'Radioactive': False} elements['Ce'] = {'name': 'Cerium', 'number': 58, 'Mass': 140.116, 'Type': 'Lanthanide', 'Radioactive': False} elements['Pr'] = {'name': 'Praseodymium', 'number': 59, 'Mass': 140.908, 'Type': 'Lanthanide', 'Radioactive': False} elements['Nd'] = {'name': 'Neodymium', 'number': 60, 'Mass': 144.242, 'Type': 'Lanthanide', 'Radioactive': False} elements['Pm'] = {'name': 'Prometheum', 'number': 61, 'Mass': 144.913, 'Type': 'Lanthanide', 'Radioactive': True} elements['Sm'] = {'name': 'Samarium', 'number': 62, 'Mass': 150.36, 'Type': 'Lanthanide', 'Radioactive': False} elements['Eu'] = {'name': 'Europium', 'number': 63, 'Mass': 151.964, 'Type': 'Lanthanide', 'Radioactive': False} elements['Gd'] = {'name': 'Gadolinium', 'number': 64, 'Mass': 157.25, 'Type': 'Lanthanide', 'Radioactive': False} elements['Tb'] = {'name': 'Terbuim', 'number': 65, 'Mass': 158.925, 'Type': 'Lanthanide', 'Radioactive': False} elements['Dy'] = {'name': 'Dysprosium', 'number': 66, 'Mass': 162.500, 'Type': 'Lanthanide', 'Radioactive': False} elements['Ho'] = {'name': 'Holmium', 'number': 67, 'Mass': 164.930, 'Type': 'Lanthanide', 'Radioactive': False} elements['Er'] = {'name': 'Erbium', 'number': 68, 'Mass': 167.259, 'Type': 'Lanthanide', 'Radioactive': False} elements['Tm'] = {'name': 'Thulium', 'number': 69, 'Mass': 168.934, 'Type': 'Lanthanide', 'Radioactive': False} elements['Yb'] = {'name': 'Ytterbium', 'number': 70, 'Mass': 173.055, 'Type': 'Lanthanide', 'Radioactive': False} elements['Lu'] = {'name': 'Lutetium', 'number': 71, 'Mass': 174.967, 'Type': 'Lanthanide', 'Radioactive': False} elements['Hf'] = {'name': 'Hafnium', 'number': 72, 'Mass': 178.49, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Ta'] = {'name': 'Tantalum', 'number': 73, 'Mass': 180.948, 'Type': 'Transistion Metal', 'Radioactive': False} elements['W'] = {'name': 'Tungsten', 'number': 74, 'Mass': 183.84, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Re'] = {'name': 'Rhenium', 'number': 75, 'Mass': 186.207, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Os'] = {'name': 'Osmium', 'number': 76, 'Mass': 190.23, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Ir'] = {'name': 'Iridium', 'number': 77, 'Mass': 192.217, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Pt'] = {'name': 'Platinum', 'number': 78, 'Mass': 195.085, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Au'] = {'name': 'Gold', 'number': 79, 'Mass': 196.967, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Hg'] = {'name': 'Mercury', 'number': 80, 'Mass': 200.592, 'Type': 'Transistion Metal', 'Radioactive': False} elements['Tl'] = {'name': 'Thallium', 'number': 81, 'Mass': 204.383, 'Type': 'Basic Metal', 'Radioactive': False} elements['Pb'] = {'name': 'Lead', 'number': 82, 'Mass': 207.2, 'Type': 'Basic Metal', 'Radioactive': False} elements['Bi'] = {'name': 'Bismuth', 'number': 83, 'Mass': 208.980, 'Type': 'Basic Metal', 'Radioactive': True} elements['Po'] = {'name': 'Polonium', 'number': 84, 'Mass': 208.982, 'Type': 'Semimetal', 'Radioactive': True} elements['At'] = {'name': 'Astatine', 'number': 85, 'Mass': 209.987, 'Type': 'Halogen', 'Radioactive': True} elements['Rn'] = {'name': 'Radon', 'number': 86, 'Mass': 222.018, 'Type': 'Noble Gas', 'Radioactive': True} elements['Fr'] = {'name': 'Francium', 'number': 87, 'Mass': 223.020, 'Type': 'Alkali Metal', 'Radioactive': True} elements['Ra'] = {'name': 'Radium', 'number': 88, 'Mass': 226.025, 'Type': 'Alkaline Earth', 'Radioactive': True} elements['Ac'] = {'name': 'Actinium', 'number': 89, 'Mass': 227.028, 'Type': 'Actinide', 'Radioactive': True} elements['Th'] = {'name': 'Thorium', 'number': 90, 'Mass': 232.038, 'Type': 'Actinide', 'Radioactive': True} elements['Pa'] = {'name': 'Protactinium', 'number': 91, 'Mass': 231.036, 'Type': 'Actinide', 'Radioactive': True} elements['U'] = {'name': 'Uranium', 'number': 92, 'Mass': 238.029, 'Type': 'Actinide', 'Radioactive': True} elements['Np'] = {'name': 'Neptunium', 'number': 93, 'Mass': 237.048, 'Type': 'Actinide', 'Radioactive': True} elements['Pu'] = {'name': 'Plutonium', 'number': 94, 'Mass': 244.064, 'Type': 'Actinide', 'Radioactive': True} elements['Am'] = {'name': 'Americium', 'number': 95, 'Mass': 243.061, 'Type': 'Actinide', 'Radioactive': True} elements['Cm'] = {'name': 'Curium', 'number': 96, 'Mass': 247.070, 'Type': 'Actinide', 'Radioactive': True} elements['Bk'] = {'name': 'Berkelium', 'number': 97, 'Mass': 247.070, 'Type': 'Actinide', 'Radioactive': True} elements['Cf'] = {'name': 'Californium', 'number': 98, 'Mass': 251.080, 'Type': 'Actinide', 'Radioactive': True} elements['Es'] = {'name': 'Einsteinium', 'number': 99, 'Mass': 254, 'Type': 'Actinide', 'Radioactive': True} elements['Fm'] = {'name': 'Fermium', 'number': 100, 'Mass': 257.095, 'Type': 'Actinide', 'Radioactive': True} elements['Md'] = {'name': 'Mendelevium', 'number': 101, 'Mass': 258.1, 'Type': 'Actinide', 'Radioactive': True} elements['No'] = {'name': 'Nobelium', 'number': 102, 'Mass': 259.101, 'Type': 'Actinide', 'Radioactive': True} elements['Lr'] = {'name': 'Lawrencium', 'number': 103, 'Mass': 262, 'Type': 'Actinide', 'Radioactive': True} elements['Rf'] = {'name': 'Rutherfordium', 'number': 104, 'Mass': 261, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Db'] = {'name': 'Dubnium', 'number': 105, 'Mass': 262, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Sg'] = {'name': 'Seaborgium', 'number': 106, 'Mass': 266, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Bh'] = {'name': 'Bohrium', 'number': 107, 'Mass': 264, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Hs'] = {'name': 'Hassnium', 'number': 108, 'Mass': 269, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Mt'] = {'name': 'Meitnerium', 'number': 109, 'Mass': 268, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Ds'] = {'name': 'Darmstadium', 'number': 110, 'Mass': 269, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Rg'] = {'name': 'Roetgenium', 'number': 111, 'Mass': 272, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Cn'] = {'name': 'Copernicium', 'number': 112, 'Mass': 277, 'Type': 'Transistion Metal', 'Radioactive': True} elements['Nh'] = {'name': 'Nihonium', 'number': 113, 'Mass': 'Unknown', 'Type': 'Basic Metal', 'Radioactive': True} elements['Fl'] = {'name': 'Ferovium', 'number': 114, 'Mass': 289, 'Type': 'Basic Metal', 'Radioactive': True} elements['Mc'] = {'name': 'Moscovium', 'number': 115, 'Mass': 'Unknown', 'Type': 'Basic Metal', 'Radioactive': True} elements['Lv'] = {'name': 'Livermorium', 'number': 116, 'Mass': 298, 'Type': 'Basic Metal', 'Radioactive': True} elements['Ts'] = {'name': 'Tennessine', 'number': 117, 'Mass': 'Unknown', 'Type': 'Halogen', 'Radioactive': True} elements['Og'] = {'name': 'Oganesson', 'number': 118, 'Mass': 'Unknown', 'Type': 'Noble Gas', 'Radioactive': True} usr_inp = input("What would you like to search for? ") print elements[usr_inp] RE: Pulling any information from a dictionary with a user input - heiner55 - Nov-21-2017 usr_inp = input("What would you like to search for? ") elem = elements[usr_inp] print(elem["name"]) print(elem["number"]) #... RE: Pulling any information from a dictionary with a user input - Larz60+ - Nov-21-2017 Since you are recent, I'd suggest you switch to python 3.6 now. There is so much more to offer, and python 2's life span is comming to a close. It will only be supported a few more years (2020) RE: Pulling any information from a dictionary with a user input - iFunKtion - Nov-21-2017 hi, Like Larz60+ suggested, move onto python3 or 3.6 and learn that. As for your question, if it is just an exercise in working with dictionaries, heiner55 has everything you need to access some of your data. Another way to approach this would be to use a database, either mysql or nosql, either way, database information is completely invaluable as you will end up working with them at some point. Postgres seems to be a popular choice, but there is also sqlite on the sql side of things, or there is Couchdb or Mongodb on the nosql side of things. Which ever route you might like to try, a database is definitely the most efficient way to go as you can then pull any data you want in any way which can then be refined in python. It's not hard to build a small scalable command line application from that point. RE: Pulling any information from a dictionary with a user input - buran - Nov-21-2017 It's just about accessing dictionary data. You may need to loop (one way or another) over the dict to find all elements that satisfy certain criteria. below are some examples, but it's not exhaustive list of approaches. assuming dict is named periodic_table (I will come to this a bit later)# using for loop for element in periodic_table.values(): if element['Type'].endswith('Metal'): print(element) # using list comprehension radioactive_elements = [element['name'] for element in periodic_table.values() if element['Radioactive']] print(radioactive_elements) # using built-in function filter for element in filter((lambda x: x['Type'] == 'Nonmetal'), periodic_table.values()): print('{name} : {Mass}'.format(**element))you create your dict in a very naive, although perfectly valid way. Instead of creating empty dict and assign key, value pairs, you can simply create dict like this periodic_table = {'H':{'name': 'Hydrogen', 'number': 1, 'Mass': 1.008, 'Type': 'Nonmetal', 'Radioactive': False}, 'He': {'name': 'Helium', 'number': 2, 'Mass': 4.003, 'Type': 'Noble Gas', 'Radioactive': False}, .... here go rest of the elements .... } I would make all keys either lower case or first char upper case. at the moment some of the keys are lowercase , e.g. 'name' and some have first char upper case, e.g. 'Mass'. I will choose one or the other and stick to it. iFunKtion already mentioned database and this is fine, but at least I would consider to put the periodic table data in external file, e.g. json (see attached). the access it like this import json with open('mendeleev.json', 'r') as f: periodic_table = json.load(f)Finally, if you plan to work with periodic table in long run, you don't need to invent the wheel - there are several packaged that have implemented the periodic table of elements and provide nice functionality to work with the table. have a look at some of them: https://pypi.python.org/pypi?%3Aaction=search&term=periodic+table RE: Pulling any information from a dictionary with a user input - metulburr - Nov-21-2017 There is already a library that contains this information as well as common methods such as mix_by_volume() etc. pip install periodictable
although i am not sure if they have a search method based on name/number/isotope, etc. I would assume so. You would have to look more into it.
RE: Pulling any information from a dictionary with a user input - buran - Nov-21-2017 here it is RE: Pulling any information from a dictionary with a user input - Darmanus - Nov-22-2017 The only reason that I've not updated is that it's part of a course that I'm doing just to get me introduced to programming, and will be moving on to C# at some point. The reason I created this dictionary is because the lessons started it off and I felt bad leaving it unfinished, plus the learning how to do it yourself rather than inserting another file to do it for you! This has given me some insight into how I can refine the work that I've already done as well! Thanks for the help, all! RE: Pulling any information from a dictionary with a user input - heiner55 - Nov-22-2017 C# is a good choice but only if you are programming for windows. RE: Pulling any information from a dictionary with a user input - Darmanus - Nov-22-2017 (Nov-21-2017, 07:20 PM)buran Wrote: It's just about accessing dictionary data. You may need to loop (one way or another) over the dict to find all elements that satisfy certain criteria. below are some examples, but it's not exhaustive list of approaches. I tried both of these bits, but anything that I put after the dictionary now returns the following syntax error: && echo Exit status: $? && exit 1 File "/Users/admin/Desktop/PeriodicTable.py", line 254 for element in periodic_table.values(): ^ SyntaxError: invalid syntaxEven simply trying to print produces a syntax error It is, however, entirely possible I'm being dumb |