1

我保证我已经尝试过搜索,但我发现的每一个问题最终都有一些未说明或违反的标准,这使得答案对我来说不够。

我正在向 Python 脚本发送一个列表。该列表将存储在某个地方,但我想尽量减少写入(这是在远程服务上,每次写入都要收费)。

listNew = ["some", "list", "sent", "in", "that", "may", "be", "different", "later", "some"]
listPrevious = ["some", "some", "list", "that", "was", "saved", "previously"]

(请不要因为它们是字符串而分心;我的列表实际上包含整数。)

简单的基本算法是逐个索引地迭代两个列表。如果项目相同,我不需要写;繁荣,省钱。然而,最终保存的数据应该是 listNew。

在其他语言中,我可以通过索引直接引用元素。

for (int i = 0; i < listNew.length; i++) {
    // Have we exceeded the previous list's length? Time to just write data in.
    if (listPrevious[i] == null)
        listPrevious.append(listNew[i]);
        continue;

    if (listNew[i] != listPrevious[i])
        listPrevious[i] = listNew[i]
}

不幸的是,我在循环技术列表方法中发现的并没有提供:

  1. 通过索引获取元素而不删除它的方法(pop 方法),也不是

  2. 通过精确值和定位获取元素索引的方法,因为我有重复项(在上面的代码中,使用 list.index("some") 将返回 listPrevious 中的第一个索引,尽管我实际上正在查看最后一个索引listNew 中的元素),也不

  3. 遍历我的列表超出列表之一长度的方法(似乎 zip() 不会迭代超出较小列表的长度)。

关于我应该如何处理这个问题的任何想法?当我搜索以前的问题时,总是以某种方式违反了这三个标准之一。

顺便说一句,我试图避免像下面这样的解决方案,这也是其他问题中标记的解决方案之一。

for newitem in listNew
    for olditem in listPrevious
        if newitem != olditem
            # save the newitem

这会将 listNew 中的元素与 listPrevious 中的每个元素进行比较,这是低效的。我只需要知道它是否匹配另一个列表中的相同索引。

-------- 通过评论请求

输入:2 个列表,listNew 和 listPrevious。另一个例子

  • listNew = [100, 500, 200, 200, 100, 50, 700]
  • listPrevious = [100, 500, 200, 400, 400, 50]

输出:listPrevious 现在是 listNew,而不必覆盖相同的元素。

listPrevious = [100, 500, 200, 200, 100, 50, 700]

  • 不需要写入:[100, 500, 200, _,, 50, _ _] <- 保存 4 次写入

  • 确实需要写:[ _,, _ _, 200, 100, __, 700] <- 执行了 3 次写入,而不是执行了 .length 次写入!

4

4 回答 4

3

从您的 C 代码中,我创建了以下内容。希望它做你想要的:

for i in range(len(listNew)):
    # Have we exceeded the previous list's length? Time to just write data in.
    if i >= len(listPrevious):
        listPrevious.append(listNew[i])
        continue

    if listNew[i] != listPrevious[i]:
        listPrevious[i] = listNew[i]
于 2012-10-09T22:20:35.507 回答
2

如果你想用你需要的索引按顺序迭代enumerate

for idx, item in enumerate(mylist):
  # idx is the 0-indexed value where item resides in mylist.

如果你想在 python 中迭代成对的东西,你可以使用zip

for a, b in zip(newlist, oldlist):
  # items a and b reside at the same index in their respective parent lists.

您可以结合以下方法:

for idx, (a, b) in enumerate(zip(newlist, oldlist)):
  # here you have everything you probably need, based on what I can 
  # tell from your question.

根据您的数据集,您还可以查看itertools模块中的附加功能,特别是izip_longest.

于 2012-10-09T22:23:45.650 回答
1

Python 的列表方法实际上确实提供了您认为它没有提供的所有功能(最后一个代码示例等同于您的示例代码)

  1. 通过索引获取元素而不删除它的方法(pop 方法)

    >>> data = ['a', 'b', 'c']
    >>> data[1]        # accessing an element by index
    'b'
    
  2. 通过精确值和定位获取元素索引的方法,因为我有重复项(在上面的代码中,使用 list.index("some") 将返回 listPrevious 中的第一个索引,尽管我实际上正在查看最后一个索引listNew 中的元素)

    >>> data = ['a', 'b', 'c', 'b', 'a']
    >>> data.index('a')     # without a start arg, call finds the first index
    0
    >>> data.index('a', 1)  # you can find later indices by giving a start index
    4
    
  3. 遍历我的列表超出列表之一长度的方法(似乎 zip() 不会迭代超出较小列表的长度)。

    for i, item in enumerate(listNew):    # loops over indices and values
        if i >= len(listPrevious):
            listPrevious.append(item)
            continue
    
        if item != listPrevious[i]:
            listPrevious[i] = item
    
于 2012-10-09T22:26:49.237 回答
0

物品的位置重要吗?

如果不是简单地这样做::

for n in NewList:
    if n not in OldList:
        OldList.append(n)
        process(n)
于 2012-10-09T22:25:25.227 回答