如何根据Python中的特定条件拆分列表列表?
我有一份篮球比赛四分之一的分数清单,如下所示:如何根据Python中的特定条件拆分列表列表?,python,list,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')
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','13'),('14','14'),('20','12'),('18','21'),('9','9'),('22','12'),('14','21'),('6','6'),('12','3'),('20','18','15'),('23','20'),('27','20'),('22','16'),'18','20','20','20'),('20','20','12'),'26','21'),('26','21'),'21'),'26','21'),'21'),('21'),'21','21','20','20','20',('21', '28'), ('25', '24'), ('18', '24'), ('15', '18'), ('20', '22'), ('23', '14')]
qt_sc=zip(map(int,[x[0]表示qt_sc中的x]),map(int,[x[1]表示qt_sc中的x]))
def检查比赛(游戏):
左_分数=总和([x[0]表示游戏中的x])
右_分数=总和([x[1]表示游戏中的x])
#打印“左分数:”+str(左分数)+“右分数:”+str(右分数)
如果左\u分数==右\u分数:
返回真值
返回错误
计数器=0
输出=[]
i=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\u 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)]]
如果没有办法破译哪些元素是加时赛分数,那几乎是不可能的。我对篮球比赛不太熟悉,为什么在你理想的比赛分割列表中,第二场比赛有四个四分之一,但有一个是平局?如果规则不是一场比赛有四个非平局四分之一(最多8个四分之一),这是什么?等一下,第一节加时赛而不是其他加时赛的意义是什么?如果一场比赛只有四节,这不是平局,如果有更多的加时赛,总共8节,这意味着在常规赛的四节比赛中是平局。如果最后一节是0,那么每节加时赛是5分钟如果两队得分更多,则为赢家,如果两队平手,则多出一个四分之一。你究竟是如何获得这些数据的,而不是一开始就按比赛进行划分的?
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)]]