Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python 3中删除列表中部分重复项的特定实例_Python_List_Duplicates_Match_Partial - Fatal编程技术网

在Python 3中删除列表中部分重复项的特定实例

在Python 3中删除列表中部分重复项的特定实例,python,list,duplicates,match,partial,Python,List,Duplicates,Match,Partial,我对Python比较陌生。然而,我的需求通常只涉及对严格格式化的数据文件进行简单的字符串操作。我有一个特定的情况,我在网上搜索试图解决,结果却一无所获 情况就是这样。我有一个由两部分组成的实体的简单列表,格式如下: name = ['PAUL;25', 'MARY;60', 'PAUL;40', 'NEIL;50', 'MARY;55', 'HELEN;25', ...] ans = ['MARY;60', 'PAUL;40', 'HELEN;25', 'NEIL;50, ...] max

我对Python比较陌生。然而,我的需求通常只涉及对严格格式化的数据文件进行简单的字符串操作。我有一个特定的情况,我在网上搜索试图解决,结果却一无所获

情况就是这样。我有一个由两部分组成的实体的简单列表,格式如下:

name = ['PAUL;25', 'MARY;60', 'PAUL;40', 'NEIL;50', 'MARY;55', 'HELEN;25', ...]
ans = ['MARY;60', 'PAUL;40', 'HELEN;25', 'NEIL;50, ...]

max_score = {}
for n in name:
    person, score_str = n.split(';')
    score = int(score_str)
    if person not in max_score or max_score[person] < score:
        max_score[person] = score

ans = [
  '%s;%s' % (person, score)
  for person, score in max_score.items()
]
而且,我只需要保留任何重复名称的一个实例(忽略“;”右侧的数字),只保留数字最大的条目,以及仍然附加的最高值。所以答案是这样的:

name = ['PAUL;25', 'MARY;60', 'PAUL;40', 'NEIL;50', 'MARY;55', 'HELEN;25', ...]
ans = ['MARY;60', 'PAUL;40', 'HELEN;25', 'NEIL;50, ...]

max_score = {}
for n in name:
    person, score_str = n.split(';')
    score = int(score_str)
    if person not in max_score or max_score[person] < score:
        max_score[person] = score

ans = [
  '%s;%s' % (person, score)
  for person, score in max_score.items()
]
列表中元素的顺序无关紧要,但ans列表项的格式必须保持不变

我也许能想出一个办法来暴力对付它。我看过2D列表、集合、元组等,但似乎找不到答案。名单上大约有一百万个条目,所以我需要一些高效的东西。我相信对你们中的一些人来说,这将是非常容易的

感谢您提供的任何意见

干杯


alkemyst可能最好的数据结构是一个字典,将条目拆分(并转换为整数),然后重新连接

大概是这样的:

name = ['PAUL;25', 'MARY;60', 'PAUL;40', 'NEIL;50', 'MARY;55', 'HELEN;25', ...]
ans = ['MARY;60', 'PAUL;40', 'HELEN;25', 'NEIL;50, ...]

max_score = {}
for n in name:
    person, score_str = n.split(';')
    score = int(score_str)
    if person not in max_score or max_score[person] < score:
        max_score[person] = score

ans = [
  '%s;%s' % (person, score)
  for person, score in max_score.items()
]
max_score={}
对于名称中的n:
个人,分数=n.split(“;”)
分数=整数(分数)
如果人员不在最高分数或最高分数[人员]<分数:
最高分数[人]=分数
ans=[
“%s;%s%”(人,分数)
对于个人,在max_score.items()中得分
]
对于许多函数和程序来说,这是一种相当常见的结构:首先将输入转换为内部表示形式(在本例中,拆分并转换为整数),然后执行逻辑或计算(在本例中为唯一性和最大值),然后转换为所需的输出表示形式(在本例中,字符串以
分隔;


在效率方面,此代码会先查看每个输入项一次,然后再查看每个输出项一次;不太可能有比这更好的方法(当然不是正式的,也不太可能在实践中)。所有每项操作都是固定时间和快速的。它在内存中累积中间答案(在
max_score
中),但这也是不可避免的;如果内存有问题,输入和输出可以更改为迭代器/生成器,但整个中间答案必须在
max_score
中累积,然后才能输出任何项目。

我将其理解为“为了唯一性而忽略数字,为了确定最高值和输出而保留数字”…这很有道理@sabik.没错,太好了!工作起来很有魅力。非常感谢你@sabik。