如何使用输入 csv 文件索引和自动处理列表

发布于 2024-10-20 18:51:11 字数 4771 浏览 0 评论 0 原文

我有 csv 输入文件,我们将其命名为 heights.csv。我的示例文件只有四行,但不能限制。 第一列仅包含不相关的名称,第二列包含我想要作为整数处理的值。

1000,57020,
2003,56095,
2007,55964,
3001,57020,

我想执行这样的操作:

first_read_a = 第一行的值+first random.randint(500, 1000)
Second_read_a = 第一行的值 + first_read - 下一行的值
Third_read_a = 第一行的值 + random.randint(500, 1000) - 下一行的值
Fourth_read_a = 第一行的值 + random.randint(500, 1000) - 下一行的值

依此类推,如果列表会更长,直到最后一行,没有下一行,也没有要减去的下一个值。
该过程的结果必须以某种方式存储和索引,因为下一个任务
是使用生成的值向后执行类似的操作。

first_read_b = 最后一行的值 + Fourth_read_a - 紧邻最后一行的值

依此类推,直到这次从列表开头算起的第二行,而不是第一行。
我有示例代码,但它非常有限,我想向您询问有效的解决方案的示例。

import random
a = [57020, 56095, 55964, 57020]

compute = input(">")

if compute == 1: 
    read_0a = random.randint(500,1000)                
    read_1a = a[0] + read_0a - a[1]                 
    read_2a = a[1] + random.randint(500,2300) - a[2]
    read_3a = a[2] + random.randint(500,2300) - a[3]

    read_1b = a[3] + read_3a - a[2]              
    read_2b = a[2] + read_2a - a[1]                


    print read_0a, read_1a, read_2b, read_2a, read_1b, read_3a
    print read_1a + read_2a + read_3a
    print read_0a + read_1b + read_2b

协议是将脚本开发为这种形式,这将允许处理输入 *.csv 文件并以我的示例脚本中完成的方式生成合适的整数,但不需要定义列表 [示例中的“a”列表]手动并为每个“读取”添加指令如何生成。我想知道如何索引 csv 文件以及如何以整数而不是字符串的形式访问索引项以及如何通过索引触发这些整数。
----------- -------------------------------------------------- --
让我解释一下。
我们有 eyquem 的高级脚本:

import csv
import random

将 open('heights.csv','rb') 作为 f: a = [int(row[1]) for row in csv.reader(f)] 打印 'a==',a

计算 = 输入(">")

如果计算 == 1: read_a = dict((i+1,a[i] + random.randint(500,2300) - a[i+1]) 对于 xrange(0,len(a)-1) 中的 i

read_a[0] = read_a[1] - a[0] + a[1]

read_b = dict( (i, a[len(a)-1] + read_a[len(a)-i] - a[len(a)-i-1])
               for i in xrange(1,len(a)-1))
print read_a, read_b

and we get the example result:

这是输入列表:a== [57020, 56095, 55964, 57020]
并生成“读取”:
<代码>{0: 1637、1: 2562、2: 1991、3: 4} {1: 1060、2: 2916}

让我们检查一下:
57020 + 1637 - 2562 = 56095 [正确!,因为它等于列表中的第二个值]
所以让我们取列表中的第二个值并继续:
56095 + 1991 - 1060 = 57026 [错误,因为它不等于列表中的第三个值]
所以现在剩下的将给我们错误的结果。

正确的依赖关系如下:
让我使用我发布的第一个脚本生成的“读取”示例值向您展示我想要的结果:
a = [57020, 56095, 55964, 57020]
57020 #列表中的第一个值
+712 #read_0a = random.randint(500,1000)
-1637 #read_1a = 1st + read_0a - 2nd
[=]56095 #列表中的第二个值
+772 #read_2b = 3rd + read_2a - 2nd
-903 #read_2a = 2nd + random.randint(500,2300) - 3rd
[=]55964 #列表中的第三个值
+1279 #read_1b = 4th] + read_3a - 3rd
-223 #read_3a = 3rd + random.randint(500,2300) - 4th
[=]5720 #列表中的第四个值

就是这样。
当您从列表中的第一个值开始运行并按照我上面显示的方式执行生成的“读取”的连续值时,您将获得最后一个值,在我们的示例中是第四个值,获得第二个和第三个值。

现在看一下生成的读取的最终检查:

check_a = read_1a + read_2a + read_3a
check_b = read_0a + read_1b + read_2b

...并且您会看到check_a 中的值之和 = check_b 中的值之和

仅当第一行和最后一行具有相同的值时,上述检查才是权威的 - 在示例文件中为 57020。
如果第一行和最后一行具有不同的值,则检查必须如下所示:
first_line_value - last_line_value=(read_0a + read_1b + read_2b) - (read_1a + read_2a + read_3a)。< br> 因此,我们可以用更恰当的方式说:
first_line_value - last_line_value = read_backward 之和 - read_onward 之和
我希望它能帮助您了解必须实现的目标。
并感谢您的耐心等待。

------------- ------------------------------------------------

eyquem 的脚本效果非常好!
并且完全实现了我此时此刻但在结束时给出的指示我的问题我希望您考虑一些额外的事情,我希望这些事情不需要重写整个代码

1]第一个是输入“heights.csv”文件。
程序是否可能接受带小数点“.”的值
但在计算过程中忽略它? 输入 csv 文件将如下所示:
1000,57.020,
2003,56.095,
2007,55.964,
3001,57.020,

