Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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改进了更大输入的列表处理_Python_Performance - Fatal编程技术网

python改进了更大输入的列表处理

python改进了更大输入的列表处理,python,performance,Python,Performance,我的代码的思想是,当同一用户使用相同的设备id进行复制时,它将更新列表(在我的情况下,创建一个新列表)并删除重复的条目。 它还将从重复条目中获取最后一个id1、id2和id3,并将它们放在新列表中的一个条目中,并使用重复的类型更新该类型 为了解释这一点,我提供了一个包含4个列表的示例(在更新列表之前和之后打印) 我的代码可以工作,但是我还有一个大约800k的列表,我试着运行代码,它运行了一个小时。我怎样才能更好地处理这个问题?(无法更改输入类型,因为这来自另一个API调用,我只能更改删除重复的代

我的代码的思想是,当同一用户使用相同的设备id进行复制时,它将更新列表(在我的情况下,创建一个新列表)并删除重复的条目。 它还将从重复条目中获取最后一个id1、id2和id3,并将它们放在新列表中的一个条目中,并使用重复的类型更新该类型

为了解释这一点,我提供了一个包含4个列表的示例(在更新列表之前和之后打印)

我的代码可以工作,但是我还有一个大约800k的列表,我试着运行代码,它运行了一个小时。我怎样才能更好地处理这个问题?(无法更改输入类型,因为这来自另一个API调用,我只能更改删除重复的代码)


如果将
dev_设置为一个集合,检查是否存在值会更快。
目前,每个值都必须遍历
dev_exist
列表中的所有值,以检查它是否已经存在。然而,检查集合中是否存在值是使用散列来完成的,速度会快得多

这将在很大一部分时间内解决问题

编辑: 在查找重复项时,也可以用dict替换列表。DICT还提供快速的
输入法

my_dict = {}
for val in my_list:
    if val[0] in my_dict:
        print(val[0], "exists already")
        # Your code for replacing an existing entry here
    else:
        my_dict[val[0]] = val[1:]

# To convert back to a list
new_list = []
for key, value in my_dict.items():
    new_list.append([key]+value)

正如其他人已经提到的,扫描列表不仅效率很低,而且是O(n),因此列表越大,查找时间越短

这里有两个列表扫描,一个是隐式的(
dev\u id in dev\u exist
),另一个是显式的(
for y in fin\u list:if dev\u id in y[0]:

解决方案是使用dict(如果插入顺序重要,则使用集合.OrderedDict
)存储重复数据消除结果,以“id”作为键,行作为值-dict键查找为0(1)(恒定时间)且非常快速。此dict还将替换
dev\u exist
列表

同样给出了示例数据,您可能希望用
if x[i]==“somestring”
替换x[i]
中的
if'somestring',这更准确(
'foobar'
中的'foo'将返回true,可能不是您想要的),并且(稍微)更快(取决于字符串的长度)


如果您不关心顺序,请查看使用集合?在某些情况下,它们比列表快得多。@KuboMD也是我的第一反应,但在这种情况下,解决方案是一个简单的dict(或者一个OrderedDict,如果顺序很重要的话)。我认为复制不会太常见。我想我们实际上并不知道。我添加了一个字典方法来解决这个问题。是的,应该使用dict O(1)。使用真实数据进行了尝试,改进了很多。谢谢
my_dict = {}
for val in my_list:
    if val[0] in my_dict:
        print(val[0], "exists already")
        # Your code for replacing an existing entry here
    else:
        my_dict[val[0]] = val[1:]

# To convert back to a list
new_list = []
for key, value in my_dict.items():
    new_list.append([key]+value)
def rm_dupl(my_list):
    results = {} # or `collections.OrderedDict`

    for row in my_list:
        prev_row = results.get(row[0])
        if prev_row:
            # if entry exist, we just update the existing entry with 
            # the value of this current row, and not creating a new entry
            # below we update with the duplication one

            val = row[2] # avoids multiple access to `row[2]`
            if val == 'Broadband':
                prev_row[2] += '_Broadband'
                prev_row[6] = row[6]
            elif val == 'IPTV':
                prev_row[2] += '_IPTV'
                prev_row[7] = row[7]
            elif val == 'Voice':
                prev_row[2] += '_Voice'
                prev_row[8] = row[8]

        else:
            # no matching row found, let's add
            # a new one
            results[dev_id] = row

    # and returns the values
    # NB in py3 you'll want `list(results.values())` instead
    return results.values()