Get Specific Element from a 2d List Using Condition - python-3.x
Here is a list.
list=[[[5, 1, 50], [7, 10, 52], 2], [[7, 10, 52], [10, 5, 163], 3], [[10, 5, 163], [13, 9, 85], 3], [[13, 9, 85], [14, 3, 176], 1], [[14, 3, 176], [15, 7, 96], 1]]
I want to retrieve an element from the list based on the minimum value which are 2,3,3,1,1 or we can say list[i][j]. From these element I want to find the minimum which is 1 and from that I want to retrieve the element [[[13, 9, 85], [14, 3, 176], 1]]. As here two 1 exist, indexwise I am choosing first one.
Can you tell me how can I do the code?
Sorry for being noob.
Just use find minimum concept with O(N)
list1=[[[5, 1, 50], [7, 10, 52], 2], [[7, 10, 52], [10, 5, 163], 3], [[10, 5, 163], [13, 9, 85], 3], [[13, 9, 85], [14, 3, 176], 1], [[14, 3, 176], [15, 7, 96], 1]]
# Assign first element as a minimum.
min1 = list1[0][2]
minIx = 0;
for i in range(len(list1)):
# If the other element is less than min element
if list1[i][2] < min1:
min1 = list1[i][2] #It will change
minIx = i
print("The smallest element in the list is ",list1[minIx])
Related
What is the Easiest way to extract subset of a 2D matrix in python?
mat = [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35]] Lets say I want to extract upper left 2x2 matrix [[0, 1,], [6, 7, ]] doing mat2=mat[:2][:2] doesnt work. It extracts the rows correctly but not columns.Seems like I need to loop throughto get the columns. Additionally I need to do a deepcopy to mat2 suchthat modifying mat2 dont change mat.
This is because [:2] returns a list containing the first 2 elements of your matrix. For example :- arr = [[1, 2], [1, 3]] print(arr[:2]) # will print the first 2 elements of the array, that is [1, 2] and [1, 3], packed into a list. So, Output : [[1, 2], [1, 3]]. In the same way, mat = [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17], [18, 19, 20, 21, 22, 23], [24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35]] mat2 = mat[:2] # => [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]] # Now, if you again try to get the first 2 elements from mat2 you will get the first 2 elements of mat2, not the first 2 elements of the lists inside mat2. mat3 = mat2[:2] # => [[0, 1, 2, 3, 4, 5], [6, 7, 8, 9, 10, 11]] That is where you went wrong, but this concept is quite counter-intuitive, so no worries. So the solution would be to get the first 2 elements from matrix mat and then loop over its elements and then get the first 2 elements from them. Therefore, this should work for you: list(x[:2] for x in mat[:2]) Or, as #warped pointed, if you can use numpy, you can do the following: import numpy as np mat = np.array(mat) mat[:2, :2]
How to compare items in lists of lists by index position using a condition? (Python)
I am comparing two lists of lists x and a. The items in the lists in a must be higher than those with the same index position in the lists in x. If not, that list must be thrown out of a. A counter tracks how many lists in a meet the condition for each list in x. With comparing items in a vs. x by index position I mean: If x = [[a, b], [c, d]] And a = [[A, B], ... [...]] Then compare A only with a, and B with only b. Then compare A only to c, B only to d, etc. I have the following code, which works when the condition is set to >= but not when reversed to <. I have been helped by answers on this forum here: How to compare items in two lists of lists by matrix positions? Python. But no post answers my question fully. The solution is probably simple and staring me in the face, but I just cannot figure out how to fix this. Any suggestions what I am doing wrong? I would be very grateful if someone has a solution. x = [[10, 11], [14, 12]] a = [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16]] list = [] ans=[] for i in x: # for every Barrier path i in x c=0 lis = [] for j in a: # for every stock price path j in a for z in range(len(j)): if(i[z]>=j[z]): c=c+1 lis.append(j) break ans.append(c) list.append(lis) The results are as expected: ans: [2, 6] list: [[[9, 10], [10, 11]], [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15]]] However, with the same code and only the condition reversed from >= to <, the result is not as expected. x = [[10, 11], [14, 12]] a = [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16]] list = [] ans=[] for i in x: # for every Barrier path i in x c=0 lis = [] for j in a: # for every stock price path j in a for z in range(len(j)): if(i[z]<j[z]): c=c+1 lis.append(j) break ans.append(c) list.append(lis) ans: [5, 4] Expected: [5, 1] list: [[[11, 12], [12, 13], [13, 14], [14, 15], [15, 16]], [[12, 13], [13, 14], [14, 15], [15, 16]]] Expected: [[[11, 12], [12, 13], [13, 14], [14, 15], [15, 16]], [[15, 16]]] When flipping the logical condition, I expect results that are the reverse of the answer above but they are not. Any suggestions how to change the code to solve this?
Here is what you are looking for: def compare_lists(list1,list2, key = lambda x, y: x>=y, key2 = any): s = [[i for i in list2 if key2([key(m,n) for m,n in zip(k,i)])] for k in list1] return {"ans" : [len(i) for i in s], "list" : s} Below are the results with different keys: print(compare_lists(x, a, lambda x,y: x<y,all)) # all to be less than {'ans': [5, 1], 'list': [[[11, 12], [12, 13], [13, 14], [14, 15], [15, 16]], [[15, 16]]]} print(compare_lists(x, a))# any to be greater than {'ans': [2, 6], 'list': [[[9, 10], [10, 11]], [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15]]]} print(compare_lists(x, a, lambda x,y: x<y)) #any to be less than {'ans': [5, 4], 'list': [[[11, 12], [12, 13], [13, 14], [14, 15], [15, 16]], [[12, 13], [13, 14], [14, 15], [15, 16]]]} print(compare_lists(x, a, lambda x,y: x>=y,all)) # all to be greater than {'ans': [2, 3], 'list': [[[9, 10], [10, 11]], [[9, 10], [10, 11], [11, 12]]]}
Simplify numpy expression [duplicate]
This question already has answers here: Access n-th dimension in python [duplicate] (5 answers) Closed 2 years ago. How can I simplify this: import numpy as np ex = np.arange(27).reshape(3, 3, 3) def get_plane(axe, index): return ex.swapaxes(axe, 0)[index] # is there a better way ? I cannot find a numpy function to get a plane in a higher dimensional array, is there one? EDIT The ex.take(index, axis=axe) method is great, but it copies the array instead of giving a view, what I originally wanted. So what is the shortest way to index (without copying) a n-th dimensional array to get a 2d slice of it, with index and axis?
Inspired by this answer, you can do something like this: def get_plane(axe, index): slices = [slice(None)]*len(ex.shape) slices[axe]=index return ex[tuple(slices)] get_plane(1,1) output: array([[ 3, 4, 5], [12, 13, 14], [21, 22, 23]])
What do you mean by a 'plane'? In [16]: ex = np.arange(27).reshape(3, 3, 3) Names like plane, row, and column, are arbitrary conventions, not formally defined in numpy. The default display of this array looks like 3 'planes' or 'blocks', each with rows and columns: In [17]: ex Out[17]: array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]) Standard indexing lets us view any 2d block, in any dimension: In [18]: ex[0] Out[18]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [19]: ex[0,:,:] Out[19]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [20]: ex[:,0,:] Out[20]: array([[ 0, 1, 2], [ 9, 10, 11], [18, 19, 20]]) In [21]: ex[:,:,0] Out[21]: array([[ 0, 3, 6], [ 9, 12, 15], [18, 21, 24]]) There are ways of saying I want block 0 in dimension 1 etc, but first make sure you understand this indexing. This is the core numpy functionality. In [23]: np.take(ex, 0, 1) Out[23]: array([[ 0, 1, 2], [ 9, 10, 11], [18, 19, 20]]) In [24]: idx = (slice(None), 0, slice(None)) # also np.s_[:,0,:] In [25]: ex[idx] Out[25]: array([[ 0, 1, 2], [ 9, 10, 11], [18, 19, 20]]) And yes you can swap axes (or transpose), it that suits your needs.
Removing permutations from a list
I am trying to remove duplicates from a list of permutations to just leave the combinations , the list of number is as follows: [[1, 3, 11, 13], [1, 7, 11, 9], [1, 9, 11, 7], [1, 15, 9, 3], [3, 1, 11, 13], [3, 5, 11, 9], [3, 15, 9, 1], [5, 3, 11, 9], [5, 11, 9, 3], [5, 13, 9, 1], [7, 1, 11, 9], [7, 11, 9, 1], [9, 1, 11, 7], [11, 5, 9, 3], [11, 7, 9, 1], [13, 5, 9, 1], [15, 1, 9, 3], [15, 3, 9, 1]] Here is a method I have made but none of the list being printed is empty : def permtocomb(fcombs): fcombinations=fcombs global value global comp global count global combs global b for z in range(0,len(fcombinations)): count = 0 print('testing array'+str(fcombinations[z])) for x in range(0,4): value=fcombinations[z][x] for j in range(0,len(fcombinations)): print('against arrqay'+str(fcombinations[j])) for v in range(0,4): if value==fcombinations[j][v]: count+=1 b=j if count<=3: #fcombinations.pop(fcombinations[c]) combs.append(fcombinations[b]) permtocomb(fcombinations) print(fcombinations) print(combs) Are there any python plugins or builtins capable of removing permutations just leaving the combinations?
It is usually a bad idea to use global variables as it makes debugging very hard. Here's a very simple way to do this, just keep a record of what you have seen. You can use set to ignore combinations but sets are unhashable so you can use frozensets, e.g.: In []: data = [[1, 3, 11, 13], ...] seen = set() result = [] for d in data: if frozenset(d) not in seen: result.append(d) seen.add(frozenset(d)) result Out[]: [[1, 3, 11, 13], [1, 7, 11, 9], [1, 15, 9, 3], [3, 5, 11, 9], [5, 13, 9, 1]] If you don't care about order this can be simplified: In []: [list(e) for e in set(frozenset(d) for d in data)] Out[]: [[1, 11, 9, 7], [11, 1, 3, 13], [9, 13, 5, 1], [1, 3, 9, 15], [11, 9, 3, 5]] However, if you really don't care about order you may want to just leave it as a set of frozensets: set(frozenset(d) for d in data) Note: This assumes there are no duplicate values in each of your elements (which your example data doesn't have), if you do have multiple values you will need to switch to multisets (collections.Counter()).
Node.js File Output Incomplete
I've got a script that I'm toying around with to format all generated permutations for a specific scenario so that I can then create an array of strings for the next part. For whatever reason, when I output to my 'output' file, the file stops at ' ["1-12-17-"], ' when it should end on ' ["25-24-23-"], ' The console output ends on the right string but the text file stops short. Here's the code: var fs = require('fs'); // 13800 var input = [ [1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 6], [1, 2, 7], [1, 2, 8], [1, 2, 9], [1, 2, 10], [1, 2, 11], [1, 2, 12], [1, 2, 13], [1, 2, 14], [1, 2, 15], [1, 2, 16], [1, 2, 17], [1, 2, 18], [1, 2, 19], [1, 2, 20], [1, 2, 21], [1, 2, 22], [1, 2, 23], [1, 2, 24], [1, 2, 25], [1, 3, 2], [1, 3, 4], [1, 3, 5], [1, 3, 6], [1, 3, 7], [1, 3, 8], [1, 3, 9], [1, 3, 10], [1, 3, 11], [1, 3, 12], [1, 3, 13], [1, 3, 14], [1, 3, 15], [1, 3, 16], [1, 3, 17], [1, 3, 18], [1, 3, 19], [1, 3, 20], [1, 3, 21], [1, 3, 22], [1, 3, 23], [1, 3, 24], [1, 3, 25], [1, 4, 2], [1, 4, 3], [1, 4, 5], [1, 4, 6], [1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 4, 10], [1, 4, 11], [1, 4, 12], [1, 4, 13], [1, 4, 14], [1, 4, 15], [1, 4, 16], [1, 4, 17], [1, 4, 18], [1, 4, 19], [1, 4, 20], [1, 4, 21], [1, 4, 22], [1, 4, 23], [1, 4, 24], [1, 4, 25], [1, 5, 2], [1, 5, 3], [1, 5, 4], [1, 5, 6], [1, 5, 7], [1, 5, 8], [1, 5, 9], [1, 5, 10], [1, 5, 11], [1, 5, 12], [1, 5, 13], [1, 5, 14], [1, 5, 15], [1, 5, 16], [1, 5, 17], [1, 5, 18], [1, 5, 19], [1, 5, 20], [1, 5, 21], [1, 5, 22], [1, 5, 23], [1, 5, 24], [1, 5, 25], [1, 6, 2], [1, 6, 3], [1, 6, 4], [1, 6, 5], [1, 6, 7], [1, 6, 8], [1, 6, 9], [1, 6, 10], [1, 6, 11], [1, 6, 12], [1, 6, 13], [1, 6, 14], [1, 6, 15], [1, 6, 16], [1, 6, 17], [1, 6, 18], [1, 6, 19], [1, 6, 20], [1, 6, 21], [1, 6, 22], [1, 6, 23], [1, 6, 24], [1, 6, 25], [1, 7, 2], [1, 7, 3], [1, 7, 4], [1, 7, 5], [1, 7, 6], [1, 7, 8], [1, 7, 9], [1, 7, 10], [1, 7, 11], [1, 7, 12], [1, 7, 13], [1, 7, 14], [1, 7, 15], [1, 7, 16], [1, 7, 17], [1, 7, 18], [1, 7, 19], [1, 7, 20], [1, 7, 21], [1, 7, 22], [1, 7, 23], [1, 7, 24], [1, 7, 25], [1, 8, 2], [1, 8, 3], [1, 8, 4], [1, 8, 5], [1, 8, 6], [1, 8, 7], [1, 8, 9], [1, 8, 10], [1, 8, 11], [1, 8, 12], [1, 8, 13], [1, 8, 14], [1, 8, 15], [1, 8, 16], [1, 8, 17], [1, 8, 18], [1, 8, 19], [1, 8, 20], [1, 8, 21], [1, 8, 22], [1, 8, 23], [1, 8, 24], [1, 8, 25], [1, 9, 2], [1, 9, 3], [1, 9, 4], [1, 9, 5], [1, 9, 6], [1, 9, 7], [1, 9, 8], [1, 9, 10], [1, 9, 11], [1, 9, 12], [1, 9, 13], [1, 9, 14], [1, 9, 15], [1, 9, 16], [1, 9, 17], [1, 9, 18], [1, 9, 19], [1, 9, 20], [1, 9, 21], [1, 9, 22], [1, 9, 23], [1, 9, 24], [1, 9, 25], [1, 10, 2], [1, 10, 3], [1, 10, 4], [1, 10, 5], [1, 10, 6], [1, 10, 7], [1, 10, 8], [1, 10, 9], [1, 10, 11], [1, 10, 12], [1, 10, 13], [1, 10, 14], [1, 10, 15], [1, 10, 16], [1, 10, 17], [1, 10, 18], [1, 10, 19], [1, 10, 20], [1, 10, 21], [1, 10, 22], [1, 10, 23], [1, 10, 24], [1, 10, 25], [1, 11, 2], [1, 11, 3], [1, 11, 4], [1, 11, 5], [1, 11, 6], [1, 11, 7], [1, 11, 8], [1, 11, 9], [1, 11, 10], [1, 11, 12], [1, 11, 13], [1, 11, 14], [1, 11, 15], [1, 11, 16], [1, 11, 17], [1, 11, 18], [1, 11, 19], [1, 11, 20], [1, 11, 21], [1, 11, 22], [1, 11, 23], [1, 11, 24], [1, 11, 25], [1, 12, 2], [1, 12, 3], [1, 12, 4], [1, 12, 5], [1, 12, 6], [1, 12, 7], [1, 12, 8], [1, 12, 9], [1, 12, 10], [1, 12, 11], [1, 12, 13], [1, 12, 14], [1, 12, 15], [1, 12, 16], [1, 12, 17], [1, 12, 18], [1, 12, 19], [1, 12, 20], [1, 12, 21], [1, 12, 22], [1, 12, 23], [1, 12, 24], [1, 12, 25], [1, 13, 2], [1, 13, 3], [1, 13, 4], [1, 13, 5], [1, 13, 6], [1, 13, 7], [1, 13, 8], [1, 13, 9], [1, 13, 10], [1, 13, 11], [1, 13, 12], [1, 13, 14], [1, 13, 15], [1, 13, 16], [1, 13, 17], [1, 13, 18], [1, 13, 19], [1, 13, 20], [1, 13, 21], [1, 13, 22], [1, 13, 23], [1, 13, 24], [1, 13, 25], [1, 14, 2], [1, 14, 3], [1, 14, 4], [1, 14, 5], [1, 14, 6], [1, 14, 7], [1, 14, 8], [1, 14, 9], [1, 14, 10], [1, 14, 11], [1, 14, 12], [1, 14, 13], [1, 14, 15], [1, 14, 16], [1, 14, 17], [1, 14, 18], [1, 14, 19], [1, 14, 20], [1, 14, 21], [1, 14, 22], [1, 14, 23], [1, 14, 24], [1, 14, 25], [1, 15, 2], [1, 15, 3], [1, 15, 4], [1, 15, 5], [1, 15, 6], [1, 15, 7], [1, 15, 8], [1, 15, 9], [1, 15, 10], [1, 15, 11], [1, 15, 12], [1, 15, 13], [1, 15, 14], [1, 15, 16], [1, 15, 17], [1, 15, 18], [1, 15, 19], [1, 15, 20], [1, 15, 21], [1, 15, 22], [1, 15, 23], [1, 15, 24], [1, 15, 25], [1, 16, 2], [1, 16, 3], [1, 16, 4], [1, 16, 5], [1, 16, 6], [1, 16, 7], [1, 16, 8], [1, 16, 9], [1, 16, 10], [1, 16, 11], [1, 16, 12], [1, 16, 13], [1, 16, 14], [1, 16, 15], [1, 16, 17], [1, 16, 18], [1, 16, 19], [1, 16, 20], [1, 16, 21], [1, 16, 22], [1, 16, 23], [1, 16, 24], [1, 16, 25], [1, 17, 2], [1, 17, 3], [1, 17, 4], [1, 17, 5], [1, 17, 6], [1, 17, 7], [1, 17, 8], [1, 17, 9], [1, 17, 10], [1, 17, 11], [1, 17, 12], [1, 17, 13], [1, 17, 14], [1, 17, 15], [1, 17, 16], [1, 17, 18], [1, 17, 19], [1, 17, 20], [1, 17, 21], [1, 17, 22], [1, 17, 23], [1, 17, 24], [1, 17, 25], [1, 18, 2], [1, 18, 3], [1, 18, 4], [1, 18, 5], [1, 18, 6], [1, 18, 7], [1, 18, 8], [1, 18, 9], [1, 18, 10], [1, 18, 11], [1, 18, 12], [1, 18, 13], [1, 18, 14], [1, 18, 15], [1, 18, 16], [1, 18, 17], [1, 18, 19], [1, 18, 20], [1, 18, 21], [1, 18, 22], [1, 18, 23], [1, 18, 24], [1, 18, 25], [1, 19, 2], [1, 19, 3], [1, 19, 4], [1, 19, 5], [1, 19, 6], [1, 19, 7], [1, 19, 8], [1, 19, 9], [1, 19, 10], [1, 19, 11], [1, 19, 12], [1, 19, 13], [1, 19, 14], [1, 19, 15], [1, 19, 16], [1, 19, 17], [1, 19, 18], [1, 19, 20], [1, 19, 21], [1, 19, 22], [1, 19, 23], [1, 19, 24], [1, 19, 25], [1, 20, 2], [1, 20, 3], [1, 20, 4], [1, 20, 5], [1, 20, 6], [1, 20, 7], [1, 20, 8], [1, 20, 9], [1, 20, 10], [1, 20, 11], [1, 20, 12], [1, 20, 13], [1, 20, 14], [1, 20, 15], [1, 20, 16], [1, 20, 17], [1, 20, 18], [1, 20, 19], [1, 20, 21], [1, 20, 22], [1, 20, 23], [1, 20, 24], [1, 20, 25], [1, 21, 2], [1, 21, 3], [1, 21, 4], [1, 21, 5], [1, 21, 6], [1, 21, 7], [1, 21, 8], [1, 21, 9], [1, 21, 10], [1, 21, 11], [1, 21, 12], [1, 21, 13], [1, 21, 14], [1, 21, 15], [1, 21, 16], [1, 21, 17], [1, 21, 18], [1, 21, 19], [1, 21, 20], [1, 21, 22], [1, 21, 23], [1, 21, 24], [1, 21, 25], [1, 22, 2], [1, 22, 3], [1, 22, 4], [1, 22, 5], [1, 22, 6], [1, 22, 7], [1, 22, 8], [1, 22, 9], [1, 22, 10], [1, 22, 11], [1, 22, 12], [1, 22, 13], [1, 22, 14], [1, 22, 15], [1, 22, 16], [1, 22, 17], [1, 22, 18], [1, 22, 19], [1, 22, 20], [1, 22, 21], [1, 22, 23], [1, 22, 24], [1, 22, 25], [1, 23, 2], [1, 23, 3], [1, 23, 4], [1, 23, 5], [1, 23, 6], [1, 23, 7], [1, 23, 8], [1, 23, 9], [1, 23, 10], [1, 23, 11], [1, 23, 12], [1, 23, 13], [1, 23, 14], [1, 23, 15], [1, 23, 16], [1, 23, 17], [1, 23, 18], [1, 23, 19], [1, 23, 20], [1, 23, 21], [1, 23, 22], [1, 23, 24], [1, 23, 25], [1, 24, 2], [1, 24, 3], [1, 24, 4], [1, 24, 5], [1, 24, 6], [1, 24, 7], [1, 24, 8], [1, 24, 9], [1, 24, 10], [1, 24, 11], [1, 24, 12], [1, 24, 13], [1, 24, 14], [1, 24, 15], [1, 24, 16], [1, 24, 17], [1, 24, 18], [1, 24, 19], [1, 24, 20], [1, 24, 21], [1, 24, 22], [1, 24, 23], [1, 24, 25], [1, 25, 2], [1, 25, 3], [1, 25, 4], [1, 25, 5], [1, 25, 6], [1, 25, 7], [1, 25, 8], [1, 25, 9], [1, 25, 10], [1, 25, 11], [1, 25, 12], [1, 25, 13], [1, 25, 14], [1, 25, 15], [1, 25, 16], [1, 25, 17], [1, 25, 18], [1, 25, 19], [1, 25, 20], [1, 25, 21], [1, 25, 22], [1, 25, 23], [1, 25, 24] ]; // I had to dramatically shorten the data var temp = []; //console.log(input.length); for (var x = 0; x < input.length; x++) { temp[x] = "[\"" + input[x][0] + "-" + input[x][1] + "-" + input[x][2] + "-\"], "; if (true) { fs.appendFile("output", temp[x], function (err) {}); console.log("HERE - " + temp[x]); } } Any ideas? Could the output indicate a limitation of node.js? Or perhaps it's a file line limitation? Any help would be greatly appreciated.
fs.appendFile is asynchronous, so writes to file might not occur in order you would expect. You can easily fix the problem by using fs.appendFileSync instead of fs.appendFile. Or you could change your code to use asynchronous method in the right way: var fs = require('fs'); var input = []; for (var i = 1; i < 25; i++) for (var j = 1; j < 25; j++) for (var k = 1; k < 25; k++) input.push([i, j, k]); var temp = []; /* Synchronous method */ for (var x = 0; x < input.length; x++) { temp[x] = makeStr(input[x]); fs.appendFileSync("output.txt", temp[x]); console.log("HERE - " + temp[x]); } /* Asynchronous method */ var i = 0, callback = function (err) { i++; if (!err && i < input.length) { temp.push(makeStr(input[i])); fs.appendFile("output.txt", temp[i], callback); } }; temp.push(makeStr(input[i])); fs.appendFile("output.txt", temp[i], callback); function makeStr(item) { return '["' + item[0] + '-' + item[1] + '-' + item[2] + '"]'; }