但是为了计算“读取”,将使用如下整数:57020, 56095, 55964, 57020

2]接下来是自动重新计算能力。
有时,在计算过程中,我们的“读数”显示为带有负号的负值,例如:
a== [57020, 56095, 55964, 57020]
read_a== {1: 1984, 2: 2128, 3: -452}
read_b== {1: 604, 2: 1997, 3: 1059}
在此结果中我们看到'-452'
在这种情况下,脚本应该重新计算,直到“读取”中没有负值

3] 最后一个是输出文件,例如“output.txt” 是否可以生成包含生成的“读取”的文件?
模板将如下所示:
[1] # 方括号中的订单号
1=1000 # 取自输入文件第一列的名称,前面始终带有 '1='
4=2118 # 生成“read”,前面总是带有“4=”

[2]
1=2003
4=3043

[3]
1=
4=2054

[4]
1=2007
4=2185

[5]
1=
4=2263

[6]
1=3001
4=1207

正如您所看到的,从输入文件的第一列中获取的名称,始终以“1=”开头,必须出现在第一个和最后一个“读取”中,然后仅出现在所有“向后读取”中

I have csv input file, lets call it heights.csv. my example file has only four lines but it cannot be limited.
The first column contains just irrelevant name, the second one contains values I want to process as integers.

1000,57020,
2003,56095,
2007,55964,
3001,57020,

I would like to perform action like this:

first_read_a = value of the first line + first random.randint(500, 1000)
second_read_a = value of the first line + first_read - value of the next line
third_read_a = value of the first line + random.randint(500, 1000) - value of the next line
fourth_read_a = value of the first line + random.randint(500, 1000) - value of the next line

and so on if the list would be longer until the last line where there's no next line and no next value to subtract.
results of the process must be stored and indexed somehow because the next task
is to perform similar action backwards, using generated values.

first_read_b = value of the last line + fourth_read_a - next to last line value

and so on until the second line from the beginning of the list this time, not the first one.
i have sample code but it is very limited and I'd like to ask You for example of efficient solution.

import random
a = [57020, 56095, 55964, 57020]

compute = input(">")

if compute == 1: 
    read_0a = random.randint(500,1000)                
    read_1a = a[0] + read_0a - a[1]                 
    read_2a = a[1] + random.randint(500,2300) - a[2]
    read_3a = a[2] + random.randint(500,2300) - a[3]

    read_1b = a[3] + read_3a - a[2]              
    read_2b = a[2] + read_2a - a[1]                


    print read_0a, read_1a, read_2b, read_2a, read_1b, read_3a
    print read_1a + read_2a + read_3a
    print read_0a + read_1b + read_2b

