根据部分匹配python删除重复项
说我有一个列表,
var1 = ["VenueA/2003", "VenueA/2006", "VenueA/2009","VenueB/2009"]
我想做的是根据VENUEX删除列表中的所有重复元素,并保留第一次出现 在上面的示例中,有三个类似的venuea
是venuea/2003
,venuea/2006
和venuea/venuea/2009
。由于venuea/2003
是第一次出现,我想保留并删除
我想要的结果是
var1 = ["VenueA/2003","VenueB/2009"]
我该怎么做?
Say I have a list
var1 = ["VenueA/2003", "VenueA/2006", "VenueA/2009","VenueB/2009"]
What I want to do is remove all duplicate elements in the list based on the VenueX and keep the first occurrence
In the above example, there are three similar VenueA
which are VenueA/2003
, VenueA/2006
and VenueA/2009
. As VenueA/2003
is the first occurrence, I want to keep that and remove the rest of VenueA
The result that I want is
var1 = ["VenueA/2003","VenueB/2009"]
How do I go about doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以构建一个由前缀键入的地图,并具有值,其中一个具有此前缀的字符串。
地图
构造函数可用于此。由于映射构造函数将保留具有给定前缀的字符串的最后一个 em> constrorce,因此您应该逆转输入和输出,以获得 first 匹配:
You can build a map keyed by the prefixes, and with as value, one of the strings that has this prefix. The
Map
constructor can be used for this.As the Map constructor will retain the last occurrence of the string that has a given prefix, you should reverse the input and the output to get the first match instead:
同样,如果您有更多
场地
,则可以增加LEN的价值并将其附加到k
Similarly, if you have more
Venues
you can increase the value of len and append them tok