-4

I have some data looks like in this format

2,3,4 
3,4,5 
5,6,7

I pack the array as:

with open('house_price_data.txt') as data:
substrings = data.read().split()
array = [map(int, substring.split(',')) for substring in substrings]

My task is to do some calculation like this for each data in the set:

(2-3)**2 + (3-3)**2 + (5-3)**2
(3-4)**2 + (4-4)**2 + (5-4)**2

My expected answer is C1 = 5 and C2 = 2

I wrote a code like this

for [a for a, b, c in array] in range (0,2):
C1 = (([a for a, b, c in array]) - 3)**2
C2 = (([b for a, b, c in array]) - 4)**2

But it is not working. For the purpose of for loop, I think it will read the data 2,3,5 one by one minus 3 and square the result one by one and sum the total results. So how can I improve it?

A part from that, I also have problems with this code

[a for a, b, c in array]
[b for a, b, c in array]
[c for a, b, c in array]

I need to call array many times with this code with item a, b and c of the array in the program, when I have such codes in the program error massage come

not enough values to unpack (expected 3, got 0)

How can I do to make changes?

poonck1
  • 71
  • 1
  • 1
  • 6
  • What is the logic of the formula? And what would be the output for the given sample input? – trincot Feb 25 '17 at 16:37
  • 2
    Unclear where those subtractions come from or how you expect to subtract a number from a list... – OneCricketeer Feb 25 '17 at 16:40
  • @cricket_007, For loop is to call the data 2,3,5 for C1 and 2,4,5 for C2. My expected answer is C1 = 5 and C2 = 2 – poonck1 Feb 25 '17 at 16:44
  • How do you get 2,4,5 from that array? – OneCricketeer Feb 25 '17 at 16:45
  • @cricket_007, just corrected the error – poonck1 Feb 25 '17 at 16:47
  • Okay, the logic of 2,3,5 does not match 3,4,5 – OneCricketeer Feb 25 '17 at 16:50
  • 2
    Looks like it's your first ever attempt at writing any Python code as your loop doesn't make _any_ sense at all because of this list comprehension and subtraction of integers from lists. Seriously, please try to learn some Python before asking here. – ForceBru Feb 25 '17 at 16:50
  • I still don't understand how the initial 2d-array corresponds to the numbers in the formulas. Maybe you could clarify by using unique numbers in the array and the formulas? – tobias_k Feb 25 '17 at 16:56
  • 1
    @ForceBru The OP is up to a problem and has asked three questions on SO related to it ([1](http://stackoverflow.com/questions/42393894/how-to-read-data-from-text-file-into-array-with-python), [2](http://stackoverflow.com/questions/42457461/count-and-calculation-in-a-2d-array-in-python) and the current one) . So, like you said, *looks like it's indeed their first ever attempt at writing any Python code* – Sнаđошƒаӽ Feb 25 '17 at 17:11
  • A piece of advice to the OP, please do not try to get everything done for you by other people. Instead, first learn some python and basic logic. You can surely get *almost everything* done for you by using this site, but trust me, then you'll never be a good programmer. – Sнаđошƒаӽ Feb 25 '17 at 17:15
  • Indeed, this is my first time to write something about array. Actually, I encountered the problems are related to array. If someone can help me to solve it, I am appreciated. . – poonck1 Feb 26 '17 at 02:54

2 Answers2

0

If you have array = [[2,3,4],[3,4,5],[5,6,7]], then you want a = [2,3,5], then that would be

a = [x[0] for x in array]

Otherwise, array[0] is [2,3,4] and you can instead do

a, b, c = array 

To unpack the 2D array.


Sidenote: you seem to have a CSV file, so I would strongly suggest using Pandas and Numpy for your numerical calculations

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

This question is unclear and probably destined for oblivion, but if I understand correctly, which is far from certain, you are trying to do something like this.

array = [[2, 3, 5], [3, 4, 5], [5, 6, 7]]

#initialize the variables C1 and C2
C1 = 0
C2 = 0

#iterate the elements of the FIRST list in your list
#so 2,3,5 (I assume you have indicated 2,3,4 by mistake)
for element in array[0]:
    C1+=(element-3)**2

#iterate the elements of the SECOND list in your list
#so 3,4,5
for element in array[1]:
    C2+=(element-4)**2

print("C1 =", C1)
print("C2 =", C2)

Output:

C1 = 5
C2 = 2

But your example is ambiguous. Maybe 2,3,5 are the first elements in each sublist ? In this case, the logic is the same.

#iterate the FIRST element in each sublist in your list
for element in array:
    C1+=(element[0]-3)**2

If that's what you want to do, then it's best for you to do it like that, with classic loops. List comprehensions (things like [x for x in array if ...]) are shortcuts for advanced Python programmers. They do exactly the same thing, but are less clear and more error prone.

Ettore Rizza
  • 2,800
  • 2
  • 11
  • 23