encapsulation in python list (want to use " instead of ')

Posted by Codehai on Stack Overflow See other posts from Stack Overflow or by Codehai
Published on 2013-10-17T21:05:23Z Indexed on 2013/10/17 21:54 UTC
Read the original article Hit count: 195

Filed under:
|
|

I have a list of users users["pirates"] and they're stored in the format ['pirate1','pirate2']. If I hand the list over to a def and query for it in MongoDB, it returns data based on the first index (e.g. pirate1) only. If I hand over a list in the format ["pirate1","pirate"], it returns data based on all the elements in the list. So I think there's something wrong with the encapsulation of the elements in the list. My question: can I change the encapsulation from ' to " without replacing every ' on every element with a loop manually?

Short Example:

aList = list()
# get pirate Stuff
# users["pirates"] is a list returned by a former query
# so e.g. users["pirates"][0] may be peter without any quotes
for pirate in users["pirates"]:
    aList.append(pirate)
    aVar = pirateDef(aList)
print(aVar)

the definition:

def pirateDef(inputList = list()):
# prepare query 
col = mongoConnect().MYCOL


# query for pirates Arrrr
pirates = col.find({ "_id" : {"$in" : inputList}}
                      ).sort("_id",1).limit(50)

# loop over users
userList = list()
for person in pirates:
    # do stuff that has nothing to do with the problem

       # append user to userlist
    userList.append(person)

return userList  

If the given list has ' encapsulation it returns:

'pirates': [{'pirate': 'Arrr', '_id': 'blabla'}]

If capsulated with " it returns:

'pirates' : [{'_id': 'blabla', 'pirate' : 'Arrr'}, {'_id': 'blabla2', 'pirate' : 'cheers'}]

EDIT: I tried figuring out, that the problem has to be in the MongoDB query. The list is handed over to the Def correctly, but after querying pirates only consists of 1 element...

Thanks for helping me

Codehai

© Stack Overflow or respective owner

Related posts about python

Related posts about list