The deal is to develope the script to such form, which would let to process input *.csv file and generate suitable integers in the way it is done in my example script but without need to define the list ['a' list in example] by hand and to put instruction for every single 'read' how it has to be generated. I would like to know, how to index a csv file and how can I access indexed items as integers, not strings and how to trigger those integers by index.
---------------------------------------------------------------
Let me explain more.
We have eyquem's advanced script:

import csv
import random

with open('heights.csv','rb') as f: a = [int(row[1]) for row in csv.reader(f)] print 'a==',a

compute = input(">")

if compute == 1: read_a = dict((i+1,a[i] + random.randint(500,2300) - a[i+1]) for i in xrange(0,len(a)-1) )

read_a[0] = read_a[1] - a[0] + a[1]

read_b = dict( (i, a[len(a)-1] + read_a[len(a)-i] - a[len(a)-i-1])
               for i in xrange(1,len(a)-1))
print read_a, read_b


and we get the example result:

this is input list: a== [57020, 56095, 55964, 57020]

and generated 'reads':
{0: 1637, 1: 2562, 2: 1991, 3: 4} {1: 1060, 2: 2916}


Let's check:
57020 + 1637 - 2562 = 56095 [correct!, because it is equal to the second value on the list]
So let's take the second value on the list and go forth:
56095 + 1991 - 1060 = 57026 [wrong, because it is not equal to the third value on the list]
so now the rest is gonna give us wrong results.

The the right relationship of dependence is as following:
Let me show You my desired result, using example values of 'reads' generated with my [primitive]first script posted:
a = [57020, 56095, 55964, 57020]
57020 #the 1st value on the list
+712 #read_0a = random.randint(500,1000)
-1637 #read_1a = 1st + read_0a - 2nd
[=]56095 #the 2nd value on the list
+772 #read_2b = 3rd + read_2a - 2nd
-903 #read_2a = 2nd + random.randint(500,2300) - 3rd
[=]55964 #the 3rd value on the list
+1279 #read_1b = 4th] + read_3a - 3rd
-223 #read_3a = 3rd + random.randint(500,2300) - 4th
[=]5720 #the 4th value on the list

And that's it.
When you run from the 1st value on the list and will do + and - succesive values of generated 'reads' in the way I show above, You will get the last value, in our example the 4th one, gaining on the way the 2nd and the 3rd value.

Now take a look at the final check of generated reads:

check_a = read_1a + read_2a + read_3a
check_b = read_0a + read_1b + read_2b

... and You'll see that sum of values in check_a = sum of values in check_b

The above check is authoritative only if the first line and the last line has the same value - in example file it is 57020.
If the first line and the last line has different value the check has to be like this:
first_line_value - last_line_value=(read_0a + read_1b + read_2b) - (read_1a + read_2a + read_3a).
So we can say in more adequate way that:
first_line_value - last_line_value = sum of reads_backward - sum of reads_onward
I hope it'll help You to understand what it has to be achieved.
and thank You for Your patience.

---------------------------------------------------------------

eyquem's script works really great!
and completely implemets my instruction that has been given to this moment but at the end of my question I would like You to consider several extra things, that, I hope, will not require to rewrite the whole code

1]The first one is the input, 'heights.csv' file.
Is it possible that the program would accept values with decimal point '.'
but just ignore it during one's computations??
The input csv file would be like this:
1000,57.020,
2003,56.095,
2007,55.964,
3001,57.020,


but nevertheless to compute 'reads',the integers like: 57020, 56095, 55964, 57020 would be used

2]The next thing is auto recomputation ability.
Sometimes, during the computation our 'reads' appear as negative values with minus, for example:
a== [57020, 56095, 55964, 57020]
read_a== {1: 1984, 2: 2128, 3: -452}
read_b== {1: 604, 2: 1997, 3: 1059}

in this result we see '-452'
In this case the script should just recompute until there's no negative value among 'reads'

3] The last thing is an output file, for example 'output.txt'
Is it possible to generate such file containg generated 'reads'?
The template would be like this:
[1] # order number in square bracket
1=1000 # name taken from the first column of input file, always preceded with '1='
4=2118 # generated 'read', always preceded with '4='

[2]
1=2003
4=3043

