我的数据库包含大量酒店的集合(大约121000家)。你知道吗
我的收藏是这样的:
{
"_id" : ObjectId("57bd5108f4733211b61217fa"),
"autoid" : 1,
"parentid" : "P01982.01982.110601173548.N2C5",
"companyname" : "Sheldan Holiday Home",
"latitude" : 34.169552,
"longitude" : 77.579315,
"state" : "JAMMU AND KASHMIR",
"city" : "LEH Ladakh",
"pincode" : 194101,
"phone_search" : "9419179870|253013",
"address" : "Sheldan Holiday Home|Changspa|Leh Ladakh-194101|LEH Ladakh|JAMMU AND KASHMIR",
"email" : "",
"website" : "",
"national_catidlineage_search" : "/10255012/|/10255031/|/10255037/|/10238369/|/10238380/|/10238373/",
"area" : "Leh Ladakh",
"data_city" : "Leh Ladakh"
}
每个文档可以有一个或多个电话号码,电话号码之间用“|”分隔符分隔。你知道吗
我得把电话号码相同的文件放在一起。你知道吗
我所说的实时,是指当用户打开一个特定的酒店在web界面上查看其详细信息时,我应该能够显示所有与之链接的酒店,这些酒店按普通电话号码分组。你知道吗
在分组时,如果一家酒店链接到另一家酒店,而该酒店链接到另一家酒店,则所有3家酒店都应分组在一起。你知道吗
Example : Hotel A has phone numbers 1|2, B has phone numbers 3|4 and C has phone numbers 2|3, then A, B and C should be grouped together.
from pymongo import MongoClient
from pprint import pprint #Pretty print
import re #for regex
#import unicodedata
client = MongoClient()
cLen = 0
cLenAll = 0
flag = 0
countA = 0
countB = 0
list = []
allHotels = []
conContact = []
conId = []
hotelTotal = []
splitListAll = []
contactChk = []
#We'll be passing the value later as parameter via a function call
#hId = 37443;
regx = re.compile("^Vivanta", re.IGNORECASE)
#Connection
db = client.hotel
collection = db.hotelData
#Finding hotels wrt search input
for post in collection.find({"companyname":regx}):
list.append(post)
#Copying all hotels in a list
for post1 in collection.find():
allHotels.append(post1)
hotelIndex = 11 #Index of hotel selected from search result
conIndex = hotelIndex
x = list[hotelIndex]["companyname"] #Name of selected hotel
y = list[hotelIndex]["phone_search"] #Phone numbers of selected hotel
try:
splitList = y.split("|") #Splitting of phone numbers and storing in a list 'splitList'
except:
splitList = y
print "Contact details of",x,":"
#Printing all contacts...
for contact in splitList:
print contact
conContact.extend(contact)
cLen = cLen+1
print "No. of contacts in",x,"=",cLen
for i in allHotels:
yAll = allHotels[countA]["phone_search"]
try:
splitListAll.append(yAll.split("|"))
countA = countA+1
except:
splitListAll.append(yAll)
countA = countA + 1
# print splitListAll
#count = 0
#This block has errors
#Add code to stop when no new links occur and optimize the outer for loop
#for j in allHotels:
for contactAll in splitListAll:
if contactAll in conContact:
conContact.extend(contactAll)
# contactChk = contactAll
# if (set(conContact) & set(contactChk)):
# conContact = contactChk
# contactChk[:] = [] #drop contactChk list
conId = allHotels[countB]["autoid"]
countB = countB+1
print "Printing the list of connected hotels..."
for final in collection.find({"autoid":conId}):
print final
这是我用Python写的一段代码。在这个例子中,我尝试在for循环中执行线性搜索。我现在得到一些错误,但它应该工作时纠正。你知道吗
我需要一个优化的版本,因为线性搜索的时间复杂度很低。你知道吗
我是一个非常新的,所以任何其他建议,以改善代码是欢迎的。你知道吗
谢谢。你知道吗
对于任何Python内存搜索问题,最简单的答案是“使用dict”。Dicts给出O(ln)键访问速度,list给出O(N)。你知道吗
还要记住,您可以将一个Python对象放入尽可能多的dict(或list)中,也可以在一个dict或list中放入尽可能多的次数。它们不是复制的。只是个参考。你知道吗
所以必需品看起来像
在这个循环的末尾,
hotelsbyphone["123456"]
将是一个hotel对象的列表,这些对象的phone_search
字符串之一是“123456”。键编码特性是.setdefault(key, [])
方法,如果该键不在dict中,它将初始化一个空列表,这样您就可以附加到它。你知道吗一旦你建立了这个索引,这将很快
除了
try ... except
,测试if x in hotelsbyphone:
相关问题 更多 >
编程相关推荐