Python 将两个输入文件传递给函数会导致错误结果

Python 将两个输入文件传递给函数会导致错误结果,python,python-2.7,random,Python,Python 2.7,Random,首先为我的英语感到抱歉。我不是以英语为母语的人。 一般来说,我尝试为每个输入文件检索可变样本大小的索引。之后,我想比较两个输入文件之间的值。因此,我有两个输入文件作为参数。第一个函数随机选择特定范围内的值。 第二个函数获取第一个函数的输出,并为每个样本计算一个索引 为此,我首先将两个输入文件作为参数传递给脚本 file_name1 = sys.argv[1] file_name2 = sys.argv[2] 我选择了每个输入文件的值并将其保存到列表中,如下所示: data1 = [2, 6,

首先为我的英语感到抱歉。我不是以英语为母语的人。 一般来说,我尝试为每个输入文件检索可变样本大小的索引。之后,我想比较两个输入文件之间的值。因此,我有两个输入文件作为参数。第一个函数随机选择特定范围内的值。 第二个函数获取第一个函数的输出,并为每个样本计算一个索引

为此,我首先将两个输入文件作为参数传递给脚本

file_name1 = sys.argv[1]
file_name2 = sys.argv[2]
我选择了每个输入文件的值并将其保存到列表中,如下所示:

data1 = [2, 6, 4, 8, 9, 8, 6, 6, 6,, 7, 7, 4, 2, 2, 2, ......] #sample size 835
data2 = [7, 7, 5, 3,4, 2, 8, 6, 5, 1, 1, 9, 7 ......] #sample size 2010
我编写了一个函数,它从列表中随机选取一定范围(0、200、400、n)内的数字。 之后,我将相同的值分组并将值和值的数量作为键值保存在字典中

def subsamples(list_object):

   val = np.array(list_object)
   n = len(val)
   count = 0
   while (count < n ):
       count += 200
     if (count > n):
        break
     subsample = np.random.choice(val, count, replace=False)
     unique, counts = np.unique(subsample, return_counts=True)
     group_cat = dict(zip(unique, counts))
     pois_group.append(group_cat)

     return pois_group
但是当我为每个输入文件调用函数时,我得到了以下输出。它正确输出第一个输入,但第二个输出打印第一个输入,然后打印自己的输入, 有人知道我做错了什么吗

Input1 has 835 pois
Index: 37.720000 , sample size: 200
Index: 43.590000 , sample size: 400
Index: 46.010000 , sample size: 600
Index: 48.770000 , sample size: 800
---------------------------
Input1 has 2010 pois
Index: 37.720000 , sample size: 200
Index: 43.590000 , sample size: 400
Index: 46.010000 , sample size: 600
Index: 48.770000 , sample size: 800
Index: 22.610000 , sample size: 200
Index: 21.110000 , sample size: 400
Index: 25.920000 , sample size: 600
Index: 27.670000 , sample size: 800
Index: 28.630000 , sample size: 1000
Index: 28.110000 , sample size: 1200
Index: 28.380000 , sample size: 1400
Index: 28.610000 , sample size: 1600
Index: 28.910000 , sample size: 1800
Index: 29.120000 , sample size: 2000

有人知道我做错了什么吗?

您没有在此处显示整个文件,但我很确定您已将
pois_group
定义为文件级别的列表。每次调用
子样本时,它都会扩展该列表。您应该在
子样本
函数的设置中添加
pois_group=[]

您能修复您的问题吗?根据您所拥有的内容,代码可以有不同的含义。
Input1 has 835 pois
Index: 37.720000 , sample size: 200
Index: 43.590000 , sample size: 400
Index: 46.010000 , sample size: 600
Index: 48.770000 , sample size: 800
---------------------------
Input1 has 2010 pois
Index: 37.720000 , sample size: 200
Index: 43.590000 , sample size: 400
Index: 46.010000 , sample size: 600
Index: 48.770000 , sample size: 800
Index: 22.610000 , sample size: 200
Index: 21.110000 , sample size: 400
Index: 25.920000 , sample size: 600
Index: 27.670000 , sample size: 800
Index: 28.630000 , sample size: 1000
Index: 28.110000 , sample size: 1200
Index: 28.380000 , sample size: 1400
Index: 28.610000 , sample size: 1600
Index: 28.910000 , sample size: 1800
Index: 29.120000 , sample size: 2000