如何根据 Python 中的特定条件拆分列表的列表?

How can I split a list of lists based on a specific condition in Python?

我有一份篮球比赛的单节比分列表,如下所示:

qt_sc = [('30', '12'), ('22', '25'), ('11', '16'), ('13', '19'), ('18', '26'), ('19', '13'), ('14', '14'), ('20', '12'), ('18', '21'), ('9', '9'), ('22', '12'), ('14', '21'), ('6', '6'), ('12', '3'), ('20', '18'), ('19', '15'), ('23', '20'), ('27', '20'), ('22', '16'), ('18', '20'), ('24', '10'), ('26', '19'), ('12', '23'), ('21', '28'), ('21', '28'), ('25', '24'), ('18', '24'), ('15', '18'), ('20', '22'), ('23', '14')]

加时赛有四节,最多四节。

我的目标是将此列表拆分为单独的匹配项,但可能存在的联系使这变得困难。 上面的列表是:

qt_sc = [[('30', '12'), ('22', '25'), ('11', '16'), ('13', '19')],
         [('18', '26'), ('19', '13'), ('14', '14'), ('20', '12')],
         [('18', '21'), ('9', '9'), ('22', '12'), ('14', '21'), ('6', '6'), ('12', '3')],
         [('20', '18'), ('19', '15'), ('23', '20'), ('27', '20')],
         [('22', '16'), ('18', '20'), ('24', '10'), ('26', '19')],
         [('12', '23'), ('21', '28'), ('21', '28'), ('25', '24')],
         [('18', '24'), ('15', '18'), ('20', '22'), ('23', '14')]]

我下面的代码捕获了加时赛的第一节,但没有捕获其余时间:

qt_sc2 = []
tie = ""
for i in range(0, len(qt_sc), 4):
    if tie:
        i += 1
    try:
        hp = sum(map(int, [x[0] for x in qt_sc[i:i+4]]))
        ap = sum(map(int, [x[1] for x in qt_sc[i:i+4]]))
    except:
        hp, ap = 0, 1
    if hp == ap:
        if hp != 0:
            hp, ap = 0, 0
            qt_sc2.append([y for x in qt_sc[i:i+4+1] for y in x])
            tie = "YES"
    else:
        qt_sc2.append([y for x in qt_sc[i:i+4] for y in x])
print qt_sc2

试试这个,对我有用:

qt_sc = [('30', '12'), ('22', '25'), ('11', '16'), ('13', '19'), ('18', '26'), ('19', '13'), ('14', '14'), ('20', '12'), ('18', '21'), ('9', '9'), ('22', '12'), ('14', '21'), ('6', '6'), ('12', '3'), ('20', '18'), ('19', '15'), ('23', '20'), ('27', '20'), ('22', '16'), ('18', '20'), ('24', '10'), ('26', '19'), ('12', '23'), ('21', '28'), ('21', '28'), ('25', '24'), ('18', '24'), ('15', '18'), ('20', '22'), ('23', '14')]

qt_sc2 = []
first = []
second = []

for qt in qt_sc:
    hp = sum(int(f) for f in first)
    ap = sum(int(s) for s in second)
    if len(first) // 4 > 0 and hp != ap:
        qt_sc2.append(zip(first, second))
        first = []
        second = []
    first.append(qt[0])
    second.append(qt[1])


qt_sc2

[[('30', '12'), ('22', '25'), ('11', '16'), ('13', '19')],
 [('18', '26'), ('19', '13'), ('14', '14'), ('20', '12')],
 [('18', '21'), ('9', '9'), ('22', '12'), ('14', '21'), ('6', '6'), ('12', '3')],
 [('20', '18'), ('19', '15'), ('23', '20'), ('27', '20')],
 [('22', '16'), ('18', '20'), ('24', '10'), ('26', '19')],
 [('12', '23'), ('21', '28'), ('21', '28'), ('25', '24')]]