[3]
1=
4=2054

[4]
1=2007
4=2185

[5]
1=
4=2263

[6]
1=3001
4=1207

As You see the name taken from the first column of input file, always preceded with '1=' has to appear for the first and the last 'read' and then only for all the 'backward reads'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

允世 2024-10-27 18:51:11

好吧,我希望你能成功解决你的问题。

您是否找到了如何使用值中的 '.' 进行管理?这很简单:

replace('.','')

对我来说,我认为以下代码满足所有要求。我希望这不是作业,你学到了一些东西。

import csv
import random
from sys import exit

with open('heights.csv','rb') as f:
    names,a = {},{}
    for k,row in enumerate(csv.reader(f)):
        names[k] = row[0]
        a[k]     = int(row[1].replace('.',''))
    print names
    print 'a==',a
    L = len(a)

compute = input(">")

if compute == 1:

    if a[1] - a[0] <= 1000 and all(a[i] - a[i-1] <= 2300 for i in xrange(2,L)):

        read_b = {L-1 : random.randint( max(500,a[1]-a[0]), 1000 ) }
        read_a = {1   : a[0] + read_b[L-1] - a[1] }

        for i in xrange(2,L):
            read_b[L-i] = random.randint( max(500,a[i]-a[i-1]), 2300 )
            read_a[i] = a[i-1] + read_b[L-i] - a[i]

    else:
        exit("The file can't be treated because of the relative values of "
             "the following couples of lines : "+\
             ('(0,1) ' if 1000 < a[1] - a[0] else "")+\
             ' '.join(repr((i-1,i)) for i in xrange(2,L) if 2300 < a[i] - a[i-1]))



    with open('output.txt','w') as fw:
        fw.write('[1]\n1={}\n4={}\n\n[2]\n1={}\n4={}\n\n'.\
                 format(names[0],read_b[L-1],names[1],read_a[1]))

        fw.writelines('[{}]\n1=\n4={}\n\n[{}]\n1={}\n4={}\n\n'.\
                      format(str(2*k-1),read_b[L-k],str(2*k),names[k],read_a[k])
                      for k in xrange(2,L))                           



    # display of the execution

    print 'read_a== %s\nread_b== %s\n\n' % (read_a,read_b)+\
          ''+\
          ('\n'.join(('  a[%s] ========== %s ******' % (y,a[y])+\
                      '\n    +read_b[%s] == %s' % (str(L-y-1), read_b[L-y-1])+\
                      '\n    -read_a[%s] == %s' % (str(y+1)  ,-read_a[y+1]  ) )
                     for y in xrange(L-1)) )+\
          '\n  a[%s] ========== %s ******\n\n' % (L-1,a[L-1])+\
          ''+\
          ('\n'.join(('  a[%s] ========== %s ******' % (y,a[y])+\
                      '\n    +read_a[%s] == %s' % (y  , read_a[y]  )+\
                      '\n    -read_b[%s] == %s' % (L-y,-read_b[L-y]) )
                     for y in xrange(L-1,0,-1)) )+\
          '\n  a[%s] ========== %s ******\n\n' % (0,a[0])+\
          ''+\
          'sum(read_a.values())== %s\nsum(read_b.values())== %s' %\
          (sum(read_a.values()),sum(read_b.values()))

解释:

read_b 变量的值由 randint() 创建。如果每个 randint() 中的边界被视为不可触及的约束,则正性条件因此取决于 read_a 变量的值。

您写道:

read_0a = random.randint(500,1000)  
read_1a = a[0] + read_0a - a[1]

也就是说

read_a[0] = random.randint(500,1000)  
read_a[1] = a[0] + read_a[0] - a[1]
# nota bene: read_a[0] is in fact read_b[len(a)-1]

,仅当 0 <= a[0] + read_a[0] - a[1]< 时才验证条件 0 <= read_a[1] /strong>

也就是说 if a[1] - a[0] <= read_a[0] (E)

