Appending in Python 3 issue - list

This is the part of the code that gives me a hard time:
if (id1 == id2):
idlist.append[id1]
The error:
builtins.TypeError: 'builtin_function_or_method' object is not subscriptable
the problematic line is "idlist.append[id1]"
Any idea why ? I am trying to append the value id1 hold into idlist.
Thanks !!

Lists have a method that is called append
Because it is a method you should use it like that:
.append(something)
ie. invoke (like any other func)

Related

Converting "Textarea" object from iPython wigdet to a list or iterable array

I have created several Textarea widgets in Jupyter/Python in order to capture some string inputs.
In the highlighted in yellow that you can see below, the idea is that the user puts a list of numbers here (copied from Excel) and later I need to convert this text into a list or an array that contains these numbers (an iterable object). I have no idea how to do this. See:
When I print the type of this object that is called "plus" I get this:
print(type(plus))
<class 'ipywidgets.widgets.widget_string.Textarea'>
But, I am expecting to have something like this:
plus = [454, 555]
Can I bounce some ideas off you to get this?
Thanks a lot!!!
If you have an ipywidget in general, you can observe its change and get its value as following.
foo = widgets.Textarea()
# to get the value
foo.value
# to do something on value change
def bar(change):
print(change.new)
foo.observe(bar, names=['value'])
You will then have to format the string you get from the products value, but that shouldn't be too difficult.
Hope this helps

functools.partial raising TypeError "got multiple values for keyword argument" Python

I try to efficiently iterate over many strings and there is a repeated part of the strings I would like to insert with a partial function
def add_seqs(seqs_outer,sequence):
return seqs_outer[0]+sequence+seqs_outer[1]
def my_function(string,start,stop,list_variable):
seqs_pre=string[:start]
seqs_post=string[stop:]
seqs_outer=(seqs_pre,seqs_post)
seqs_out=map(functools.partial(add_seqs,seqs_outer=seqs_outer),list_variable)
return seqs_out
I finally want to use an apply function over many different strings with a fixed list_variable, but I get an error in my_function:
TypeError: my_function() got multiple values for keyword argument 'seqs_outer'
I guess I do something wrong in the use of partial - how can I make the above code work?
Okay, inspired by this thread I swapped the order of arguments in
def add_seqs(seqs_outer,sequence)
to
def add_seqs(sequence,seqs_outer)
this did the job

How can I calculate mean of list of strings?

I trying to calculate mean of one colum in a csv file.First, I read one column from .csv file and save it into a list. Next when I try to get mean it have a error
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
my code is :
with open('XXXXXX.csv') as f:
reader = csv.DictReader(f)
for row in reader:
for (k,v) in row.items():
columns_95[k].append(v)
sVaR5 = columns_95['95%']
mean_95 = sum(sVaR5)/len(sVaR5)
and my csv looks like:
95% 99%
1.225 2.332
1.252 10.252
2.336 4.213
... ...
when I check my list, output is['1.225','1.252','2.336'] I think maybe the quote mark is the reason why my code has error. but how to fix it!Thanks!!!
sum is a function. If you want to call the function sum with the argument sVaR5, you need to write:
sum(sVaR5)
If your sVaR5 is a list of strings, you could convert them to floats for the sum:
sum(map(float, sVaR5))
If you put sum[sVaR5], Python tries to call __getitem__ on the object sum, hence the error
'builtin_function_or_method' object has no attribute '__getitem__'

TypeError during executemany() INSERT statement using a list of strings

I am trying to just do a basic INSERT operation to a PostgreSQL database through Python via the Psycopg2 module. I have read a great many of the questions already posted regarding this subject as well as the documentation but I seem to have done something uniquely wrong and none of the fixes seem to work for my code.
#API CALL + JSON decoding here
x = 0
for item in ulist:
idValue = list['members'][x]['name']
activeUsers.append(str(idValue))
x += 1
dbShell.executemany("""INSERT INTO slickusers (username) VALUES (%s)""", activeUsers
)
The loop creates a list of strings that looks like this when printed:
['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo']
I am just trying to have the code INSERT these strings as 1 row each into the table.
The error specified when running is:
TypeError: not all arguments converted during string formatting
I tried changing the INSERT to:
dbShell.executemany("INSERT INTO slackusers (username) VALUES (%s)", (activeUsers,) )
But that seems like it's merely treating the entire list as a single string as it yields:
psycopg2.DataError: value too long for type character varying(30)
What am I missing?
First in the code you pasted:
x = 0
for item in ulist:
idValue = list['members'][x]['name']
activeUsers.append(str(idValue))
x += 1
Is not the right way to accomplish what you are trying to do.
first list is a reserved word in python and you shouldn't use it as a variable name. I am assuming you meant ulist.
if you really need access to the index of an item in python you can use enumerate:
for x, item in enumerate(ulist):
but, the best way to do what you are trying to do is something like
for item in ulist: # or list['members'] Your example is kinda broken here
activeUsers.append(str(item['name']))
Your first try was:
['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo']
Your second attempt was:
(['b2ong', 'dune', 'drble', 'drars', 'feman', 'got', 'urbo'], )
What I think you want is:
[['b2ong'], ['dune'], ['drble'], ['drars'], ['feman'], ['got'], ['urbo']]
You could get this many ways:
dbShell.executemany("INSERT INTO slackusers (username) VALUES (%s)", [ [a] for a in activeUsers] )
or event better:
for item in ulist: # or list['members'] Your example is kinda broken here
activeUsers.append([str(item['name'])])
dbShell.executemany("""INSERT INTO slickusers (username) VALUES (%s)""", activeUsers)

Why is this freemarker code failing when there is a comma in the list?

I have a map that contains a list (all the values in the list are strings):
["diameter":["1", "2", "3"]]
["length":["2", "3", "4"]]
I iterate through it in freemarker:
<#list product.getSortedVariantMap.keySet() as variantCode>
<#list product.getSortedVariantMap[variantCode] as variantValue>
This works fine. However if one of the strings contains a comma like this:
def returnValue = ["diameter":["3,5"]]
I get the following error:
?size is unsupported for: freemarker.ext.beans.SimpleMethodModel
The problematic instruction:
----------
==> list product.getSortedVariantMap[variantCode] as variantValue [on line 200, column 41 in product.htm]
I have no idea what the error could be, a comma in a string shouldn't create that error.
It depends on FreeMarker configuration, but product.getSortedVariantMap most probably returns the method itself, not its return value. You should write product.sortedVariantMap. (Although I don't understand why it doesn't stop earlier, on product.getSortedVariantMap.keySet(). Maybe your example is not exactly what to run?)