0

Can someone help me write a python code that does this:

A = (1, 3, 5, 5, 2, 4, 6)

Basically, I want to create a variable that will take the first value in the array (in this case, 1), and spit out the difference of each element from that value:

eg. 1 = 1 = 0, 3-1 = 2, 5-1 = 0, etc

to finally spit out a variable that has:

A = (0, 2, 4, 4, 1, 3, 5) 
martineau
  • 119,623
  • 25
  • 170
  • 301
NeuroKween
  • 45
  • 8
  • 4
    That's a tuple, not a list. – jordanm Feb 24 '20 at 17:53
  • 2
    `[x - A[0] for x in A]` – jordanm Feb 24 '20 at 17:54
  • So, you have a list ```A``` containing integers and you want to create another list ```B```, where ```B[i] = A[i] - A[0]```? Also @jordanm solution should work – Melon Feb 24 '20 at 17:55
  • 3
    Welcome to stack overflow! This is not a code-writing or tutorial service, and we ask that you provide a [mcve] for your question, including _code_ for what you've tried based on your own research – G. Anderson Feb 24 '20 at 17:56
  • `A = tuple(value-A[0] for value in A)` – martineau Feb 24 '20 at 17:57
  • Yep, that's the idea! – NeuroKween Feb 24 '20 at 17:57
  • What is the issue, exactly? Have you tried anything, done any research? Stack Overflow is not a free code writing service. See: [tour], [ask], [help/on-topic], https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users. – AMC Feb 25 '20 at 01:20

5 Answers5

0

This can be your solution.

>>> a = [1,2,3,5,6]   # your list
>>> a_new = [i-a[0] for i in a]   # The output that you need.
>>> a_new
[0, 1, 2, 4, 5] 
shivankgtm
  • 1,207
  • 1
  • 9
  • 20
0

First, a list in python is written with square brackets, like that:

l = [1, 3, 5, 5, 2, 4, 6]

To get your result there are many ways. One way would be list comprehensions, for example with

 [x - l[0] for x in l]

You can use map:

map(lambda x: x-l[0], l)

You could use a loop of many other ways.

rolf82
  • 1,531
  • 1
  • 5
  • 15
  • 1
    The `map()` solution would produce a generator, so you would need to wrap it inside a `list()` call. – norok2 Feb 24 '20 at 18:08
0

Basically, I want to create a variable that will take the first value in the array (in this case, 1), and spit out the difference of each element from that value:

The object A points to a tuple not a list or array.

Here's, how you should do it (One liner in Python) using generator expression:

A = (1, 3, 5, 5, 2, 4, 6)
print(tuple(i - li[0] for i in A))

Outputs:

(0, 2, 4, 4, 1, 3, 5)
abhiarora
  • 9,743
  • 5
  • 32
  • 57
0

It is a simple program no need to use lambda and complicate things.

l = [1,3,4,6,5,3,6,8]
num = l[0]
l = [i-num for i in l]
print(l)

Use this if you do not want to include the first element -

l = [1,3,4,6,5,3,6,8]
num = l[0]
l = l[1:]
l = [i-num for i in l]
print(l)

l[1:] ---- this takes the values from index 1 to end from the list.

0

This is a simple question. People have been using lambda functions but don't worry, no need to complicate things here.

A = [1, 3, 5, 5, 2, 4, 6]
B = [num-A[0] for num in A]

This gives you the required array.