简体   繁体   中英

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

I have a list of quarters scores for basketball games like the one below:

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')]

There are four quarters and up to four quarters for extra time.

My aim is to split this list into the individual matches, but the possible ties make that difficult. The above list would be:

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')]]

My code below catches the first quarter of extra time but not the rest:

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

Try this, it works for me:

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')]]

This will do the trick:

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)

Al alternative to the solutions already provided would be to split the given list in two parts, the quarters belonging to first game and the rest of the quarters.

Do this in a loop until the list is exhausted:

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

First, convert the tuples of strings to tuples of integers. Then apply the get_games function :

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)]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM