[Tutor] input and raw input

Evert Rol evert.rol at gmail.com
Sat Sep 25 19:14:31 CEST 2010


> > any one have an idea about how we can input many number in the one time and change it to list.
> > for example:
> >
> > a=input("Enter the number of your class in the school:")     # the number can be enter as: 12,13,14 or 12 13 14 with a space in between.
> >
> > now how I can put these numbers into list like b=[12,13,14] with len( a ) =3
> 
> A string has a method split(); that may help you.
> In your case, where you want either a space or a comma as a separator, it depends whether both can be used at the same time. If not, you can check for the occurrence of one or the other separator and run split() with the correct separator. If both can occur in the same line, you may want to use the regex module instead: re.split()
> 
> No need for the 're' module. Even in the case where both can be used together, you can still just use string methods: 
> 
> >>> s
> '12, 13 14'
> >>> s.replace(',', '').split(' ')
> ['12', '13', '14']

Good point.
To be finicky, you'll probably want to replace ',' by ' ' and let split work on whitespace instead of a single space. In case of tabs or a 12,13,14 input.


> > I tried with that but it's working only for a numbers less than 10 ex. 1,2,3 or 1 2 3 but it's not when I go for numbers higher than 10 like in example above.
> >
> > a=raw_input("Enter the number of your class in the school:")
> > m=[]
> >  for I range (len( a)):
> >     if a[I]==',':
> >         pass
> >     elif a[I]==' ':
> >         pass
> >     else:
> >         m.append(a[I])
> > m=map(float,m)
> > print m;print len( m )
> > >> [1,2,3]
> > >> 3
> >
> > looking forward to seeing your help,
> > regards,
> > Ahmed
> >
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > To unsubscribe or change subscription options:
> > http://mail.python.org/mailman/listinfo/tutor
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 
> -- 
> Brian K. Jones
> My Blog          http://www.protocolostomy.com
> Follow me      http://twitter.com/bkjones



More information about the Tutor mailing list