简体   繁体   中英

How can I turn the output into a list in python?

I am new to python, just wonder how can I turn searchresult into a list: for example, the searchresult is string output:

1234567
1234566
1234555

How can I convert it into a list?

   searchresult=("" + a['href'][1:-5][-7:])
    l=[]
    for i in searchresult:
        l.append(searchresult())
        print (l)

The above code returns a result with 'str' object is not callable

You can use .split() Also I assume you want the list to be of integers. For that you can use map

output = "1234567 1234566 1234555"
lst = list(map(int,output.split()))

output

[1234567, 1234566, 1234555]

I've seen the code on https://pasteboard.co/K1DJNVk.jpg Based on the code provided in the link I'd like to walk you through what @BuddyBob said.

your code is as follows:

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)

To get the desired output you can change the code to following (I have commented the changes) -

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
 

   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM