开发者

Reading space separated input in Python

开发者 https://www.devze.com 2023-04-10 14:29 出处:网络
Here is the input specification: The program has to read t lines of inputs. Each line consist of 2 space separated values first one is the name and second is the age.

Here is the input specification:

The program has to read t lines of inputs. Each line consist of 2 space separated values first one is the name and second is the age.

An Example of开发者_StackOverflow社区 input:

Mike 18
Kevin 35
Angel 56

How to read this kind of input in Python? If I use raw_input(), both name and age are read in the same variable.


the_string = input()
name, age = the_string.split()

In Python 2, use raw_input instead of input.


If you have it in a string, you can use .split() to separate them.

>>> for string in ('Mike 18', 'Kevin 35', 'Angel 56'):
...   l = string.split()
...   print repr(l[0]), repr(int(l[1]))
...
'Mike' 18
'Kevin' 35
'Angel' 56
>>>


Assuming you are on Python 3, you can use this syntax

inputs = list(map(str,input().split()))

if you want to access individual element you can do it like that

m, n = map(str,input().split())


You can do the following if you already know the number of fields of the input:

client_name = raw_input("Enter you first and last name: ")
first_name, last_name = client_name.split() 

and in case you want to iterate through the fields separated by spaces, you can do the following:

some_input = raw_input() # This input is the value separated by spaces
for field in some_input.split():
    print field # this print can be replaced with any operation you'd like
    #             to perform on the fields.

A more generic use of the "split()" function would be:

    result_list = some_string.split(DELIMITER)

where DELIMETER is replaced with the delimiter you'd like to use as your separator, with single quotes surrounding it.

An example would be:

    result_string = some_string.split('!')    

The code above takes a string and separates the fields using the '!' character as a delimiter.


For python 3 it would be like this n,m,p=map(int,input().split())


For python 3 use this

inp = list(map(int,input().split()))
#input => java is a programming language
#return as => ("java","is","a","programming","language")

input() accepts a string from STDIN.

split() splits the string about whitespace character and returns a list of strings.

map() passes each element of the 2nd argument to the first argument and returns a map object

Finally list() converts the map to a list


a=[]
for j in range(3):
    a.append([int(i) for  i in input().split()])

In this above code the given input i.e Mike 18 Kevin 35 Angel 56, will be stored in an array 'a' and gives the output as [['Mike', '18'], ['Kevin', '35'], ['Angel', '56']].


Space separated values inside an array with specific range:

arr = list(map(int, input().split()))[:n]

here n is denoting the maximum length of list.


For Python3:

a, b = list(map(str, input().split()))
v = int(b)
0

精彩评论

暂无评论...
验证码 换一张
取 消