我不该这么做 但我觉得运动很滑稽
lines = [i.strip() for i in l if i.strip()]
abbrev = dict(zip(lines[0::2], lines[1::2]))
然后abbrev
是一个字典/绘图,您通过将整个名字作为密钥查找获得缩略语 。
如果你想听懂我的话 我建议你用互动的 Python 贝壳 分别试试每一块
Edit for lazy StackOverflow readers
在此交互式会议示例中,还提出一些补充建议:
>>> infile = open("abbreviations.txt", "r")
>>> lines = [l.strip() for l in infile]
>>> lines
[ BURGER KING , , BRG , , MCDONALDS , , MCDNLDS , , WENDYS , , WNDY ]
>>> lines[0::4]
[ BURGER KING , MCDONALDS , WENDYS ]
>>> lines[2::4]
[ BRG , MCDNLDS , WNDY ]
>>> zip(lines[0::4], lines[2::4])
[( BURGER KING , BRG ), ( MCDONALDS , MCDNLDS ), ( WENDYS , WNDY )]
>>> abbrev = dict(zip(lines[0::4], lines[2::4]))
>>> abbrev
{ MCDONALDS : MCDNLDS , WENDYS : WNDY , BURGER KING : BRG }
>>> abbrev["WENDYS"]
WNDY