I am trying to append an element to a list within a list that has an incremented value each time:
def get_data(file):
matrix = [ ['one','two','three'] ] #list of lists
test_count = 0
line_count = 0 #keep track of which line we are on
for line in file:
if line.find('example') != -1: #test for example string
temp_a = re.findall(r"\'(.+?)\'",line)[0]
print matrix[test_count][0] #should print 'one'
matrix[test_count][0].insert(temp_a) #should insert temp_a instead of 'one'
test_count += 1 #go to next "new" list in the matrix
line_count += 1 #go to next line
What I want is the result of findall to go into temp_a and from there to insert it into index 0 of the first list within a list. Then the next time findall is true, I want to insert temp_a to index 0 of the second list.
For example if the first temp_a value is 9, I would like the first list in the matrix to be:
[ [9,y,z] ]
If on the second findall my temp_a is 4, I want the matrix to become:
[ [9,y,z], [4,y,z] ]
The above code is my best attempt so far.
I have 2 questions:
1) How can I initialize a 'list of lists' if the amount of lists isn't fixed?
2) The list ['one','two','three'] was to test with printing what is going on. If I try to print out matrix[test_count][0], I get an "index out of range" error, but the moment I change it to print out matrix[0][0] it prints 'one' correctly. Is there something with the scope that I'm missing here?
To answer your questions:
1) Like this: matrix = []
Simply put, this just creates an empty list that you can append anything you want into, including more lists. So matrix.append([1,2,3]) gives you a list like this: [[1,2,3]]
2) So you're index out of range error is coming from the fact that you're incrementing test_count to 1 but your matrix is remaining length of 1 (meaning it only has the 0 index) since you never append anything. In order to get the output that you want you're going to need to make a few changes:
def get_data(file):
example_list = ['one','two','three']
matrix = [] #list of lists
test_count = 0
line_count = 0 #keep track of which line we are on
for line in file:
if line.find('example') != -1: #test for example string
temp_a = re.findall(r"\'(.+?)\'",line)[0]
new_list = example_list[:]
new_list[0] = temp_a
matrix.append(new_list)
test_count += 1 #go to next "new" list in the matrix
line_count += 1 #go to next line
print matrix #[['boxes', 'two', 'three'], ['equilateral', 'two', 'three'], ['sphere', 'two', 'three']]
For 2), did you try to print out test_count? Since your test_count+=1 is in if statement, it shouldn't be out of range without printing "one".
For 1), you could do this before insert:
if test_count == len(matrix):
matrix.append([])
It adds a new empty list if test_count of out range of matrix.
EDIT:
"Out of range" caused by line temp_a = re.findall(r"\'(.+?)\'",line)[0] because it can't find anything. So it's an empty list, and [0] out of range.
def get_data(file):
matrix = [ ['one','two','three'] ] #list of lists
test_count = 0
line_count = 0 #keep track of which line we are on
for line in file:
if line.find('example') != -1: #test for example string
temp_a = re.findall(r"\'(.+?)\'",line)
if temp_a:
temp_a = temp_a[0]
else:
continue # do something if not found
print(matrix[test_count][0]) #should print 'one'
new_list = matrix[test_count][:]
new_list[0] = temp_a
matrix[test_count].append(new_list) #should insert temp_a instead of 'one'
test_count += 1 #go to next "new" list in the matrix
line_count += 1 #go to next line
Related
I have a list of zeros and ones.
I am trying to replace the a value of 1 with a 0 if the previous value is also a 1 for a desired output as shown below.
list = [1,1,1,0,0,0,1,0,1,1,0]
new_list = [1,0,0,0,0,0,1,0,1,0,0]
I've tried using a for loop to no avail. Any suggestions?
How about this for loop:
list = [1,1,1,0,0,0,1,0,1,1,0]
new_list = []
ant=0
for i in list:
if ant ==0 and i==1:
new_list.append(1)
else:
new_list.append(0)
ant=i
question_list = [1,1,1,0,0,0,1,0,1,1,0]
new_list = [question_list[0]] # notice we put the first element here
for i in range(1, len(question_list) + 1):
# check if the current and previous element are 1
if question_list[i] == 1 and question_list[i - 1] == 1:
new_list.append(0)
else:
new_list.append(question_list[i])
The idea here is we iterate over the list, while checking the previous element.
Compare each element of a list in existing order with elements of a second list in existing order as long as items in lists are equal. Stop if they're not equal and give me as result the index and name of the last match.
I thought it's straightforward with a while loop but it seems like this has to be approached with a for-loop.
My List one I want to compare:
nk_script_file_path
['P:', 'Projects', '2019_projects', '1910_My_Project', '01_Production_IN', '01_OFX', '01_Comp', '00_Nuke', 'relink_test_v001.nk']
My second list I want to compare it to:
node_filepath
['P:', 'Projects', '2019_projects', '1910_My_Project', '02_Production_OUT', '01_OFX', '01_Comp', '00_Nuke', '040_ALY', '040_ALY_040_HROTERRORBLADE', '040_ALY_040_HROTERRORBLADE_prev_Gamma22_apcs_mov', '040_ALY_040_HROTERRORBLADE_prev_v14_Gamma22_apcs.mov']
What I've tried
nk_script_file_path = r"P:/Projects/2019_projects/1910_My_Project/01_Production_IN/01_OFX/01_Comp/00_SO/relink_test_v001.nk".split("/")
node_filepath = r"P:/Projects/2019_projects/1910_My_Project/02_Production_OUT/01_OFX/01_Comp/00_S=/040_ALY/040_ALY_040_HROTERRORBLADE/040_ALY_040_HROTERRORBLADE_prev_Gamma22_apcs_mov/040_ALY_040_HROTERRORBLADE_prev_v14_Gamma22_apcs.mov".split("/")
# Compare file paths
path_object = 0
while nk_script_file_path in node_filepath:
path_object += 1
print path_object
print node_filepath[path_object]
Result I'm looking for:
"3"
or
"1910_My_Project"
You can use zip() with enumerate() to find first index where's difference. In this example if no difference is found, value of i is equal to -1:
lst1 = ['P:', 'Projects', '2019_projects', '1910_My_Project', '01_Production_IN', '01_OFX', '01_Comp', '00_Nuke', 'relink_test_v001.nk']
lst2 = ['P:', 'Projects', '2019_projects', '1910_My_Project', '02_Production_OUT', '01_OFX', '01_Comp', '00_Nuke', '040_ALY', '040_ALY_040_HROTERRORBLADE', '040_ALY_040_HROTERRORBLADE_prev_Gamma22_apcs_mov', '040_ALY_040_HROTERRORBLADE_prev_v14_Gamma22_apcs.mov']
for i, (a, b) in enumerate(zip(lst1, lst2)):
if a != b:
break
else:
i = -1
print('First difference is at index:', i)
Prints:
First difference is at index: 4
nk_script_file_path= r"P:/Projects/2019_projects/1910_My_Project/01_Production_IN/01_OFX/01_Comp/00_SO/relink_test_v001.nk".split("/")
node_filepath = r"P:/Projects/2019_projects/1910_My_Project/02_Production_OUT/01_OFX/01_Comp/00_S=/040_ALY/040_ALY_040_HROTERRORBLADE/040_ALY_040_HROTERRORBLADE_prev_Gamma22_apcs_mov/040_ALY_040_HROTERRORBLADE_prev_v14_Gamma22_apcs.mov".split("/")
j = 0
for i in nk_script_file_path:
if i != node_filepath[j] :
j = j-1
break
else:
j += 1
print(nk_script_file_path[j])
print(j)
I can print the following list of lists fine, but when I append to an empty list, it skips the last on each iteration or gives me an index out of range error when I add one more.
This works:
ordered_results = []
temp = []
A = len(results[1])-2
i = 1
while i < len(results):
x = 0
y = 1
while x < A:
temp = [results[i][0], results[0][x], results[i][y]]
print(temp)
x+=1
y+=1
temp = [results[i][0], results[0][x], results[i][y]]
print(temp)
i+=1
ordered_results
Note: len(results[0]) = 240 and len(results[1] = 241
If you replace "print" with ordered_results.append(temp) it skips:
results[i][0], results[0][239], results[i][240]
each iteration.
(Note the code was expanded as I am messing around trying to figure this out, it was more compact before).
So I've written a bit of code to stack integers in a list from the zeroth position. For some reason I cannot decipher, the while loop below is not being processed. I have followed all good style and syntax requirements that I know, and the while loop works when run by itself.
def row(line):
"""
Function that merges a single row or column.
"""
result_length = len(line)
print result_length
# Create a list of zeros the same length as the 'line' argument
pts_alloc = 0
dummy = 0
result = line
result[0:] = [pts_alloc for dummy in range(len(result))]
print result
#Iterate over the 'line' list looking for non-zero entries and
#stack them from 'result[0]'
line_count = 0
result_place = 0
while (line_count <= (len(line)-1)):
if (line[line_count] > 0):
result[result_place] = line[line_count]
print result
result_place += 1
line_count += 1
return result
print row([4, 0, 0, 5])
Is there a major error in this code that I've missed? Is there some syntax requirement that I am unaware of?
The problems seems to be this part:
result = line
result[0:] = [pts_alloc for dummy in range(len(result))]
By replacing a slice of result, with result = line, you are replacing that same slice in line, too, as result is just another reference to the same list, not a copy.
Since the slice is the entire list, anyway, just do:
result = [pts_alloc for dummy in range(len(result))]
Also, you are declaring a lot of unnecessary variables. You could shorten your code to this:
def row(line):
result = [0] * len(line)
result_place = 0
for x in line:
if x > 0:
result[result_place] = x
result_place += 1
return result
Or even this:
def row(line):
non_zero = [x for x in line if x > 0] # take non-zero values
return non_zero + [0] * (len(line) - len(non_zero)) # pad with zeros
My goals is to have a list of lists, where each item in the outer list contains a word in it's first index, and the number of times it has come across it in the second index. As an example, it should look like this:
[["test1",0],["test2",4],["test3",8]]
The only issue is that when I try to, for instance, access the word "test1" from the first inner-list, I get an index out of range error. Here is my code for how I am attempting to do this:
stemmedList = [[]]
f = open(a_document_name, 'r')
#read each line of file
fileLines = f.readlines()
for fileLine in fileLines:
#here we end up with stopList, a list of words
thisReview = Hw1.read_line(fileLine)['text']
tokenList = Hw1.tokenize(thisReview)
stopList = Hw1.stopword(tokenList)
#for each word in stoplist, compare to all terms in return list to
#see if it exists, if it does add one to its second parameter, else
#add it to the list as ["word", 0]
for word in stopList:
#if list not empty
if not len(unStemmedList) == 1: #for some reason I have to do this to see if list is empty, I'm assuming when it's empty it returns a length of 1 since I'm initializing it as a list of lists??
print "List not empty."
for innerList in unStemmedList:
if innerList[0] == word:
print "Adding 1 to [" + word + ", " + str(innerList[1]) + "]"
innerList[1] = (innerList[1] + 1)
else:
print "Adding [" + word + ", 0]"
unStemmedList.append([word, 0])
else:
print "List empty."
unStemmedList.append([word, 0])
print unStemmedList[len(unStemmedList)-1]
return stemmedList
The final output ends up being:
List is empty.
["test1",0]
List not empty"
Crash with list index out of range error which points to the line if innerList[0] == word
You have a = [[]]
Now, when you are appending to this list after encountering first word, you have
a = [ [], ['test', 0] ]
In the next iteration you are accessing the 0th element of an empty list which doesn't exist.
Assuming that stemmedList and unStemmedList are similar
stemmedList = [[]]
you have an empty list in your list of lists, it has no [0]. Instead just initialize it to:
stemmedList = []
Isn't this simpler?
counts = dict()
def plus1(key):
if key in counts:
counts[key] += 1
else:
counts[key] = 1
stoplist = "t1 t2 t1 t3 t1 t1 t2".split()
for word in stoplist:
plus1(word)
counts
{'t2': 2, 't3': 1, 't1': 4}