kunal sharma kunal sharma - 3 months ago 157
Python Question

i hadtake input as list of string but i am not able to process list 'd' to obtain required output

input from the keyboard (standard input) containing the results of several tennis matches. Each match's score is recorded on a separate line with the following format:

Winner:Loser:Set-1-score,...,Set-k-score, where 2 <= k <= 5

For example, an input line of the form

Williams:Muguruza:3-6,6-3,6-3

The input is terminated by a blank line.

Python program that reads information about all the matches and compile the following statistics for each player:


  1. Number of best-of-5 set matches won

  2. Number of best-of-3 set matches won

  3. Number of sets won

  4. Number of games won

  5. Number of sets lost

  6. Number of games lost



print out to the screen (standard output) a summary in decreasing order of ranking, where the ranking is according to the criteria 1-6 in that order (compare item 1, if equal compare item 2, if equal compare item 3 etc, noting that for items 5 and 6 the comparison is reversed).

For instance, given the following data

Djokovic:Murray:2-6,6-7,7-6,6-3,6-1
Murray:Djokovic:6-3,4-6,6-4,6-3
Djokovic:Murray:6-0,7-6,6-7,6-3
Murray:Djokovic:6-4,6-4
Djokovic:Murray:2-6,6-2,6-0
Murray:Djokovic:6-3,4-6,6-3,6-4
Djokovic:Murray:7-6,4-6,7-6,2-6,6-2
Murray:Djokovic:7-5,7-5
Williams:Muguruza:3-6,6-3,6-3

your program should print out the following

Djokovic 3 1 13 142 16 143
Murray 2 2 16 143 13 142
Williams 0 1 2 15 1 12
Muguruza 0 0 1 12 2 15

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) 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 separators 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 separator 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