0

我有一些代码可以使用以下代码解析从 url 获得的 xml 文件:

pattern4 = re.compile('title=\'Naps posted: (.*) Winners:')
pattern5 = re.compile('Winners: (.*)\'><img src=')

for row in xmlload1['rows']:
    cell = row["cell"]

##### defining the Keys (key is the area from which data is pulled in the XML) for use   in the pattern finding/regex
user_delimiter = cell['username']

##### the use of the float here is to make sure the result of the strike rate calculations returns as a decimal, otherwise python 2 rounds to the nearest integer! 
user_numberofselections = float(re.findall(pattern4, user_delimiter)[0])
user_numberofwinners = float(re.findall(pattern5, user_delimiter)[0])

strikeratecalc1 = user_numberofwinners/user_numberofselections
strikeratecalc2 = strikeratecalc1*100

##### Printing the results of the code at hand

print "number of selections = ",user_numberofselections
print "number of winners = ",user_numberofwinners
print "Strike rate = ",strikeratecalc2,"%"
print ""

getData()

我不知道是否可以过滤数据并打印不同的打印列表。我想我必须描述一个我想要实现的例子:

如果strikeratecalc2 => 20% 但也<30% 打印"X Strike rate = ",strikeratecalc2,"%"

如果strikeratecalc2 => 30% 但也<40% 打印"Y Strike rate = ",strikeratecalc2,"%"

如果strikeratecalc2 => 40% 但也<50% AND user_numberofselections >100 打印“Z Strike rate = ",strikeratecalc2,"%"

如果strikeratecalc2 => 50% AND user_numberofselections >100 打印“ZA Strike rate = ",strikeratecalc2,"%"

如果有人能够为我认为可能是一项简单的任务提供一些解决方案,我将能够根据我的需要对答案进行逆向工程。如果需要更多信息,请告诉我。亲切的问候

4

2 回答 2

2

这是一个(非常hacky)解决方案:

letter = 'X' if (strikeratecalc2 >= 0.20 and strikeratecalc2 < 0.30) else 'Y' if \
 (strikeratecalc2 >=  0.30 and strikeratecalc2 <  0.40) \
 else 'Z' if (strikeratecalc2 >= 0.40 and strikeratecalc2 < \
 0.50 and user_numberofselections > 100) else 'ZA' if \
 (strikeratecalc2 >= 0.50 and user_numberofselections  > 100) \
 else 'Default value here'

print letter, 'Strike rate = ' ,strikeratecalc2, '%'

注意:我知道它相当hackish,但它有效。而且它不需要 2000 行代码。

于 2013-06-05T05:17:21.930 回答
1

这是一种(strikerate,numberOfSelections)通过过滤谓词的级联列表运行列表并将数据附加到相应目标列表的方法:

def splitDataStream(data, xlist, ylist, zlist, zalist, otherlist):

    # a little filter-making function for repetitive filters
    def makefilter(minsr,maxsr,minnumselect):
        if minnumselect is not None:
            return lambda sr,numselect : minsr<=sr<maxsr and numselect>minnumselect
        else:
            return lambda sr,numselect : minsr<=sr<maxsr

    # define list of (destination_list, predicate_filtering_condition) tuples
    cascadingFilters = [
        (xlist, makefilter(20,30,None)),
        (ylist, makefilter(30,40,None)),
        (zlist, makefilter(40,50,100)),
        (zalist, makefilter(50,100,100)),
        ]

    # for each data pair, find destination list based on first matching
    # predicate, and append the data to it; if no match, append to otherlist
    for d in data:
        dest = next((destlist for destlist,pred in cascadingFilters if pred(*d)),
                     otherlist)
        dest.append(d)

现在运行它:

# make some dummy sample data
from random import randint
data = [(randint(0,100), randint(0,500)) for i in range(50)]

# or using your findall returned values
user_numberofselections = map(float, re.findall(pattern4, user_delimiter))
user_numberofwinners = map(float, re.findall(pattern5, user_delimiter))
data = []
for selnum,winnum in zip(user_numberofselections, user_numberofwinners):
    strikeratecalc1 = winnum/selnum
    strikeratecalc2 = strikeratecalc1*100
    data.append((strikeratecalc2, selnum))

xlist = []
ylist = []
zlist = []
zalist = []
otherlist = []
splitDataStream(data, xlist, ylist, zlist, zalist, otherlist)

# list all summary lists
for listname in "xlist ylist zlist zalist otherlist".split():
    listvar = locals()[listname]
    print listname, listvar

印刷:

xlist [(28, 107), (21, 13), (24, 492)]
ylist [(33, 213), (33, 367), (34, 177), (37, 385), (38, 316), (34, 114)]
zlist [(46, 467), (46, 183), (45, 171)]
zalist [(54, 335), (85, 396), (54, 309), (77, 360), (76, 417), (98, 418), (80, 227), (65, 350), (71, 243), (55, 325), (88, 433)]
otherlist [(0, 400), (47, 8), (2, 326), (10, 122), (16, 355), (16, 52), (72, 10), (17, 199), (7, 41), (17, 58), (8, 243), (10, 342), (65, 85), (42, 21), (9, 391), (97, 44), (17, 420), (16, 172), (57, 4), (59, 97), (62, 94), (62, 60), (18, 148), (9, 207), (7, 343), (1, 390), (47, 60)]
于 2013-06-05T04:09:03.497 回答