那么,有 3 种情况:

  • if a[ 1] - a[0] <= 500 为 True,则 (E) 对于由 read_a[0] = random 定义的任何值 read_a[0] 始终为 True .randint(500,1000)

  • if 500 <= a[1] - a[ 0] <= 1000 为 True ,则 (E) 仅对于由 read_a[0] = random.randint(a[1) 定义的值 read_a[0] 为 True ] - a[0] ,1000)

  • if <强>1000 < a[1] - a[0] 为 True ,不可能找到 500 到 1000 之间的值作为 read_a[0] 的值,因此 read_a[0] 的值>read_a[1] 将为正值。

我们可能会想重申一下指令 read_a[0] = random.randint(500,1000) ,也就是说 read_b[len(a)-1] = random.randint(500, 1000) 直到 read_a[0] 的值符合条件 read_a[1] >= 0

但如果1000 < a[1] - a[0] ,会出现死循环。这意味着文件中的值不能是没有条件的任何整数:它们必须验证某些条件,以便所有 read_a 值均为正数。并且我把这个条件的验证放到了代码中。

对于带有边界的 read_a 中的其他值也是如此

randint(500,2300)

Well, I hope you worked with success on your problem.

Did you find how to manage with '.' in the values ? That was simple:

replace('.','')

For me, I think the following code fulfills all the requirements. I hope it wasn't a homework and you learned something.

import csv
import random
from sys import exit

with open('heights.csv','rb') as f:
    names,a = {},{}
    for k,row in enumerate(csv.reader(f)):
        names[k] = row[0]
        a[k]     = int(row[1].replace('.',''))
    print names
    print 'a==',a
    L = len(a)

compute = input(">")

if compute == 1:

    if a[1] - a[0] <= 1000 and all(a[i] - a[i-1] <= 2300 for i in xrange(2,L)):

        read_b = {L-1 : random.randint( max(500,a[1]-a[0]), 1000 ) }
        read_a = {1   : a[0] + read_b[L-1] - a[1] }

        for i in xrange(2,L):
            read_b[L-i] = random.randint( max(500,a[i]-a[i-1]), 2300 )
            read_a[i] = a[i-1] + read_b[L-i] - a[i]

    else:
        exit("The file can't be treated because of the relative values of "
             "the following couples of lines : "+\
             ('(0,1) ' if 1000 < a[1] - a[0] else "")+\
             ' '.join(repr((i-1,i)) for i in xrange(2,L) if 2300 < a[i] - a[i-1]))



    with open('output.txt','w') as fw:
        fw.write('[1]\n1={}\n4={}\n\n[2]\n1={}\n4={}\n\n'.\
                 format(names[0],read_b[L-1],names[1],read_a[1]))

        fw.writelines('[{}]\n1=\n4={}\n\n[{}]\n1={}\n4={}\n\n'.\
                      format(str(2*k-1),read_b[L-k],str(2*k),names[k],read_a[k])
                      for k in xrange(2,L))                           



    # display of the execution

    print 'read_a== %s\nread_b== %s\n\n' % (read_a,read_b)+\
          ''+\
          ('\n'.join(('  a[%s] ========== %s ******' % (y,a[y])+\
                      '\n    +read_b[%s] == %s' % (str(L-y-1), read_b[L-y-1])+\
                      '\n    -read_a[%s] == %s' % (str(y+1)  ,-read_a[y+1]  ) )
                     for y in xrange(L-1)) )+\
          '\n  a[%s] ========== %s ******\n\n' % (L-1,a[L-1])+\
          ''+\
          ('\n'.join(('  a[%s] ========== %s ******' % (y,a[y])+\
                      '\n    +read_a[%s] == %s' % (y  , read_a[y]  )+\
                      '\n    -read_b[%s] == %s' % (L-y,-read_b[L-y]) )
                     for y in xrange(L-1,0,-1)) )+\
          '\n  a[%s] ========== %s ******\n\n' % (0,a[0])+\
          ''+\
          'sum(read_a.values())== %s\nsum(read_b.values())== %s' %\
          (sum(read_a.values()),sum(read_b.values()))

.

Explanation:

.

The values of read_b variables are created by randint() . If the bounds in each randint() are considered untouchable constraints, the condition of positiveness is consequently on the values of the read_a variables.

