0

I have a list of integers. I need to add a value to all the elements in the the list. This addition on the list must happen n times and I need a print of all the values

I have x elements in the list. So i typically created a array of x and all the elements with value n. Unable to create a loop to execute the process though in python?

  • Broadcast it: `K = 123 + mylist`. – Adrian Keister Oct 08 '20 at 02:23
  • @AdrianKeister: This requires `mylist` to not be a `list`, but a `numpy` `array` type. – ShadowRanger Oct 08 '20 at 02:24
  • Oh, you're right. Could also convert, then convert back. Don't know if that gains you anything, though. – Adrian Keister Oct 08 '20 at 02:26
  • `sum()` is for adding all the elements of the list togther, it's not for adding something to each element. – Barmar Oct 08 '20 at 02:26
  • I tried the array function as well but unable to loop it 250 times and print all the values – muth.code13 Oct 08 '20 at 02:27
  • @MuthyaMounika If you're adding 123 a total of 250 times, why not just add 30750 to each element of the list? – Adrian Keister Oct 08 '20 at 02:27
  • @AdrianKeister I need all the samples. for example my timestamp is at 1, the next current value occurs at 1+123, then 2+123 so on for 256 datapoints of a single packet. the next data point in the list belongs to the next packet. – muth.code13 Oct 08 '20 at 02:31
  • Well, I'm totally confused. Could you please post a small sample of the data you have before this loop, and what you need it to look like after the loop runs? Especially include the dimensions of each array, and the sizes (i.e., the shapes of each array or list). – Adrian Keister Oct 08 '20 at 02:42
  • @AdrianKeister My excel file is as follows: Col A 0 25650 51200 76800 102400 and so on 392 rows In excel for column B: B=A2+123 Similarly C=B+123 and D=C+123 Col A. Col B 0. 123 25650. 25773 51200. 51323 76800. 76923 102400. 102523 I will need this operation to occur 255 times. As i have 392 rows, need to print 392*256(incl col A) data points which can be unodered. – muth.code13 Oct 08 '20 at 03:05

2 Answers2

1

Using normal forloop.

myList = [0, 12, 30, 32] #column A
for i in myList:
    holder = i
    temp_list=[i,]
    for x in range(10): #change range(10) to range(250)
        holder += 123
        temp_list.append(holder)
    #print(temp_list)
    print(*temp_list, sep='    ')
ron_olo
  • 146
  • 6
  • when I am already adding the 123 via dummy array, why to add 123 at new_value? I would like to add this 123 again to the newly obtained list but still have a duplicate of that value. I would need to keep doing this addition 250 times. – muth.code13 Oct 08 '20 at 02:51
  • My excel file is as follows: Col A 0 25650 51200 76800 102400 and so on 392 rows In excel for column B: B=A2+123 Similarly C=B+123 and D=C+123 Col A. Col B 0. 123 25650. 25773 51200. 51323 76800. 76923 102400. 102523 I will need this operation to occur 255 times. As i have 392 rows, need to print 392*256(incl col A) data points which can be unodered. list A only prints Col B but not C, D etc till IW – muth.code13 Oct 08 '20 at 03:22
  • I change my answer and add some comments. hope it solves your problem – ron_olo Oct 08 '20 at 04:41
  • if you need to print it without commas and brackets. change print(temp_list) to print(*temp_list, sep=' ') – ron_olo Oct 08 '20 at 04:49
  • Great. If this answer solved your problem, please mark it as 'accepted' by clicking the green check mark. – ron_olo Oct 09 '20 at 00:11
  • Example i have the time stamp and the duration (34016) that needs to be spread over 256 datapoints by adding 123 so I dont need it as (i = ; i – muth.code13 Oct 09 '20 at 02:26
  • can you clarify your last question. and add it to the main question – ron_olo Oct 09 '20 at 02:26
  • I have updated the console image on the main question. The data is printed as the right triangle model of data but the only information needed is available in the 5th row which i had highlighted as i gave range(5). Also i was wondering if it is possible to print it to a list – muth.code13 Oct 09 '20 at 02:58
  • you're print(*temp_list, sep=' ') is inside the inner loop it should be only on the main loop. please check the indention of my answer – ron_olo Oct 09 '20 at 03:08
0

Like this?

myList = [YOUR LIST]
newList = [x+123 for x in myList]
print(newList)
Wasif
  • 14,755
  • 3
  • 14
  • 34