这样做就可以了:

qt_sc = [('30', '12'), ('22', '25'), ('11', '16'), ('13', '19'), ('18', '26'), ('19', '13'), ('14', '14'), ('20', '12'), ('18', '21'), ('9', '9'), ('22', '12'), ('14', '21'), ('6', '6'), ('12', '3'), ('20', '18'), ('19', '15'), ('23', '20'), ('27', '20'), ('22', '16'), ('18', '20'), ('24', '10'), ('26', '19'), ('12', '23'), ('21', '28'), ('21', '28'), ('25', '24'), ('18', '24'), ('15', '18'), ('20', '22'), ('23', '14')]
qt_sc = zip(map(int, [x[0] for x in qt_sc]), map(int, [x[1] for x in qt_sc]))

def check_for_tie(game):
    left_score = sum([x[0] for x in game])
    right_score = sum([x[1] for x in game])
    # print "Left Score: " + str(left_score) + " Right Score: " + str(right_score)
    if left_score == right_score:
        return True
    return False

counter = 0 
output = []
i = 0

while counter < len(qt_sc):
    overtime_per = 0 
    game = qt_sc[counter:counter+4]
    while check_for_tie(game):
        overtime_per += 1
        game = qt_sc[counter:counter+4+overtime_per]

    output.append(game)
    counter = counter + 4 + overtime_per

for game in output:
    print game
qt_sc = [('30', '12'), ('22', '25'), ('11', '16'), ('13', '19'), ('18', '26'), ('19', '13'), ('14', '14'), ('20',       '12'), ('18', '21'), ('9', '9'), ('22', '12'), ('14', '21'), ('6', '6'), ('12', '3'), ('20', '18'), ('19', '15'),       ('23', '20'), ('27', '20'), ('22', '16'), ('18', '20'), ('24', '10'), ('26', '19'), ('12', '23'), ('21', '28'), ('21',  '28'), ('25', '24'), ('18', '24'), ('15', '18'), ('20', '22'), ('23', '14')]

games = []
game = []
for team1, team2 in qt_sc:
    game.append((team1, team2))
    if len(game) >= 4 and sum(int(i) - int(j) for i, j in game) != 0:
        games.append(game)
        game = []

for game in games:
    print(game)

除了已经提供的解决方案之外,所有替代方案都是将给定的列表分成两部分,属于第一场比赛的四分之一和其余四分之一。

循环执行此操作,直到列表用完:

def get_first_game(qt_sc):
    """ takes a list of quarter scores and returns the list split
        in two parts, those belonging to a single game from the start
        of the list, and the rest of the list.
    """
    n = 4
    team1_score = sum(x for x, y in qt_sc[0:4])
    team2_score = sum(y for x, y in qt_sc[0:4])
    if team2_score == team1_score:
        for x, y in qt_sc[4:]:
            n += 1
            if x != y:
                break
    return qt_sc[0:n], qt_sc[n:]

def get_games(qt_sc):
    """ applies the function get_first_game repeatedly
        to split up all the games
    """
    games = []
    while True:
        a, qt_sc = get_first_game(qt_sc)
        games.append(a)
        if qt_sc == []:
            break
    return games

首先,将字符串元组转换为整数元组。然后应用 get_games 函数:

import pprint
scores = [(int(x), int(y)) for x, y in qt_sc]
pprint.pprint(get_games(scores))
[[(30, 12), (22, 25), (11, 16), (13, 19)],
 [(18, 26), (19, 13), (14, 14), (20, 12)],
 [(18, 21), (9, 9), (22, 12), (14, 21), (6, 6), (12, 3)],
 [(20, 18), (19, 15), (23, 20), (27, 20)],
 [(22, 16), (18, 20), (24, 10), (26, 19)],
 [(12, 23), (21, 28), (21, 28), (25, 24)],
 [(18, 24), (15, 18), (20, 22), (23, 14)]]