Sql pareto for column values per row help request

append did not work

I posted how extend and append didn’t work. Never found out why.

No error, just never completed. It would just timeout instead of append or extend.
When I got insert to work, I focused on that.

Probably because you’re inserting on the wrong thing, which might explain why append did not work.

Can you try a simple summary of what you need, with a simple example ?

What do you mean I am inserting in the wrong thing?
I always insert on a recalculated length. It is always inserted at the end.

It is working

back before I had it working:
this post I had appending and extending timing out

I currently do this:

for row in dataIn:
	data=[row[colName] for colName in reasonHeaders]	
	pareto = [[x, y] for y, x in sorted(zip(data, reasonHeaders),reverse=True)]

print pareto
 
for row in dataIn:
	data2=[row[colName] for colName in suffixHeaders]	
	testThis =[[x,y] for y, x in zip(data2,suffixHeaders)]
	
print testThis
print len(pareto)

for i in range(len(pareto)):
 for j in range(len(testThis)):
  	pareto[i].insert(len(pareto),testThis[j][1])

for i in range(len(pareto)):
	if pareto[i][1]!=0 and pareto[i][3] !=0:  
	   pareto[i].insert(len(pareto),(str(round( (float(pareto[i][1])/float(pareto[i][3]))*100,2) )+"%"))
	else:    
	   pareto[i].insert(len(pareto),str(round(float(0.00),2))+"%") 
	
print '-----'

print pareto[0][0]
print pareto[0][1]
print pareto[0][3]
print pareto[0][4]
	
print pareto  

The results are great.

It sounds like you are saying it is efficient to calculate the length once.
If I counted the length only once, then when I insert, the next insert would be off by one.

I mean you're inserting in pareto[i] using the length of pareto, which doesn't look like something you'd want to do.
I kind of feel like it working is a bug :smiley:

This is appending the same thing testThis to the same receiver pareto over and over again.
Note that there, you were appending to pareto, not to pareto[i]

how is it a bug?

append doesn’t work

it times out

Why ? When does the length of pareto change ? You're inserting into pareto[i], that doesn't change the length of pareto !

look:

pareto = [[], [], []]

print(len(pareto))

for i, p in enumerate(pareto):
	p.insert(len(pareto), i)
	print(len(pareto))

print(pareto)

output:

3
3
3
3
[[0], [1], [2]]

Clearly, inserting at index 3 in an empty list works, but it's not what you want to do.
It's a bug in the logic. It doesn't prevent it from having a valid output, but it's not what you want to do.

2 Likes

I don’t understand that.

I think you are saying something important, but I do not understand what you are saying.

I think I need a break, and will come back refreshed maybe.

He is saying that if you append to a list inside of pareto, you are not changing the length of pareto but the length of the list inside pareto. If pareto[1] has 50 items in it, pareto still only has 3 lists in it.

1 Like

To be even more exhaustive:

pareto = [[],[],[]]

print len(pareto)

for i in range(len(pareto)):
	pareto[i].insert(len(pareto), i)
	print len(pareto)

print pareto

for i in range(len(pareto)):
	pareto[i].append(i)
	print len(pareto)

print pareto

for i,p in enumerate(pareto):
	p.append(i)
	print len(pareto)

print pareto

output:

3
3
3
3
[[0], [1], [2]]
3
3
3
[[0, 0], [1, 1], [2, 2]]
3
3
3
[[0, 0, 0], [1, 1, 1], [2, 2, 2]]

appending is the exact same thing as "inserting at the end"

1 Like

I’m also saying that the current code will break sooner or later.
Probably sooner.

1 Like

I am back

haha super bugged code that works and doesn't

so len(pareto) is 18

I pass 1-18 through "i"

len(testThis) is 2

len(pareto[i]) is 2 before I start to add things to it, gets to be size 5 at the end right?

so where I am calling len(pareto) what is happening?
I am inserting at the 18th position which doesn't exist so it goes on the end?

I should be calling len(pareto[i]) to get the real position to insert if I understand it.

You mean you pass 0-17?

1 Like

Python has 0-based indexing, so the first element is 0…17. Some software has 1-based indexing, so for Matlab, the first element starts with 1. As mentioned, use .append() not insert. If append doesn’t work you have other problems. insert() will work but not the best solution to this.

It should probably be this, but I still say append should be used instead.

pareto[i].insert(len(pareto[i]),testThis[j][1])
1 Like

why do you recommend append over insert?

it will automatically put the new element at the end of the list

When you start getting into programming/scripting that is hard to follow, I recommend writing pseudo code/algorithm first (detailed explanation of each logical step), then use that as a guide to write the script.

1 Like

Let’s take this from the top.

What you have:
A database table with a bunch of columns, 4 of those being metadata

What you want:
A table with, with rows matching the database, but with only 5 columns, for the highest 5 columns from the database table

What I’m not sure about:
What happens to the 4 metadata columns ?
What headers do you want ? If you want to keep the headers of the 5 selected columns, how should they be stored, considering they’re not going to be the same for each row ?

Anything I missed ?

Provide that information and I’ll write the script you want, with explanation about everything.

1 Like

for the highest 5 columns from each row of the table.

Requirements had changed on me as I went.

My outputs should be like:
machine, reason, qty, net, defects, percent qty/defects for each row
rows are sorted, not just 5

on top of that, I removed some of the rows entirely because some of the reasons, were kind of in a between meta data and not exactly a reason

it is kind of unfinished as I need more rows, or more pieces of data from the plc
the total is not 100%


I am still looking at posts from this morning.

Some of the things suggested seemed like I should implement.


and now I add the prefixheaders

which is meta data, that goes first, but I probably could append it to the end, then position it in the table component in perspective with columns.

your offer is generous @pascal.fragnoud
I want to understand the scripting more and I think if I hand it off, then try to learn it afterwards, it will not be effective for me.

Yes that's how I understood it, this is what 'with rows matching the database' meant.
For each row in the database table, make a row in a table where the columns are the top 5 database columns for that row.

I have no idea what this means.