找到你要的答案

Q:How to manipulate a tuple that contains multiple tuple

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".
answer1: 回答1:
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”,也忽视等。

然后我们将“每个字典上面得到的一个关键是用户名:(T [ 0 ],dict(拉链的…我们得到一个元组。

最后从元组列表中我们创建一个字典使用的内置函数。

上面的代码会将你的输入元组元组的一个像字典词典:

{'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