[英]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)]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.