You wrote :

read_0a = random.randint(500,1000)  
read_1a = a[0] + read_0a - a[1]

that is to say

read_a[0] = random.randint(500,1000)  
read_a[1] = a[0] + read_a[0] - a[1]
# nota bene: read_a[0] is in fact read_b[len(a)-1]

The condition 0 <= read_a[1] is verified only if 0 <= a[0] + read_a[0] - a[1]

that is to say if a[1] - a[0] <= read_a[0] (E)

So, there is 3 cases:

  • if a[1] - a[0] <= 500 is True, then (E) is always True for any value read_a[0] defined by read_a[0] = random.randint(500,1000)

  • if 500 <= a[1] - a[0] <= 1000 is True , then (E) is True only for value read_a[0] defined by read_a[0] = random.randint(a[1] - a[0] ,1000)

  • if 1000 < a[1] - a[0] is True , it is impossible to find a value between 500 and 1000 to be the value of read_a[0] so as the value of read_a[1] will be positive.

We may think to reiterate the instruction read_a[0] = random.randint(500,1000) that is to say read_b[len(a)-1] = random.randint(500,1000) until the value of read_a[0] respects the condition read_a[1] >= 0.

But if 1000 < a[1] - a[0] , there would be an endless loop. This means that the values in the file can't be any integer without condition : they must verify some conditions in order that all the read_a values be positive. And I put the verification of this condition in the code.

.

It's the same for the other values in read_a with bounds

randint(500,2300)
梦回旧景 2024-10-27 18:51:11
from random import randint

compute = input(">")

if compute == 1:

    # If you have to store it anyway, might as well go whole-hog:
    vals = [int(l.split(",")[1]) for l in open("heights.csv").readlines()]

    reads[0] = vals[0] + randint(500, 1000)
    for i in xrange(len(vals) - 1):
        reads.append(vals[i-1] + randint(500, 1000) - vals[i])
        # not sure what to do with the last value
from random import randint

compute = input(">")

if compute == 1:

    # If you have to store it anyway, might as well go whole-hog:
    vals = [int(l.split(",")[1]) for l in open("heights.csv").readlines()]

    reads[0] = vals[0] + randint(500, 1000)
    for i in xrange(len(vals) - 1):
        reads.append(vals[i-1] + randint(500, 1000) - vals[i])
        # not sure what to do with the last value
递刀给你 2024-10-27 18:51:11
import csv
import random

with open('fofo.txt','rb') as f:
    rd = csv.reader(f)
    a = [int(row[1]) for row in rd]
    print 'a==',a

