[英]How can I turn the output into a list in python?
我是searchresult
的新手,只是想知道如何將搜索結果轉換為列表:例如,搜索結果是字符串searchresult
:
1234567
1234566
1234555
如何將其轉換為列表?
searchresult=("" + a['href'][1:-5][-7:])
l=[]
for i in searchresult:
l.append(searchresult())
print (l)
上面的代碼返回一個帶有'str'的結果 object is not callable
您可以使用.split()
另外我假設您希望列表是整數。 為此,您可以使用map
output = "1234567 1234566 1234555"
lst = list(map(int,output.split()))
output
[1234567, 1234566, 1234555]
我已經看到了https://pasteboard.co/K1DJNVk.jpg上的代碼基於鏈接中提供的代碼,我想帶您了解@BuddyBob 所說的內容。
你的代碼如下:
for div in data:
links = div.findAll('a')
for a in links:
print ("" + a['href'][1:-5][-7:])
searchresult=("" + a['href'][1:-5][-7:])
lst=list(map(int,output.split()))
print(lst)
要獲得所需的 output 您可以將代碼更改為以下(我已對更改進行了評論) -
temp=[] # lets declare a temporary array
output=[] # lets declare another array to hold the output data
for div in data:
links = div.findAll('a')
for a in links:
print ("" + a['href'][1:-5][-7:])
searchresult=("" + a['href'][1:-5][-7:])
lst=list(map(int,output.split()))
temp.append(lst) # I have replaced print(lst) with this statement
[[output.append(j) for j in i] for i in temp] # This removes any sublists
print(output) # Voila ! The desired output
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.