【问题标题】:Python3 search value in this arrayPython3 在这个数组中搜索值
【发布时间】:2015-06-25 03:56:38
【问题描述】:

我仍在学习python3,请就此提出建议。 我有一个很长的数组,如下所示,我如何检查任何数组元素中是否存在两个这个值(日期在第 4 个位置,字符串在数组的第二个位置)。

数组:

[
('1','200','300','500','2015-04-25 7:00:00'),
('1','200','500','500','2015-04-26 8:00:00'),
('1','200','500','500','2015-04-26 8:00:00'), # Repeated
('1','200','900','500','2015-04-27 9:00:00'),
('1','200','300','500','2015-04-28 17:00:00'),
('1','200','300','500','2015-04-28 17:00:00'), # Repeated
...
...
]

【问题讨论】:

标签: python-3.x


【解决方案1】:

一些不需要使用外部库的方法是:

long_array = [
    ('1','200','300','500','2015-04-25 7:00:00'),
    ('1','200','500','500','2015-04-26 8:00:00'),
    ('1','200','500','500','2015-04-26 8:00:00'), # Repeated
    ('1','200','900','500','2015-04-27 9:00:00'),
    ('1','200','300','500','2015-04-28 17:00:00'),
    ('1','200','300','500','2015-04-28 17:00:00'), # Repeated
    # ...
]

使用一组..

values = set()
for entry in long_array:    
    value = (entry[1], entry[4])
    if (value in values): 
        print("Duplicate " + str(entry))
    else:
        values.add(value)

或使用收集计数器..

from collections import Counter

values = Counter([(entry[1], entry[4]) for entry in long_array])
for value, count in values.items():
    if count > 1:
        print(str(count) + " duplicates of " + str(value))

数组的大小在这里非常重要。这些可能会导致非常大的数组出现问题。

【讨论】:

  • 我喜欢你的一套解决方案,因为它不需要任何库插件,并且不时可移植。谢谢。
  • 我认为 set 实现也更好。计数器实现会遍历所有值两次,设置实现只遍历一次。如果大小太大,我将建议仅将哈希值存储在集合中。但这会导致可能的哈希冲突的潜在问题(也许,如果你正在做大量的冲突)。计数器是很好,如果你已经有了清单。所以它们都可以不时派上用场。
【解决方案2】:

我建议使用熊猫。假设您的数组(实际上在 Python 中称为 list)称为 A,您可以加载它

import pandas as pd
df = pd.DataFrame(A)
df
   0    1    2    3                    4
0  1  200  300  500   2015-04-25 7:00:00
1  1  200  500  500   2015-04-26 8:00:00
2  1  200  500  500   2015-04-26 8:00:00
3  1  200  900  500   2015-04-27 9:00:00
4  1  200  300  500  2015-04-28 17:00:00

然后你可以像这样得到重复的行

df['Repeated'] = df.duplicated(subset=[3,4])
df

Out[463]: 
   0    1    2    3                    4 Repeated
0  1  200  300  500   2015-04-25 7:00:00    False
1  1  200  500  500   2015-04-26 8:00:00    False
2  1  200  500  500   2015-04-26 8:00:00     True
3  1  200  900  500   2015-04-27 9:00:00    False
4  1  200  300  500  2015-04-28 17:00:00    False

【讨论】:

    【解决方案3】:

    如果您想在 Python 中实际编写解决方案以进行实践,这里有一种方法:

    # the indices in the tuples to be used as keys for determining repeats
    # set this to whatever indices you would like (or even all of them)!
    key_indices = [1, 4]
    
    # for a given tuple tpl, construct a key consisting of the values in tpl
    # that are found at the indices given in ki
    def make_key(tpl, ki):
        key_elements = []
        for i in ki:
            key_elements.append(tpl[i])
    
        # need to return a tuple, as you cannot use a list as a key for a dict
        return tuple(key_elements)
    
    data = [
    ('1','200','300','500','2015-04-25 7:00:00'),
    ('1','200','500','500','2015-04-26 8:00:00'),
    ('1','200','500','500','2015-04-26 8:00:00'), # Repeated
    ('1','200','900','500','2015-04-27 9:00:00'),
    ('1','200','300','500','2015-04-28 17:00:00'),
    ('1','200','300','500','2015-04-28 17:00:00') # Repeated
    ]
    
    # the data structure that we'll use to remember where we've seen keys before
    memory = dict()
    duplicates = set()
    
    for i in range(0, len(data)):
        # make the key for comparison
        k = make_key(data[i], key_indices)
    
        # find out where we've seen this before
        # if nowhere else, return an empty list
        previous_locations = memory.get(k, [])
    
        # note that we have now seen this key at location i
        previous_locations.append(i)
    
        if (len(previous_locations) > 1):
            duplicates.add(i)
    
        # update the dict with the new location
        memory[k] = previous_locations
    
    print("Duplicate values found at: {}".format(list(duplicates)))
    
    
    # and if you want to know which keys were duplicated where?
    for k in memory.keys():
        locs = memory[k]
        if len(locs) > 1:
            print("{}: {}".format(k, locs))
    

    输出:

    Duplicate values found at: [2, 5]
    ('200', '2015-04-28 17:00:00'): [4, 5]
    ('200', '2015-04-26 8:00:00'): [1, 2]
    

    【讨论】:

      猜你喜欢
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多