0

The code below is for scraping data from webpage.Actually,the output from this code
and output from the another list is to be considered .

list2=[]

###-I am collecting all span tags ,storing as text in variable called alpha.

for i in range(len(contents)):
        for j in contents[i].findAll('span'):
            alpha=j.text
#     print(alpha)
            alphachar=re.sub('[^a-zA-Z]+', '', alpha) #I am eliminating empty lists.
            alphabets=alphachar.split()  #converting to list
            for item in alphabets:
                if item!=[]:
                    list2.append(item)   #I am appending to lists
for (a, b) in zip(li,list2):
            print(a,b)     

The output of the above code is :


AMD AdvancedMicroDevicesInc
BAC BankofAmericaCorp
GE GeneralElectricCo
F FordMotorCo
M MacysInc
PFE PfizerInc
FCX FreeportMcMoRanInc
BMY BristolMyersSquibbCo
T ATTInc
JWN NordstromInc
JWN NordstromInc
M MacysInc
LB LBrandsInc
GPS GapInc
SJM JMSmuckerCo
CPRI CapriHoldingsLtd
RL RalphLaurenCorp
BIIB BiogenInc
FCX FreeportMcMoRanInc
ADS AllianceDataSystemsCorp

Now I have another list called name :

name = allbody.findAll('h3')

Its output is:

Most actives,Gainers

Now,I want the output as:


 - Most actives

AMD AdvancedMicroDevicesInc
BAC BankofAmericaCorp
GE GeneralElectricCo
F FordMotorCo
M MacysInc
PFE PfizerInc
FCX FreeportMcMoRanInc
BMY BristolMyersSquibbCo
T ATTInc
JWN NordstromInc

 - Gainers

JWN NordstromInc
M MacysInc
LB LBrandsInc
GPS GapInc
SJM JMSmuckerCo
CPRI CapriHoldingsLtd
RL RalphLaurenCorp
BIIB BiogenInc
FCX FreeportMcMoRanInc
ADS AllianceDataSystemsCorp

I tried using nested for loop for names and zip function but didnt work out.Can anyone please help out in this?

  • you would need `list2[i:i+10]` to get 10 elements from list. And you have to add 10 to `i` to get next 10 elements. – furas Nov 23 '19 at 02:04

1 Answers1

0

This is situation when range(len()) can be useful to generate i for list2[i:i+10].

Using

for i range(0, len(list2), 10):

you can assign to i values 0, 10, etc. which will create list2[0:10], list2[10:20], etc.

list1 = ['Most actives', 'Gainers']

data = '''AMD AdvancedMicroDevicesInc
BAC BankofAmericaCorp
GE GeneralElectricCo
F FordMotorCo
M MacysInc
PFE PfizerInc
FCX FreeportMcMoRanInc
BMY BristolMyersSquibbCo
T ATTInc
JWN NordstromInc
JWN NordstromInc
M MacysInc
LB LBrandsInc
GPS GapInc
SJM JMSmuckerCo
CPRI CapriHoldingsLtd
RL RalphLaurenCorp
BIIB BiogenInc
FCX FreeportMcMoRanInc
ADS AllianceDataSystemsCorp'''

list2 = data.split('\n')

for name, i in zip(list1, range(0, len(list2), 10)):
    print('\n-', name, '\n')
    for item in list2[i:i+10]:
        print(item)

EDIT: As I know there is no special function for slicing list2 but you can create own

(source: How do you split a list into evenly sized chunks?)

def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]

and then for-loop with zip() will look nicer

def chunks(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]


list1 = ['Most actives', 'Gainers']

data = '''AMD AdvancedMicroDevicesInc
BAC BankofAmericaCorp
GE GeneralElectricCo
F FordMotorCo
M MacysInc
PFE PfizerInc
FCX FreeportMcMoRanInc
BMY BristolMyersSquibbCo
T ATTInc
JWN NordstromInc
JWN NordstromInc
M MacysInc
LB LBrandsInc
GPS GapInc
SJM JMSmuckerCo
CPRI CapriHoldingsLtd
RL RalphLaurenCorp
BIIB BiogenInc
FCX FreeportMcMoRanInc
ADS AllianceDataSystemsCorp'''

list2 = data.split('\n')

for name, items in zip(list1, chunks(list2, 10)):
    print('\n-', name, '\n')
    for item in items:
        print(item)
furas
  • 134,197
  • 12
  • 106
  • 148
  • I added example with function `chunks()` – furas Nov 23 '19 at 02:29
  • you can keep it as tuples - `list2.append( (a, b) )` - and then you may have to use `for a,b in items: print(a, b)` – furas Nov 23 '19 at 02:44
  • But this prints me only the -

    Most Actives

    SBAC SBACommunicationsCorp
    – suchitra nagarajan Nov 23 '19 at 02:57
  • I don't know what you really have in lists and how you use them - ie. in your question wasn't `

    `. Maybe create new question on new page and describe problem with all details. And put full code so we could run it.

    – furas Nov 23 '19 at 02:59
  • https://stackoverflow.com/questions/59004270/i-want-to-display-first-word-from-1st-list-and-display-10-words-from-2nd-list-an--I have posted the entire ques.Can you please let me know if tis could be solved? – suchitra nagarajan Nov 23 '19 at 03:27
  • it seems you used `data.append( (a,b) )` after `for`-loop but you should use it inside `for`-loop - in place of `print(a,b)` – furas Nov 23 '19 at 03:38