[英]Remove item from one list if NOT in another list?
This problem sounds very simple and probably has a very easy solution.这个问题听起来很简单,而且可能有一个非常简单的解决方案。 However, nothing I have tried works.
但是,我没有尝试过。
The file I am importing, 'bedwars.csv', has 29 rows.我正在导入的文件“bedwars.csv”有 29 行。 Each (comma-separated) row consists of a player and their point value: 'Player, Points'.
每行(以逗号分隔)由一名球员及其得分组成:“球员,得分”。
I have a list of 16 players, 'samplePlayers.csv', all of whom are also in 'bedwars.csv'.我有一个包含 16 名玩家的列表,“samplePlayers.csv”,他们都在“bedwars.csv”中。 I am trying to remove the players that are NOT in 'sampleplayers.csv' from 'bedwars.csv'.
我正在尝试从“bedwars.csv”中删除不在“sampleplayers.csv”中的玩家。 FWIW the players in 'samplePlayers.csv' ae just the first 16 players in 'bedwars.csv'.
FWIW 'samplePlayers.csv' 中的玩家只是 'bedwars.csv' 中的前 16 名玩家。
My attempt is below.我的尝试如下。
import pandas as pd
playing = pd.read_csv('samplePlayers.csv') # test data
# imports all past game data
bedwars = pd.read_csv('bedwars.csv')
bedwars.columns = ['Players', 'Points']
players = list(bedwars.Players)
points = list(bedwars.Points)
bedwarsPoints = list(zip(players, points))
print(bedwarsPoints)
for i in bedwarsPoints:
if i not in playing:
bedwarsPoints.remove(i)
print(bedwarsPoints)
Outputs the following:输出如下:
[('Star_Dragon4', 40.88), ('LethalPilot', 32.18625), ('macrocosms', 27.4), ('mrjad333', 25.82142857), ('IceKing12323', 22.51666667), ('AllieThaDon', 20.568), ('SuperToad916', 18.675), ('odsstel', 18.04285714), ('NautilusReign', 17.6), ('14ham', 16.39), ('Hunter13004', 15.72), ('sapphronn', 15.44), ('SerRonanIrwin', 14.23333333), ('LightningLucario', 14.062), ('Dispuesto', 13.565), ('KirynMissy', 13.12285714), ('Abach6', 12.68857143), ('lurchlurger', 11.78), ('Labmonjo1210', 10.78), ('humilau', 9.65125), ('BYTECH', 9.57), ('de_eh', 9.3), ('cowDude12345', 8.65), ('Menderlovemybug', 8.5), ('tenparks', 8.366666667), ('TheUW', 6.8), ('QuinnQuimby', 5.2), ('stealthguy11', 4.05), ('Buck274', 0.0)]
[('LethalPilot', 32.18625), ('mrjad333', 25.82142857), ('AllieThaDon', 20.568), ('odsstel', 18.04285714), ('14ham', 16.39), ('sapphronn', 15.44), ('LightningLucario', 14.062), ('KirynMissy', 13.12285714), ('lurchlurger', 11.78), ('humilau', 9.65125), ('de_eh', 9.3), ('Menderlovemybug', 8.5), ('TheUW', 6.8), ('stealthguy11', 4.05)]
The first being an array of length 29 (good), the array from 'samplePlayers.csv'.第一个是长度为 29(良好)的数组,来自“samplePlayers.csv”的数组。 The second, however, consists of the mangled remnants of the attempted removal.
然而,第二个是由试图移除的损坏残余物组成。 I noticed that the "for" loop has only been removing every other value.
我注意到“for”循环只删除了所有其他值。
Please let me know if any more clarification is needed and I will respond.如果需要更多说明,请告诉我,我会回复。 Thank you all in advance for your help.
预先感谢大家的帮助。
Assuming playing
is a list of strings corresponding to player names:假设
playing
是玩家名字对应的字符串列表:
bedwarsPoints = [i for i in bedwarsPoints if i[0] in playing]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.