kunal sharma kunal sharma - 3 months ago 13
Python Question

how get input of multiple lines

I have written code to take input as list of string

d=["","","","","","","","","",""]
i=0
while(True):
s=input()
d[i]=s
i=i+1
if s=="":
break


but i am not able to process list 'd' to obtain required output
d[0] is storing "Djokovic:Murray:2-6,6-7,7-6,6-3,6-1" now i want to process this string(Either converting into dictionary)

Now i am able write code(by using str.split(",") function) to calculate Number of best-of-5 set matches won, Number of best-of-3 set matches won, Number of sets won, Number of games won, Number of sets lost, Number of games lost

Answer

Is this what you are after?

>>> d=[]
>>> while(True):
...   s=raw_input()
...   if s=="":break
...   temp = [s]
...   d.append(temp)
... 
a,b,7-6,7-6,6-3
c,d,7-4,7-6,6-2
e,f,6-4,7-6,6-2

>>> d
[['a,b,7-6,7-6,6-3'], ['c,d,7-4,7-6,6-2'], ['e,f,6-4,7-6,6-2']]

This makes a list item out of the input and then appends that list to your main list d
You now should be able to process d

Edit:

If you persist in using 2 delimiters both : and , you are making life more difficult for yourself, Stick with one!
Revising the simple code above:

d=[]
while(True):
    s=raw_input()
    if s=="":break
    temp = [s]
    d.append(temp)              #d becomes a list of lists
for item in d:                  #process individual lists in d
    x=item[0].split(",")        # break up the list using the delimiter comma
    for i in range(0,len(x)):   #access each item in x
        print x[i]

Input:
Djokovic,Murray,2-6,6-7,7-6,6-3,6-1
Bloggs,Smith,2-6,6-7,7-6,6-3,6-3
Jones,Abernathy,6-3,6-3,6-3

Output:

Djokovic    
Murray
2-6
6-7
7-6
6-3
6-1
Bloggs
Smith
2-6
6-7
7-6
6-3
6-3
Jones
Abernathy
6-3
6-3
6-3