Py学习  »  Python

我想显示第一个列表中的第一个单词,显示第二个列表中的10个单词,依此类推python

suchi • 5 年前 • 1737 次点击  

下面的代码是用来从网页上抓取数据的。实际上,这个代码的输出
另一个列表的输出将被考虑。

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)     

上述代码的输出为:


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

现在我有了另一个名为name的列表:

name = allbody.findAll('h3')

其输出为:

Most actives,Gainers

现在,我希望输出为:


 - 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

我试着用嵌套for循环来命名和压缩函数,但没有成功。有人能帮忙吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50194
 
1737 次点击  
文章 [ 1 ]  |  最新文章 5 年前
furas
Reply   •   1 楼
furas    5 年前

在这种情况下 range(len()) 有助于生成 i 对于 list2[i:i+10] .

使用

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

您可以分配给 价值观 0 , 10 等,这将创造 list2[0:10] , list2[10:20] 等等。

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)

编辑: 我知道切片没有特殊功能 list2 但你可以创造自己的

(来源: 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]

然后 for -循环 zip() 会看起来更好

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)