简体   繁体   English

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

[英]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 : 然后应用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)]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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