Python object.append from for loop - python

I want to append dictionary values to a list from a for loop, though it is only picking up the last value. Please advise, here is my code:
for line in obj:
test = float(line['value'])
print(test)
a = []
a.append(test)

Huh! You have messed up the indentation. Indentation is VERY IMPORTANT in python. a list is outside the for-loop hence it would only have the last value of test. IT should be inside the for-loop.
It should be like this -
a = []
for line in obj:
test = float(line['value'])
print(test)
a.append(test)
print a

Your call to append is outside the loop. Indentation matters in Python.

Related

Problems that will be covered outside the loop

def getDate():
result = open('test1.html', 'r')
data = re.findall(r"\"engine_jds\":(.+?),\"jobid_count\"",str(result.readlines()))
jsonObj = json.loads(data[0])
for item in jsonObj:
var = item["company_name"], item["job_name"], item["workarea_text"], item[
"providesalary_text"], item["companytype_text"], item["companysize_text"], item["jobwelf"]
# print(var)
print(var)
# return var
Output in the loop can output all data without being overwritten, but output outside the loop will be overwritten and only the last data can be output.
I don't know what you mean by "overwritten" here, but you do have one major problem:
... str(result.readlines())
The readlines function returns a list of strings, one for each line. The str() function will return a single string that looks like a Python list, with brackets and quotes:
"['Line one','Line two','Line three']"
You need to decide whether you want to handle the whole file at once (in which case you want result.read()), or if you want to handle the lines one by one, in which case you will need a loop.

Python unexpected behaviour with list.append()

I was working with a machine learning model and I tried to append his output in a list for each element in antoher list.
I used a for loop but it seems like the loop jumps the first index and repeats the last ones:
x = [[0.12], [0.36], [0.48]]
print(model.run(x[0])) #this prints [-0.0006]
print(model.run(x[1])) #this prints [-0.0018]
print(model.run(x[2])) #this prints [-0.0024]
out_values = []
for value in x:
out_values.append(model.run(value))
print(out_values) #this should print [[-0.0012], [-0.0018], [-0.0024]]
# but it actually prints [[-0.0018], [-0.0024], [-0.0024]]
It doesn't seem to be a problem with the model.run() output since the first print statements worked perfectly
It must be something related to out_values.append(), because if I run:
x = [[0.12], [0.36], [0.48]]
out_values = []
out_values.append(model.run(x[0]))
out_values.append(model.run(x[1]))
out_values.append(model.run(x[2]))
print(out_values) # the result I get is still [[-0.0018], [-0.0024], [-0.0024]]
In my opinion it wasn't supposed to jump over model.run(x[0]) and repeat model.run(x[2]) twice
Is this documented or supposed to happen? Or am I doing something wrong?
I believe the issue is that model.run is returning a reference to mutable state within the model which is updated on successive run calls -- that's why there's a difference between what you print immediately after the run and what ends up in the list after additional calls. I.e. the value is correct at the time you call append, but it changes after the fact. When you call model.run(x[2]), it's modifying and returning the list that it returned when you called model.run(x[1]), and at some point it also modifies the list that was returned for x[0]. (This is not good behavior IMO -- if model is from an external library, hopefully it's at least documented in the API that you should not keep references to the return value of run! Otherwise it's just plain diabolical.)
To work around this problem so that you can keep each result as it was originally returned, make a copy of each result as you get it:
out_values = [model.run(value).copy() for value in x]
It will not skip any value or repeat any other value twice. Your model.run must be returning those values only. To remove that doubt, can you update the code to print the return value and print that like so:
for value in x:
ans = model.run(value)
print(ans)
out_values.append(ans)
Can you show output for above?
can you use this code instead?
x = (0.12, 0.36, 0.48)
print(x[0]) #this prints [-0.0006]
print(x[1]) #this prints [-0.0018]
print(x[2]) #this prints [-0.0024]
out_values = []
out_values = set(x)
print(out_values)
it'll include all x in the out_values

Adding key:value items to a dictionay in python with a for

