Mendeleiev's periodic table in python

Gribouillis 1 Tallied Votes 785 Views Share

This snippet allows your code to use the Mendeleiev's periodic table of elements. It defines a single function mendeleiev_table() which returns the table as a python list of lists.

# source: Gribouillis at www.daniweb.com - 2008

def mendeleiev_table ():
  """Returns the mendeleiev table as a python list of lists.
     Each cell contains either None or a pair (symbol, number),
     or a list of pairs for the cells * and **.
  """
  data ="""
2008 transposed mendeleiev table with atomic numbers 
BEGIN
H  1 | Li 3  | Na 11 | K  19 | Rb 37 | Cs 55 | Fr  87
     | Be 4  | Mg 12 | Ca 20 | Sr 38 | Ba 56 | Ra  88
     |       |       | Sc 21 | Y  39 |   *   |   **
     |       |       | Ti 22 | Zr 40 | Hf 72 | Rf  104
     |       |       | V  23 | Nb 41 | Ta 73 | Db  105
     |       |       | Cr 24 | Mo 42 | W  74 | Sg  106
     |       |       | Mn 25 | Tc 43 | Re 75 | Bh  107
     |       |       | Fe 26 | Ru 44 | Os 76 | Hs  108
     |       |       | Co 27 | Rh 45 | Ir 77 | Mt  109
     |       |       | Ni 28 | Pd 46 | Pt 78 | Ds  110
     |       |       | Cu 29 | Ag 47 | Au 79 | Rg  111
     |       |       | Zn 30 | Cd 48 | Hg 80 | Uub 112
     | B  5  | Al 13 | Ga 31 | In 49 | Tl 81 | Uut 113
     | C  6  | Si 14 | Ge 32 | Sn 50 | Pb 82 | Uuq 114
     | N  7  | P  15 | As 33 | Sb 51 | Bi 83 | Uup 115
     | O  8  | S  16 | Se 34 | Te 52 | Po 84 | Uuh 116
     | F  9  | Cl 17 | Br 35 | I  53 | At 85 | Uus 117
He 2 | Ne 10 | Ar 18 | Kr 36 | Xe 54 | Rn 86 | Uuo 118
END
*       **
La 57 | Ac 89
Ce 58 | Th 90
Pr 59 | Pa 91
Nd 60 | U 92
Pm 61 | Np 93
Sm 62 | Pu 94
Eu 63 | Am 95
Gd 64 | Cm 96
Tb 65 | Bk 97
Dy 66 | Cf 98
Ho 67 | Es 99
Er 68 | Fm 100
Tm 69 | Md 101
Yb 70 | No 102
Lu 71 | Lr 103
"""
  lines =[line .strip ()for line in data .split ("\n")]
  begin =lines .index ("BEGIN")
  end =lines .index ("END")
  nrows =len (lines [begin +1 ].split ("|"))
  ncols =end -begin -1 
# create initial table with empty cells
  table =[[None ]*ncols for i in range (nrows )]
# read lanthanids and actinids
  dic ={"*":[],"**":[]}
  for x ,y in [line .split ("|")for line in lines [end +2 :-1 ]]:
    e ,n =x .strip ().split ()
    dic ["*"].append ((e ,int (n )))
    e ,n =y .strip ().split ()
    dic ["**"].append ((e ,int (n )))
  for col ,line in enumerate (lines [begin +1 :end ]):
    for row ,content in enumerate (line .split ("|")):
      content =content .strip ()
      if content in dic :
        table [row ][col ]=dic [content ]
      elif content :
        e ,n =content .split ()
        table [row ][col ]=(e ,int (n ))
  return table
Gribouillis 1,391 Programming Explorer Team Colleague

I missed the following shorter function with the same output :)

def mendeleiev_table ():
  """Returns the mendeleiev table as a python list of lists.
     Each cell contains either None or a pair (symbol, atomic number),
     or a list of pairs for the cells * and **.
  """
  import re 
  L =[(e ,i +1 )for (i ,e )in enumerate (
  re .compile ("[A-Z][a-z]*").findall ("""
    HHeLiBeBCNOFNeNaMgAlSiPSClArKCaScTiVCrMnFeCoNiCuZnGaGeAsSeBrKr
    RbSrYZrNbMoTcRuRhPdAgCdInSnSbTeIXeCsBaLaCePrNdPmSmEuGdTbDyHoEr
    TmYbLuHfTaWReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaUNpPuAmCmBkCfEsFm
    MdNoLrRfDbSgBhHsMtDsRgUubUutUuqUupUuhUusUuo"""))]
  for i ,j in ((88 ,103 ),(56 ,71 )):
    L [i ]=L [i :j ]
    L [i +1 :]=L [j :]
  for i ,j in ((12 ,10 ),(4 ,10 ),(1 ,16 )):
    L [i :i ]=[None ]*j 
  return [L [18 *i :18 *(i +1 )]for i in range (7 )]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.