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,"%" を出力します。

攻撃率 calc2 => 40% であるが <50% AND user_numberofselections > 100 の場合、"Z 攻撃率 = ",strikeratecalc2,"%" を出力します。

strikeratecalc2 => 50% AND user_numberofselections >100 の場合、"ZA Strike rate = ",strikeratecalc2,"%" を出力します。

おそらく単純な作業であると思われるものに対して誰かが解決策を提供できる場合、私は自分のニーズに合わせて答えをリバースエンジニアリングすることができます. さらに情報が必要な場合はお知らせください。敬具

4

2 に答える 2

2

ここに(非常にハックな)解決策があります:

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, '%'

注: 私はそれがかなりハックであることを知っていますが、動作します。2000 行のコードも必要ありません。

于 2013-06-05T05:17:21.930 に答える
1

(strikerate,numberOfSelections)フィルター述語のカスケード リストを介してリストを実行し、それぞれのターゲット リストにデータを追加する 1 つのアプローチを次に示します。

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 に答える