python - Manipulate List with calculating column -
i'm using ipython 2.7
, want analyze production system network.
therefore, i'm trying create network out of csv-file
. csv-file
includes 2 columns. first column representation of work orders; second represents working stations. work orders represent modules. working stations have nodes of network. following data.csv
-file:
order_id,machine_id, 0,0, 0,1, 1,1, 2,2, 2,3, 2,4, 2,5, 2,6, 2,1, 2,7, 2,2, 3,8, 3,1, 4,9, 5,10, 5,10, 5,5, 5,11, 5,0, 5,12, 5,13,
as long orderid
has same number, 1 module. in list 5 modules. algorithm creating edges first part of network analysis. algorithm creates edges while in same module. algorithm works , isn't part of problem. following code has analyze machine distribution in module.
for example: machine id
2 part of orderid
2. need solution 1 in results, machine id
2 appears 2 times in orderid
2. code's output 2. machineid
10 in modul5
appears 2 times, well. code needs addition in last for-loop (analysis)
. guess code needs if
check, adds machineid
1 time per module(orderid)
.
i can't find solution, because of interdependence of 2 columns. current code:
#import module import networkx nx import csv #create graph g = nx.digraph() #open file data = open("data.csv") csv_f = csv.reader(data,delimiter=',') #open file "data.csv" data.next() #skiping first row in file #algorithmus creating edges prevorder = -1 prevmachine = -1 nodes = [] #creating file calculate sum of nodes summodul = [] #creating file calculate sum of moduls row in csv_f: if row[0] == prevorder: g.add_edge(str(prevmachine),str(row[1])) prevorder = row[0] prevmachine = row[1] nodes.append(row[1]) summodul.append(row[0]) # add index list of moduls quamodul = len(list(set(summodul))) # quantity of moduls data.close() #node reuse print "quantity of moduls: "+ str(quamodul) #sort list of nodes sum of sorted nodes quantitynodes = [] node in g.nodes(): quantitynodes.append(int(node)) quantitynodes.sort() #analysis sortnode in quantitynodes: #for-loop nodes noderu = [nodes.count(str(sortnode))] nru in noderu: print "node reuse of node: " + str(sortnode) print float(nru) print float((float(nru)*100)/quamodul)
i'm expecting following results:
qantity of moduls: 6 node reuse of node: 0 2.0 33.3333333333 node reuse of node: 1 4.0 66.6666666667 node reuse of node: 2 1.0 16.6666666667 node reuse of node: 3 1.0 16.6666666667 node reuse of node: 4 1.0 16.6666666667 node reuse of node: 5 2.0 33.3333333333 node reuse of node: 6 1.0 16.6666666667 node reuse of node: 7 1.0 16.6666666667 node reuse of node: 8 1.0 16.6666666667 node reuse of node: 10 1.0 16.6666666667 node reuse of node: 11 1.0 16.6666666667 node reuse of node: 12 1.0 16.6666666667 node reuse of node: 13 1.0 16.6666666667
your problem unconditionally nodes.append(row[1])
. thus, each time machineid encountered added without checking if appended same order.
so, simplest solution can think of solve this, leverage fact orders in order. can make dictionary maps each machine last order. append if machine never encountered before, or encountered , last order different current order. here part made changes code:
d = {} # machine last order dictionary row in csv_f: if row[0] == prevorder: g.add_edge(str(prevmachine),str(row[1])) prevorder = row[0] prevmachine = row[1] #check machine has never been encountered before or last order different current one. if ( row[1] not in d or (row[1] in d , d[row[1]] != row[0])): nodes.append(row[1]) summodul.append(row[0]) # add index list of moduls d[row[1]] = row[0] # map machine order quamodul = len(list(set(summodul))) # quantity of moduls data.close()
hints writing better question code difficult follow, took me long time understand problem , lucky had answered question of yours same topic before. so, kindly mind following when asking question:
1- try make code more readable, instance can add comments, , able track going on.
2- post code able reproduce problem, try think yourself, problem might , think can solved.
3- when explain problem try relate explanation code. instance took me while figure out modul order.
Comments
Post a Comment