(.... to be continued by daikini's code...)

rd = csv.reader(f) 是一个迭代器,当使用 rd.next() 刺激或在 for 循环期间返回 csv 文件的一行。
文件必须以读取二进制模式打开 'rb'

将项目放入列表 a 只是通过列表索引对它们进行索引。

以下代码已被编辑:

-- 我在 a[len(a)-1] 更正为 a[len(a)-i] >read_b

-- 我将 random.randint(500,2300) 放入 R 的定义之外的 read_a 列表中

-- 通过 read_a[0] = a[1] + read_a[1] - a[0] 定义 read_a[0] 没有真正的意义。事实上 read_a[0] 正是 R[0] 。我删除了这一行

——事实上,也可以定义 read_b[3] 。结果是 read_a[0] ;因此 R[0]==read_a[0]==read_b[3] !--

import csv
import random

with open('heights.csv','rb') as f:
    a = [int(row[1]) for row in csv.reader(f)]
    print 'a==',a

compute = input(">")
if compute == 1:

    R = [random.randint(500,2300) for j in xrange(len(a)-1)]

    read_a = dict((i+1,a[i] + R[i] - a[i+1])
                  for i in xrange(0,len(a)-1) )

    read_b = dict( (i, a[len(a)-i] + read_a[len(a)-i] - a[len(a)-i-1])
                   for i in xrange(1,len(a)))




    print 'R==',R
    print 'read_a==',read_a
    print 'read_b==',read_b
    print

    for y in xrange(len(a)-1):
        print '  a[%s] ========== %s ******' % (y,a[y])
        print '      +R[%s]    == %s' % (str(y),R[y])
        print '    -read_a[%s] == %s' % (str(y+1),-read_a[y+1])
    print '  a[%s] ========== %s ******' % (len(a)-1,a[len(a)-1])
    print '\n'

    #print ' a['+str(len(a)-1)+']==',a[len(a)-1]
    for y in xrange(len(a)-1,0,-1):
        print '  a[%s] ========== %s ******' % (y,a[y])
        print '    +read_a[%s] == %s' % (y,read_a[y])
        print '    -read_b[%s] == %s' % (len(a)-y,-read_b[len(a)-y])
    print '  a[%s] ========== %s ******' % (0,a[0])
    print '\n'

    print 'sum(read_a.values())==',sum(read_a.values())
    print 'sum(read_b.values())===',sum(read_b.values())

最后,我们注意到 read_b.values() = R[::-1] 那么代码可以简化为:

import csv
import random

with open('heights.csv','rb') as f:
    a = [int(row[1]) for row in csv.reader(f)]
    print 'a==',a

compute = input(">")

if compute == 1:

    read_b = dict((j+1,random.randint(500,2300)) for j in xrange(len(a)-1))

    read_a = dict((i,a[i-1] + read_b[len(a)-i] - a[i])
                  for i in xrange(1,len(a)) )



    print 'read_a==',read_a
    print 'read_b==',read_b
    print

    for y in xrange(len(a)-1):
        print '  a[%s] ========== %s ******' % (y,a[y])
        print '    +read_b[%s] == %s' % (str(len(a)-y-1),read_b[len(a)-y-1])
        print '    -read_a[%s] == %s' % (str(y+1),-read_a[y+1])
    print '  a[%s] ========== %s ******' % (len(a)-1,a[len(a)-1])
    print '\n'

    for y in xrange(len(a)-1,0,-1):
        print '  a[%s] ========== %s ******' % (y,a[y])
        print '    +read_a[%s] == %s' % (y,read_a[y])
        print '    -read_b[%s] == %s' % (len(a)-y,-read_b[len(a)-y])
    print '  a[%s] ========== %s ******' % (0,a[0])
    print '\n'

    print 'sum(read_a.values())==',sum(read_a.values())
    print 'sum(read_b.values())==',sum(read_b.values())

这些代码可用于任意数量的行 >= 2

关于正数的条件:

read_b 中的数字是首先被计算的。因此,我搜索使 read_b 始终为正的条件:

由于 read_a[i] = a[i-1] +
read_b[len(a)-i] - a[i]

=> read_b[len(a)-i] = read_a[i] + a[i] - a[i-1]

=> read_b[j] = read_a[len(a)-j] + a[len(a)-j] - a[len(a)-j-1]

因为我们必须有 0 < read_a[len(a)-j]

=> a[len(a)-j] - a[len(a)-j-1] < read_a[len(a)-j] + a[len(a)-j] -
a[len(a)-j-1]

=> a[len(a)-j] - a[len(a)-j-1] < read_b[j]

=>我们必须有 a[len(a)-j] - a[len(a)-j-1] + 1 <= read_b[j]

那么条件

max(a[len(a)-j] - a[len(a)-j-1]  + 1 , 500))  <= read_b[j] 

我对吗?

import csv
import random

with open('fofo.txt','rb') as f:
    rd = csv.reader(f)
    a = [int(row[1]) for row in rd]
    print 'a==',a

