Python: How to write for every x use x? -


#python  list="""a 1 2 3 4 5 b 1 2 3 4 5 c 1 2 3 4 5 """   a=list.split('\n') if a[0][0]==a:     if a[1][0]==b:     if a[2][0]==c:     

is there automated way python read first part of each line? instead of doing above? i'm trying use first str char on each line way python recognize next action take.

you can create dictionary , map 'a','b','c' functions.:

def func1(): print "func1"  def func2(): print "func2"  def func3(): print "func3"  dic={"a":func1,"b":func2,"c":func3}  lis="""a 1 2 3 4 5 b 1 2 3 4 5 c 1 2 3 4 5 """  item in lis.splitlines():     dic[item[0]]() 

output:

func1 func2 func3 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -