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

时间:2015-01-01 01:17:06

标签: python list

我列出了以下一项篮球比赛的季度分数:

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

4 个答案:

答案 0 :(得分:1)

试试这个,它对我有用:

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

答案 1 :(得分:1)

这样可以解决问题:

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

答案 2 :(得分:1)

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)

答案 3 :(得分:0)

已经提供的解决方案的替代方案是将给定列表分成两部分,即属于第一场比赛的季度和剩余的季度。

在循环中执行此操作,直到列表用完为止:

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