我怎样才能使列表自行更新?
how can I make it that the list updates itself?
在每个“for”循环中,从“数据”列表中取出两项(用户和喜欢)并添加到单独的列表(记分牌)中。我正在尝试检测记分牌列表中是否已存在“用户”。如果它确实存在,那么我尝试在记分牌列表中获取“用户”之后的项目(这将是之前的“喜欢”项目)并向其中添加新的“喜欢”项目,之后我尝试设置新的“喜欢”项目到新的价值。但我无法让它工作。
def likecount():
scoreboard = []
for t in range(0,len(Data),3):
user = Data[t]
likes = Data[t + 2]
if user not in scoreboard:
scoreboard.append(user), scoreboard.append(likes)
else:
scoreboard[(scoreboard.index(user) + 1)] + likes == scoreboard[(scoreboard.index(user) + 1)]
for i in range(0,len(scoreboard),2):
print (scoreboard[i],scoreboard[i+1], end= "\n")
#数据列表:
['Rhett_andEmma77', 'Los Angeles is soooo hot', '0', 'Tato', 'Glitch', '1', 'Aurio', 'April fools tomorrow', '4', 'nap', 'The bully**', '3', 'NICKSION', 'Oops', '6', 'stupidfuck', 'hes a little guy', '5', 'database', 'Ty', '1', 'stupidfuck', 'possible object show (objects needed)', '3', 'NightSkyMusic', 'nicotine takes 10 seconds to reach your brain', '27', 'stupidfuck', '@BFBleafyfan99 be anii e', '4', 'Odminey', 'Aliveness', '26', 'stupidfuck', '@techness2011 the', '5', 'techness2011', 'Boomerang', '1', 'saltyipaint', 'April is r slur month', '5', 'HENRYFOLIO', 'flip and dunk', '4', 'SpenceAnimation', 'Got Any grapes ', '2', 'RainyFox2', 'Draw me in your style****', '1', 'hecksacontagon', 'funky cat guy impresses you with his fish corpse', '11', 'HENRYFOLIO', 'flip and dunk @bruhkeko version', '4', 'nairb', 'Spoderman turns green', '5', 'SpenceAnimation', 'Jellybean', '1', 'SpenceAnimation', '@FussiArt', '3']
字典正是您要查找的东西,而不是保留项目列表。因此,无论何时考虑用户,您都会检查 he/she 是否存在
- 如果存在,则将新的喜欢添加到旧的喜欢计数中。
- 如果没有,请创建一个具有类似计数的条目。
这是自然的解决方案。
data = ['Rhett_andEmma77', 'Los Angeles is soooo hot', '0', 'Tato', 'Glitch', '1', 'Aurio', 'April fools tomorrow', '4', 'nap', 'The bully**', '3', 'NICKSION', 'Oops', '6', 'stupidfuck', 'hes a little guy', '5', 'database', 'Ty', '1', 'stupidfuck', 'possible object show (objects needed)', '3', 'NightSkyMusic', 'nicotine takes 10 seconds to reach your brain', '27', 'stupidfuck', '@BFBleafyfan99 be anii e', '4', 'Odminey', 'Aliveness', '26', 'stupidfuck', '@techness2011 the', '5', 'techness2011', 'Boomerang', '1', 'saltyipaint', 'April is r slur month', '5', 'HENRYFOLIO', 'flip and dunk', '4', 'SpenceAnimation', 'Got Any grapes ', '2', 'RainyFox2', 'Draw me in your style****', '1', 'hecksacontagon', 'funky cat guy impresses you with his fish corpse', '11', 'HENRYFOLIO', 'flip and dunk @bruhkeko version', '4', 'nairb', 'Spoderman turns green', '5', 'SpenceAnimation', 'Jellybean', '1', 'SpenceAnimation', '@FussiArt', '3']
assert(len(data)%3 == 0)
scoreboard = {}
for i in range(0, len(data), 3):
if data[i] in scoreboard.keys():
scoreboard[data[i]]+= int(data[i+2])
else:
scoreboard[data[i]] = int(data[i+2])
print(scoreboard)
输出
{'Rhett_andEmma77': 0, 'Tato': 1, 'Aurio': 4, 'nap': 3, 'NICKSION': 6, 'stupidfuck': 17, 'database': 1, 'NightSkyMusic': 27, 'Odminey': 26, 'techness2011': 1, 'saltyipaint': 5, 'HENRYFOLIO': 8, 'SpenceAnimation': 6, 'RainyFox2': 1, 'hecksacontagon': 11, 'nairb': 5}
在每个“for”循环中,从“数据”列表中取出两项(用户和喜欢)并添加到单独的列表(记分牌)中。我正在尝试检测记分牌列表中是否已存在“用户”。如果它确实存在,那么我尝试在记分牌列表中获取“用户”之后的项目(这将是之前的“喜欢”项目)并向其中添加新的“喜欢”项目,之后我尝试设置新的“喜欢”项目到新的价值。但我无法让它工作。
def likecount():
scoreboard = []
for t in range(0,len(Data),3):
user = Data[t]
likes = Data[t + 2]
if user not in scoreboard:
scoreboard.append(user), scoreboard.append(likes)
else:
scoreboard[(scoreboard.index(user) + 1)] + likes == scoreboard[(scoreboard.index(user) + 1)]
for i in range(0,len(scoreboard),2):
print (scoreboard[i],scoreboard[i+1], end= "\n")
#数据列表:
['Rhett_andEmma77', 'Los Angeles is soooo hot', '0', 'Tato', 'Glitch', '1', 'Aurio', 'April fools tomorrow', '4', 'nap', 'The bully**', '3', 'NICKSION', 'Oops', '6', 'stupidfuck', 'hes a little guy', '5', 'database', 'Ty', '1', 'stupidfuck', 'possible object show (objects needed)', '3', 'NightSkyMusic', 'nicotine takes 10 seconds to reach your brain', '27', 'stupidfuck', '@BFBleafyfan99 be anii e', '4', 'Odminey', 'Aliveness', '26', 'stupidfuck', '@techness2011 the', '5', 'techness2011', 'Boomerang', '1', 'saltyipaint', 'April is r slur month', '5', 'HENRYFOLIO', 'flip and dunk', '4', 'SpenceAnimation', 'Got Any grapes ', '2', 'RainyFox2', 'Draw me in your style****', '1', 'hecksacontagon', 'funky cat guy impresses you with his fish corpse', '11', 'HENRYFOLIO', 'flip and dunk @bruhkeko version', '4', 'nairb', 'Spoderman turns green', '5', 'SpenceAnimation', 'Jellybean', '1', 'SpenceAnimation', '@FussiArt', '3']
字典正是您要查找的东西,而不是保留项目列表。因此,无论何时考虑用户,您都会检查 he/she 是否存在
- 如果存在,则将新的喜欢添加到旧的喜欢计数中。
- 如果没有,请创建一个具有类似计数的条目。
这是自然的解决方案。
data = ['Rhett_andEmma77', 'Los Angeles is soooo hot', '0', 'Tato', 'Glitch', '1', 'Aurio', 'April fools tomorrow', '4', 'nap', 'The bully**', '3', 'NICKSION', 'Oops', '6', 'stupidfuck', 'hes a little guy', '5', 'database', 'Ty', '1', 'stupidfuck', 'possible object show (objects needed)', '3', 'NightSkyMusic', 'nicotine takes 10 seconds to reach your brain', '27', 'stupidfuck', '@BFBleafyfan99 be anii e', '4', 'Odminey', 'Aliveness', '26', 'stupidfuck', '@techness2011 the', '5', 'techness2011', 'Boomerang', '1', 'saltyipaint', 'April is r slur month', '5', 'HENRYFOLIO', 'flip and dunk', '4', 'SpenceAnimation', 'Got Any grapes ', '2', 'RainyFox2', 'Draw me in your style****', '1', 'hecksacontagon', 'funky cat guy impresses you with his fish corpse', '11', 'HENRYFOLIO', 'flip and dunk @bruhkeko version', '4', 'nairb', 'Spoderman turns green', '5', 'SpenceAnimation', 'Jellybean', '1', 'SpenceAnimation', '@FussiArt', '3']
assert(len(data)%3 == 0)
scoreboard = {}
for i in range(0, len(data), 3):
if data[i] in scoreboard.keys():
scoreboard[data[i]]+= int(data[i+2])
else:
scoreboard[data[i]] = int(data[i+2])
print(scoreboard)
输出
{'Rhett_andEmma77': 0, 'Tato': 1, 'Aurio': 4, 'nap': 3, 'NICKSION': 6, 'stupidfuck': 17, 'database': 1, 'NightSkyMusic': 27, 'Odminey': 26, 'techness2011': 1, 'saltyipaint': 5, 'HENRYFOLIO': 8, 'SpenceAnimation': 6, 'RainyFox2': 1, 'hecksacontagon': 11, 'nairb': 5}