# Q：如何处理包含多个元组的元组

For example, I have this tuple:

``````data =(('name/score','game1', 'game2', 'game3', 'game4', 'game5'),('A','1','2','3','4','5'),('B','6','7','8','9','10'),('C','11','12','13','14','15'))
``````

so data is a tuple that contains 4 smaller tuples that contains strings. Actually data is a 'table' which shows the name A, B, C and D and their respective scores. How to manipulate data, so that I extra informations I want in data?

For example, 1. how to split data into smaller tuples such as

``````tuple1 = ('name/score','game1', 'game2', 'game3', 'game4', 'game5')
tuple2 = ('A','1','2','3','4','5')
``````

and so on?

1. How to remove the 'names', which are A, B and C in each smaller tuple？ I did it by slicing:

newtuple = tuple1[1:]

Just wondering if there is a recursive way or iterative way to do it, cause I dont really get the idea of iteration and recursion.

1. Is there anyway to define a function which can retrieve the data I want? for example, I want to know to score of A in game 3, the function should return "3".

``````data =(('name/score','game1', 'game2', 'game3', 'game4', 'game5'),('A','1','2','3','4','5'),('B','6','7','8','9','10'),('C','11','12','13','14','15'))
``````

so data is a tuple that contains 4 smaller tuples that contains strings. Actually data is a 'table' which shows the name A, B, C and D and their respective scores. How to manipulate data, so that I extra informations I want in data?

For example, 1. how to split data into smaller tuples such as

``````tuple1 = ('name/score','game1', 'game2', 'game3', 'game4', 'game5')
tuple2 = ('A','1','2','3','4','5')
``````

1. How to remove the 'names', which are A, B and C in each smaller tuple？ I did it by slicing:

[ 1 ] TUPLE1 Newtuple =

1. Is there anyway to define a function which can retrieve the data I want? for example, I want to know to score of A in game 3, the function should return "3".
``````data =(('name/score','game1', 'game2', 'game3', 'game4', 'game5'),('A','1','2','3','4','5'),('B','6','7','8','9','10'),('C','11','12','13','14','15'))
``````

The first element of your tuple is a sort of header (like in an excel file the first line). You want to construct a dictionary of dictionaries where the first level keys are the users (A, B, C, etc.) and the second level of dictionaries have keys like game1, game2, etc. with the value representing the score reached in the given game.

``````D = dict((t[0], dict(zip(data[0][1:], t[1:]))) for t in data[1:])
``````

dict(zip(data[0][1:], t[1:])) is the part where you create a dictionary from every tuple of data (starting the second tuple) as values using keys from the first tuple of data ("game1", "game2", etc.). We deliberately ignore the very first element of all tuples: "name/score" is ignored, and the user names "A", "B", etc. are also ignored.

Then we "attach" to each dictionary obtained above to a key which is the username: (t[0], dict(zip.... and we obtain a tuple.

Finally from the list of tuples we create a dictionary using the dict builtin function.

The above code will convert your input tuple of tuples to a dictionary of dictionaries like:

``````{'A': {'game1': '1', 'game2': '2', 'game3': '3', 'game4': '4', 'game5': '5'},
'B': {'game1': '6', 'game2': '7', 'game3': '8', 'game4': '9', 'game5': '10'},
'C': {'game1': '11', 'game2': '12', 'game3': '13', 'game4': '14', 'game5': '15'}}
``````

To get the score of user A in game3 you write:

``````>>D["A"]["game3"]
3
``````

Since you commented that you don't want to use dictionaries, here is a function that should satisfy your needs:

``````def get_score(D, user, game):
i = D[0].index(game)
for t in D[1:]:
if t[0] == user:
return t[i]

print get_score(data, "A", "game3")
``````
``````data =(('name/score','game1', 'game2', 'game3', 'game4', 'game5'),('A','1','2','3','4','5'),('B','6','7','8','9','10'),('C','11','12','13','14','15'))
``````

The first element of your tuple is a sort of header (like in an excel file the first line). You want to construct a dictionary of dictionaries where the first level keys are the users (A, B, C, etc.) and the second level of dictionaries have keys like game1, game2, etc. with the value representing the score reached in the given game.

``````D = dict((t[0], dict(zip(data[0][1:], t[1:]))) for t in data[1:])
``````

dict（邮编（数据[ 0 ] [ 1 ] [ 1 ]，t））是你创建从每个数据元字典的部分（从第二元组）使用数据的第一个元组键的值（“场”、“游戏”等）。我们故意忽略所有元组的第一个元素：“名字/得分”是不容忽视的，和用户的名字“A”，“B”，也忽视等。

``````{'A': {'game1': '1', 'game2': '2', 'game3': '3', 'game4': '4', 'game5': '5'},
'B': {'game1': '6', 'game2': '7', 'game3': '8', 'game4': '9', 'game5': '10'},
'C': {'game1': '11', 'game2': '12', 'game3': '13', 'game4': '14', 'game5': '15'}}
``````

``````>>D["A"]["game3"]
3
``````

``````def get_score(D, user, game):
i = D[0].index(game)
for t in D[1:]:
if t[0] == user:
return t[i]

print get_score(data, "A", "game3")
``````
python  tuples