SecondLemon
SecondLemon

Reputation: 1003

Merging lists obtained by a loop

I've only started python recently but am stuck on a problem.

# function that tells how to read the urls and how to process the data the
# way I need it.


def htmlreader(i):
    # makes variable websites because it is used in a loop.
    pricedata = urllib2.urlopen(
        "http://website.com/" + (",".join(priceids.split(",")[i:i + 200]))).read()

    # here my information processing begins but that is fine.
    pricewebstring = pricedata.split("},{")
    # results in [[1234,2345,3456],[3456,4567,5678]] for example.
    array1 = [re.findall(r"\d+", a) for a in pricewebstring]

    # writes obtained array to my text file
    itemtxt2.write(str(array1) + '\n')

i = 0
while i <= totalitemnumber:
    htmlreader(i)
    i = i + 200

See the comments in the script as well.

This is in a loop and will each time give me an array (defined by array1).

Because I print this to a txt file it results in a txt file with separate arrays. I need one big array so it needs to merge the results of htmlreader(i).

So my output is something like:

[[1234,2345,3456],[3456,4567,5678]]
[[6789,4567,2345],[3565,1234,2345]]

But I want:

[[1234,2345,3456],[3456,4567,5678],[6789,4567,2345],[3565,1234,2345]]

Any ideas how I can approach this?

Upvotes: 0

Views: 57

Answers (1)

thefourtheye
thefourtheye

Reputation: 239443

Since you want to gather all the elements in a single list, you can simply gather them in another list, by flattening it like this

def htmlreader(i, result):
    ...
    result.extend([re.findall(r"\d+", a) for a in pricewebstring])

i, result = 0, []
while i <= totalitemnumber:
    htmlreader(i, result)
    i = i + 200

itemtxt2.write(str(result) + '\n')

In this case, the result created by re.findall (a list) is added to the result list. Finally, you are writing the entire list as a whole to the file.

If the above shown method is confusing, then change it like this

def htmlreader(i):
    ...
    return [re.findall(r"\d+", a) for a in pricewebstring]

i, result = 0, []
while i <= totalitemnumber:
    result.extend(htmlreader(i))
    i = i + 200

Upvotes: 2

Related Questions