Hi i have this function:
def company_runtime(company_name):
for well in all_wells:
well_runtime = {}
if well["groupName"].lower() == company_name.lower():
well_runtime.update({well["name"]: well["efficiency"]})
return well_runtime
And i want to get a dictionary with "names" and "efficiency" from the loop, if i enter a compay name as a parameter.
I am getting only the last element of the all_wells list that meet that condition.
what i am doing wrong?
Thanks in advance
Each time you go through the loop you are resetting the Dictionary to {} or empty thus it clears it out until the last time on exit. Move well_runtime = {} outside of the loop.

Python3 dictionary values being overwritten

I’m having a problem with a dictionary. I"m using Python3. I’m sure there’s something easy that I’m just not seeing.
I’m reading lines from a file to create a dictionary. The first 3 characters of each line are used as keys (they are unique). From there, I create a list from the information in the rest of the line. Each 4 characters make up a member of the list. Once I’ve created the list, I write to the directory with the list being the value and the first three characters of the line being the key.
The problem is, each time I add a new key:value pair to the dictionary, it seems to overlay (or update) the values in the previously written dictionary entries. The keys are fine, just the values are changed. So, in the end, all of the keys have a value equivalent to the list made from the last line in the file.
I hope this is clear. Any thoughts would be greatly appreciated.
A snippet of the code is below
formatDict = dict()
sectionList = list()
for usableLine in formatFileHandle:
lineLen = len(usableLine)
section = usableLine[:3]
x = 3
sectionList.clear()
while x < lineLen:
sectionList.append(usableLine[x:x+4])
x += 4
formatDict[section] = sectionList
for k, v in formatDict.items():
print ("for key= ", k, "value =", v)
formatFileHandle.close()
You always clear, then append and then insert the same sectionList, that's why it always overwrites the entries - because you told the program it should.
Always remember: In Python assignment never makes a copy!
Simple fix
Just insert a copy:
formatDict[section] = sectionList.copy() # changed here
Instead of inserting a reference:
formatDict[section] = sectionList
Complicated fix
There are lots of things going on and you could make it "better" by using functions for subtasks like the grouping, also files should be opened with with so that the file is closed automatically even if an exception occurs and while loops where the end is known should be avoided.
Personally I would use code like this:
def groups(seq, width):
"""Group a sequence (seq) into width-sized blocks. The last block may be shorter."""
length = len(seq)
for i in range(0, length, width): # range supports a step argument!
yield seq[i:i+width]
# Printing the dictionary could be useful in other places as well -> so
# I also created a function for this.
def print_dict_line_by_line(dct):
"""Print dictionary where each key-value pair is on one line."""
for key, value in dct.items():
print("for key =", key, "value =", value)
def mytask(filename):
formatDict = {}
with open(filename) as formatFileHandle:
# I don't "strip" each line (remove leading and trailing whitespaces/newlines)
# but if you need that you could also use:
# for usableLine in (line.strip() for line in formatFileHandle):
# instead.
for usableLine in formatFileHandle:
section = usableLine[:3]
sectionList = list(groups(usableLine[3:]))
formatDict[section] = sectionList
# upon exiting the "with" scope the file is closed automatically!
print_dict_line_by_line(formatDict)
if __name__ == '__main__':
mytask('insert your filename here')
You could simplify your code here by using a with statement to auto close the file and chunk the remainder of the line into groups of four, avoiding the re-use of a single list.
from itertools import islice
with open('somefile') as fin:
stripped = (line.strip() for line in fin)
format_dict = {
line[:3]: list(iter(lambda it=iter(line[3:]): ''.join(islice(it, 4)), ''))
for line in stripped
}
for key, value in format_dict.items():
print('key=', key, 'value=', value)

.split from a file and putting it in an array

Im reading a file with some information and each part is separated with a # however on each line i want it to be a different array so i did this and im not sure why its not working.
main_file = open("main_file.txt","r")
main_file_info=main_file.readlines()
test=[]
n=0
for line in main_file_info:
test[n]=line.split("#")
test=test[n][1:len(test)-1] # to get rid of empty strings at the start and the end
print(test)# see what comes out
main_file.close()
The way you are inserting the output of line.split("#") in your list is wrong. Your list is not initialized, hence, you can't simply assign anything to any element of the list. So, what you need to do is this :
test.append(line.split("#"))
Or, you can initialize your list as below :
test = [[]]*(len(main_file_info))
test = [None for _ in range(total)]
# instead of test = []
or simply just append to test:
test.append( line.split("#") )

Categories

Resources