如何让我的python代码不那么笨拙?(处理if/elif语句和panda)

gg0vcinb  于 2022-11-27  发布在  Python
关注(0)|答案(3)|浏览(162)

我编写了一个函数,它在输入一个列表后生成一个表。
这是我正在做的网页抓取脚本的一部分。
功能工作(不是最好的,但足以达到其目的),但有没有更好的方法来实现更好/类似/相同的结果?
例如,下面是一个列表,我希望将其转换为表格:

listings = 
["Search Result", "Advanced Search", "Item Trader Location Price Last Seen", "Sealed Blacksmithing Writ", "Rewards 356 Vouchers", 
"Level 1", "@rscus2001", "Shadowfen: Stormhold", "Ghost Sea Trading Co", "71,200", "X", "1", "=", "71,200 3 Hour ago", "Sealed Blacksmithing Writ", "Rewards 328 Vouchers", 
"Level 1", "@Deirdre531", "Grahtwood: Elden Root", "piston", "100,000", "X", "1", "=", "100,000 6 Hour ago", "Sealed Blacksmithing Writ", "Rewards 328 Vouchers", 
"Level 1", "@Araxas", "Luminous Legion", "100,000", "X", "1", "=", "100,000 9 Hour ago", "Sealed Blacksmithing Writ", "Rewards 356 Vouchers", 
"Level 1", "@CaffeinatedMayhem", "Craglorn: Belkarth", "Masser's Merchants", "25,000", "X", "1", "=", "25,000 13 Hour ago", "Sealed Blacksmithing Writ", "Rewards 287 Vouchers", 
"Level 1", "@Gregori_Weissteufel", "Wrothgar: Morkul Stronghold", "The Cutthroat Mutineers", "45,000", "X", "1", "=", "45,000 13 Hour ago", "<", "1", ">"]

结果:

0                     1        2                     3                            4                        5        6  7  8                   9                   10
0  Sealed Blacksmithing Writ  Rewards 356 Vouchers  Level 1            @rscus2001         Shadowfen: Stormhold     Ghost Sea Trading Co   71,200  X  1                   =   71,200 3 Hour ago
1  Sealed Blacksmithing Writ  Rewards 328 Vouchers  Level 1           @Deirdre531        Grahtwood: Elden Root                   piston  100,000  X  1                   =  100,000 6 Hour ago
2  Sealed Blacksmithing Writ  Rewards 328 Vouchers  Level 1               @Araxas              Luminous Legion                  100,000        X  1  =  100,000 9 Hour ago                None
3  Sealed Blacksmithing Writ  Rewards 356 Vouchers  Level 1    @CaffeinatedMayhem           Craglorn: Belkarth       Masser's Merchants   25,000  X  1                   =  25,000 13 Hour ago
4  Sealed Blacksmithing Writ  Rewards 287 Vouchers  Level 1  @Gregori_Weissteufel  Wrothgar: Morkul Stronghold  The Cutthroat Mutineers   45,000  X  1                   =  45,000 13 Hour ago

下面是我的代码:

import re
import pandas as pd
pd.set_option('display.max_columns', None)
pd.options.display.width=None    

def MakeTable(listings):
    hour_idx = [i for i, item in enumerate(listings) if re.search(r"([0-9,]*\s[0-9]*\s(Minute|Hour)\sago|[0-9,]*\sNow)", item)]

    if len(hour_idx) == 1:
        ls = [listings[3:hour_idx[0]+1]]
    elif len(hour_idx) == 2:
        ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1]]
    elif len(hour_idx) == 3:
        ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1]]
    elif len(hour_idx) == 4:
        ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1],listings[hour_idx[2]+1:hour_idx[3]+1]]
    else:
        ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1],listings[hour_idx[2]+1:hour_idx[3]+1],listings[hour_idx[3]+1:hour_idx[4]+1]]

    df = pd.DataFrame(ls)
    print(df)
bttbmeg0

bttbmeg01#

我们可以用listcomprehensions来陈述:

def MakeTable(listings):

    hour_idx = [i for i, item in enumerate(listings) if re.search(r"([0-9,]*\s[0-9]*\s(Minute|Hour)\sago|[0-9,]*\sNow)", item)]
    
    ls = [listings[3:hour_idx[0]+1]]
    
    ls_2 = [x[y[i]+1:y[i+1]+1] for (x, y, i) in zip(listings, hour_idx, range(len(hour_idx)-1))]
    
    ls = ls.append(ls_2)

    df = pd.DataFrame(ls)
    
    print(df)
wrrgggsh

wrrgggsh2#

我想已经有人接了--但我还是去了一小会儿:

import re
import pandas as pd

pd.set_option('display.max_columns', None)
pd.options.display.width=None

human_time_re = re.compile(r"([0-9,]*\s[0-9]*\s(Minute|Hour)\sago|[0-9,]*\sNow)")

def make_table(listings):
    hour_idx = [i for i, item in enumerate(listings) if human_time_re.search(item)]

    hour_key = lambda key: hour_idx[key] + 1
    idx = lambda key, key2=0: listings[key:hour_key(key2)]
    idx_more = lambda key=0, key2=1: listings[hour_key(key):hour_key(key2)]

    ls = (idx(3),) + tuple(idx_more(i, i+1) for i in range(len(hour_idx) - 1))
    return ls

res = make_table(listings)
ls = pd.DataFrame(res)
print(res)

据我所见,它和你发布的版本做的完全一样。

s4n0splo

s4n0splo3#

Python 3.10中,你可以编写如下语法的switch语句:

def MakeTable(listings):
 hour_idx = [i for i, item in enumerate(listings) if re.search(r"([0-9,]*\s[0-9]*\s(Minute|Hour)\sago|[0-9,]*\sNow)", item)]

 match len(hour_idx):
  case 1:
   ls = [listings[3:hour_idx[0]+1]]
  case 2:
   ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1]]
  case 3:
   ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1]]
  case  4:
   ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1],listings[hour_idx[2]+1:hour_idx[3]+1]]
  case _:
   ls = [listings[3:hour_idx[0]+1],listings[hour_idx[0]+1:hour_idx[1]+1],listings[hour_idx[1]+1:hour_idx[2]+1],listings[hour_idx[2]+1:hour_idx[3]+1],listings[hour_idx[3]+1:hour_idx[4]+1]]
 
 df = pd.DataFrame(ls)
 print(df)

相关问题