Hu, some more troubleshooting has shown. rows=sqldb(sqldb.item.idnum.belongs(idnumList)).select (sqldb.item.idnum,sqldb.item.name) Doesn't perform as I think it was intended in the above code. Instead it returns a row which is simply (item.idnum,item.name). Literally
Fixing it with rows=sqldb(sqldb.item.idnum.belongs(idnumList)).select() Works just fine as now it actually selects all the items of interest. Then with the command from above: maps=dict([str(row.idnum),row.name]for row in rows) *note i happen to be storing these ID's as strings We get a dictionary which I can access by ID-number and returns the item name. I'm thrilled, and I've learned a lot too about both Python and web2py today. On Sep 12, 6:29 pm, Chris S <[email protected]> wrote: > Guess I should have looked a little before asking. You can use an if > statement inside of that comprehension line. So (for anyone who finds > this later) you can just make it. > > new_items=[maps[item] for item in items if maps.has_key(item)] > > Yea! > > On Sep 12, 6:25 pm, Chris S <[email protected]> wrote: > > > > > Oh that's beautiful. One select and I'm set. I've got it up and > > running and it's pointed out a small problem for me. > > > How can I handle and id (int items from your example) that isn't > > present in the table? Simply skipping it would be fine for now. I'll > > see what I can come up with but I'm new to Python (and coding in > > general) so the 'list comprehension' as I think it's called is still a > > bit of a new concept. > > > On Sep 12, 4:20 pm, mdipierro <[email protected]> wrote: > > > > You are making a lot of selects and this may be slow. I suggest > > > > ## input > > > items=[... a list of ids...] > > > > ##table > > > db.define_table('item',Field('name')) > > > > ## algorithm > > > rows=db(db.item.id.belongs(items)).select(db.item.id,db.item.name) > > > maps=dict([(row.id,row.name) for row in rows]) > > > new_items=[maps[item] for item in items] > > > > and now you only have one query > > > > On Sep 12, 4:02 pm, Chris S <[email protected]> wrote: > > > > > Update: > > > > > ---------------- > > > > for idnum in idnumList: > > > > for row in sqldb(sqldb.item.idnum==idnum).select(): > > > > name=row.name > > > > Dictionary['Reagents']=list.replace(idnum,name) > > > > ------------------ > > > > > Is working ... sort of. Thus far I have it replacing one number in > > > > the list I'll look at it more later and post the whole solution when I > > > > figure it out. It should be noted. When working with the Shell to > > > > test script only one Quarry can be made, any further DB quarry's > > > > result in an error until you reload the shell! > > > > > On Sep 12, 1:12 pm, Chris S <[email protected]> wrote: > > > > > > I'm trying to go through a list of dictionary's and replace one of the > > > > > key's fields (an idnumber) with another field from a table. The code > > > > > currently looks like: > > > > > > -------------- > > > > > regex = re.compile(r'\[(\d+)\,') #Match's my item numbers > > > > > for Dictionary in DictList: > > > > > list = Dictionary['Reagents'] > > > > > idnumList = regex.findall(list) > > > > > ---------------- > > > > > > This gives me a nice list (idnumList) with id numbers in it. However > > > > > I can't seem to find a way to take each of these idnumbers, look them > > > > > up in a table and return a different field (name). > > > > > > I've been trying: > > > > > > ---------------- > > > > > for idnum in idnumList: > > > > > item=sqldb(sqldb.item.idnum==idnum.isdigit()).select() > > > > > Dictionary['Reagents']=list.replace(idnum,item.name) > > > > > --------------- > > > > > > But I'm told item.name isn't a valid field. Am I going about this the > > > > > right way, can someone point me in the right direction? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---

