list access e[-1] is fine but e[1] is not in Python -
in python, defined
string = ("car-automobile, gem-jewel, journey-voyage, boy-lad, coast-shore, " "asylum-madhouse, magician-wizard, midday-noon, furnacestove, food-fruit, " "bird-cock, bird-crane, tool-implement, brother-monk, ladbrother, " "crane-implement, journey-car, monk-oracle, cemetery-woodland, foodrooster, " "coast-hill, forest-graveyard, shore-woodland, monk-slave, coast-forest, " "lad-wizard, chord-smile, glass-magician, rooster-voyage, " "noon-string".split(', ')) test = [i.split('-') in string] and following code causes error:
[e[1] e in test] traceback (most recent call last): file "<pyshell#136>", line 1, in <module> [e[1] e in test] indexerror: list index out of range but following code works
[e[-1] e in test] why so?
you have few values no - in them:
>>> [e e in string if not '-' in e] ['furnacestove', 'ladbrother', 'foodrooster'] which when split results in one-element list; there e[0], no e[1]. e[-1] gives last element always, if there 1.
Comments
Post a Comment