BinarySearch modification



嗨,我正在修改一个二进制搜索,以便计算url中'/'的数量,并根据该数量和pi图创建分数。然而,当我试图通过字符串迭代时,我遇到了一个错误。如果你能帮我找到一个更简单的方法来排序列表中的url,而不是对每个url进行手动排序,然后将其组合在一个列表中,那将是超级棒的。谢谢你!

import numpy as np
url1 = "https://diversity.google"
url2 = "https://www.aboutamazon.com/workplace/diversity-inclusion"
url3 = "https://www.indeed.com/q-Diversity-jobs.html?vjk=ba073b4704d48c67"
url4 = "https://careers.linkedin.com/diversity-and-inclusion"
url5 = "https://github.com/about/diversity"
url6 = "https://www.apple.com/diversity/"
url7 ="https://www.samsung.com/us/about-us/diversity-and-inclusion/"
url8 = "https://diversity.fb.com"
url9 ="instagram:none"
url10 = "https://careers.twitter.com/en/diversity.html"

#for some reason doing this in a list doesnt work
url1 = sorted(url1)
url1 = "".join(url1)
url2 = sorted(url2)
url2 = "".join(url2)
url3 = sorted(url3)
url3 = "".join(url3)
url4 = sorted(url4)
url4 = "".join(url4)
url5 = sorted(url5)
url5 = "".join(url5)
url6 = sorted(url6)
url6 = "".join(url6)
url7 = sorted(url7)
url7 = "".join(url7)
url8 = sorted(url8)
url8 = "".join(url8)
url9 = sorted(url9)
url9 = "".join(url9)
url10 = sorted(url10)
url10 = "".join(url10)


data = np.array([url1,url2,url3,url4,url5,url6,url7,url8,url9,url10])
#for i in data:
#data[i] = sorted(data[i])
#data[i] = "".join(data[i])
def BinaryMod(data, low, high, x, counterArray):

mid = (high+low) / 2
for i in data:
for j in len(data[i]):
word = j
high = len(word) - 1
if word[mid] > x:
end = mid - 1

elif word[mid] < x:
low = mid+1
else:
counter = counter + 1
counterArray.append(counter)
return counterArray
counterArray = np.array([])
counterArray = BinaryMod(data, 0, 0,  '/', counterArray)

IndexError                                Traceback (most recent call last)
<ipython-input-29-1293e7cb075f> in <module>()
1 counterArray = np.array([])
2 
----> 3 counterArray = BinaryMod(data, 0, 0,  '/', counterArray)
<ipython-input-28-4fe8d062745d> in BinaryMod(data, low, high, x, counterArray)
62   mid = (high+low) / 2
63   for i in data:
---> 64     for j in len(data[i]):
65       word = j
66       high = len(word) - 1
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
import numpy as np
urls = [
"https://diversity.google",
"https://www.aboutamazon.com/workplace/diversity-inclusion",
"https://www.indeed.com/q-Diversity-jobs.html?vjk=ba073b4704d48c67",
"https://careers.linkedin.com/diversity-and-inclusion",
"https://github.com/about/diversity",
"https://www.apple.com/diversity/",
"https://www.samsung.com/us/about-us/diversity-and-inclusion/",
"https://diversity.fb.com",
"instagram:none",
"https://careers.twitter.com/en/diversity.html",
]

for url in urls:
print( url, 'contains', url.count('/'), 'slashes' )

输出:

https://diversity.google contains 2 slashes
https://www.aboutamazon.com/workplace/diversity-inclusion contains 4 slashes
https://www.indeed.com/q-Diversity-jobs.html?vjk=ba073b4704d48c67 contains 3 slashes
https://careers.linkedin.com/diversity-and-inclusion contains 3 slashes
https://github.com/about/diversity contains 4 slashes
https://www.apple.com/diversity/ contains 4 slashes
https://www.samsung.com/us/about-us/diversity-and-inclusion/ contains 6 slashes
https://diversity.fb.com contains 2 slashes
instagram:none contains 0 slashes
https://careers.twitter.com/en/diversity.html contains 4 slashes

最新更新