Python Forum
Small help for the python array save
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Small help for the python array save
#1
Hello Folks ,
I need a small help . Here is my code:
from random import randint
X=[[8, 8, 8, 8, 6], [29, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
SaveX=X
K=[]
for t in range(5):
    a=randint(5,10)
    K.append(a)
    print(K)
for a in range(5):
    X[1][0]=X[1][0]+K[a]
    print(X)
The output is like


[6, 7, 6, 9, 8]
[[8, 8, 8, 8, 6], [35, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
[[8, 8, 8, 8, 6], [42, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
[[8, 8, 8, 8, 6], [48, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
[[8, 8, 8, 8, 6], [57, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
[[8, 8, 8, 8, 6], [65, 27, 27, 27, 27], [9, 9, 9, 9, 10]]

It always sum the random value on the new list cumulatively, but what I want is , to sum the random values always to the first value of original list which is 29 . So I should obtain
[[8, 8, 8, 8, 6], [35, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
[[8, 8, 8, 8, 6], [36, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
[[8, 8, 8, 8, 6], [35, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
[[8, 8, 8, 8, 6], [38, 27, 27, 27, 27], [9, 9, 9, 9, 10]]
[[8, 8, 8, 8, 6], [37, 27, 27, 27, 27], [9, 9, 9, 9, 10]]

and when I sum the all the elemenets of above lists ,I should choose the list which has the least sum among all these 5 arrays ? Let's say the sum of the first array is 100, second is 85, third is 40, fourth is 98, fifth is 135, I should obtain as output second third array whc has least sum (40) how can I do that ?
thank you so much
Reply
#2
Please, repost using proper tags when post code, traceback, output, etc.
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to save to multiple locations during save cubangt 1 509 Oct-23-2023, 10:16 PM
Last Post: deanhystad
Question Opening small size browser with python selenium not work, need help greenpine 0 1,587 Feb-07-2022, 11:36 AM
Last Post: greenpine
  Python Regular expression, small sample works but not on file Acernz 5 2,861 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,963 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Save Numpy Array in Another Numpy Array (not Concatenate!) quest 1 1,822 Nov-06-2020, 01:06 PM
Last Post: quest
  Problems with save array into Mysql storzo 1 2,065 Sep-27-2019, 04:25 PM
Last Post: storzo
  Trying to save array results to a csv jos88 0 1,575 Sep-08-2019, 08:01 PM
Last Post: jos88
  python cache for small integer Uchikago 1 2,457 Jun-27-2019, 05:32 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020