(.... to be continued by daikini's code...)

rd = csv.reader(f) is an iterator that returns one row of the csv file when stimulated with rd.next() or during a for loop.
The file MUST be opened in reading binary mode 'rb'

Placing the items in a list a just indexes them by the index of the list.

.

The following code has been edited:

-- I corrected a[len(a)-1] with a[len(a)-i] in the definition of read_b

-- I put the random.randint(500,2300) in a list R out of the definition of read_a

-- There is no real interest to define read_a[0] by read_a[0] = a[1] + read_a[1] - a[0] . In fact read_a[0] is precisely R[0] . I eliminated this line

-- In fact , it is also possible to define read_b[3] . It turns out to be read_a[0] ; Hence R[0]==read_a[0]==read_b[3] !--

.

import csv
import random

with open('heights.csv','rb') as f:
    a = [int(row[1]) for row in csv.reader(f)]
    print 'a==',a

compute = input(">")
if compute == 1:

    R = [random.randint(500,2300) for j in xrange(len(a)-1)]

    read_a = dict((i+1,a[i] + R[i] - a[i+1])
                  for i in xrange(0,len(a)-1) )

    read_b = dict( (i, a[len(a)-i] + read_a[len(a)-i] - a[len(a)-i-1])
                   for i in xrange(1,len(a)))




    print 'R==',R
    print 'read_a==',read_a
    print 'read_b==',read_b
    print

    for y in xrange(len(a)-1):
        print '  a[%s] ========== %s ******' % (y,a[y])
        print '      +R[%s]    == %s' % (str(y),R[y])
        print '    -read_a[%s] == %s' % (str(y+1),-read_a[y+1])
    print '  a[%s] ========== %s ******' % (len(a)-1,a[len(a)-1])
    print '\n'

    #print ' a['+str(len(a)-1)+']==',a[len(a)-1]
    for y in xrange(len(a)-1,0,-1):
        print '  a[%s] ========== %s ******' % (y,a[y])
        print '    +read_a[%s] == %s' % (y,read_a[y])
        print '    -read_b[%s] == %s' % (len(a)-y,-read_b[len(a)-y])
    print '  a[%s] ========== %s ******' % (0,a[0])
    print '\n'

    print 'sum(read_a.values())==',sum(read_a.values())
    print 'sum(read_b.values())===',sum(read_b.values())

.

Finally, we notice that read_b.values() = R[::-1] then the code can be simplified to:

import csv
import random

with open('heights.csv','rb') as f:
    a = [int(row[1]) for row in csv.reader(f)]
    print 'a==',a

compute = input(">")

if compute == 1:

    read_b = dict((j+1,random.randint(500,2300)) for j in xrange(len(a)-1))

    read_a = dict((i,a[i-1] + read_b[len(a)-i] - a[i])
                  for i in xrange(1,len(a)) )



    print 'read_a==',read_a
    print 'read_b==',read_b
    print

    for y in xrange(len(a)-1):
        print '  a[%s] ========== %s ******' % (y,a[y])
        print '    +read_b[%s] == %s' % (str(len(a)-y-1),read_b[len(a)-y-1])
        print '    -read_a[%s] == %s' % (str(y+1),-read_a[y+1])
    print '  a[%s] ========== %s ******' % (len(a)-1,a[len(a)-1])
    print '\n'

    for y in xrange(len(a)-1,0,-1):
        print '  a[%s] ========== %s ******' % (y,a[y])
        print '    +read_a[%s] == %s' % (y,read_a[y])
        print '    -read_b[%s] == %s' % (len(a)-y,-read_b[len(a)-y])
    print '  a[%s] ========== %s ******' % (0,a[0])
    print '\n'

    print 'sum(read_a.values())==',sum(read_a.values())
    print 'sum(read_b.values())==',sum(read_b.values())

These codes can be used for any number of lines >= 2

.

Concerning the condition of positive numbers:

Numbers in read_b are the first to be computed. So I search for the condition that makes read_b always positive:

Since read_a[i] = a[i-1] +
read_b[len(a)-i] - a[i]

=> read_b[len(a)-i] = read_a[i] + a[i] - a[i-1]

=> read_b[j] = read_a[len(a)-j] + a[len(a)-j] - a[len(a)-j-1]

Since we must have 0 < read_a[len(a)-j]

=> a[len(a)-j] - a[len(a)-j-1] < read_a[len(a)-j] + a[len(a)-j] -
a[len(a)-j-1]

=> a[len(a)-j] - a[len(a)-j-1] < read_b[j]

=> we must have a[len(a)-j] - a[len(a)-j-1] + 1 <= read_b[j]

Then condition

max(a[len(a)-j] - a[len(a)-j-1]  + 1 , 500))  <= read_b[j] 

Am I right ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文