DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Code Snippet: Mendeleiev's periodic table in python (http://www.daniweb.com/forums/thread217156.html)

Gribouillis Oct 7th, 2008 7:57 am
Mendeleiev's periodic table in python
 
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.

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


All times are GMT -4. The time now is 7:53 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC