9

我正在尝试对这段代码做一个更优雅的版本。这基本上只是根据数字将一个字符串附加到 categorynumber 。将不胜感激任何帮助。

number = [100,150,200,500] 
categoryNumber = []

for i in range (0,len(number)):
    if (number [i] >=1000):
        categoryNumber.append('number > 1000')
    elif (number [i] >=200):
        categoryNumber.append('200 < number < 300')
    elif (number [i] >=100):
        categoryNumber.append('100 < number < 200')
    elif (number [i] >=50):
        categoryNumber.append('50 < number < 100')      
    elif (number [i] < 50):
        categoryNumber.append('number < 50')

for i in range(0,len(categoryNumber)):
    print i
4

7 回答 7

9

怎么样:

labels = (
    (1000, 'number >= 1000'),
    (200,  '200 <= number < 1000'),
    (100,  '100 <= number < 200'),
    (50,   '50 <= number < 100'),
    (0,    'number < 50'),
)

for i in number:
    for limit, label in labels:
         if i >= limit:
             categoryNumber.append(label)
             break
于 2012-10-12T14:08:57.010 回答
3

使用 bisect 怎么样?

>>> import bisect
>>> categories = ['number < 50', '50 <= number < 100', '100 <= number < 200', '200 <= number < 300', '300 <= number <1000', 'number >= 1000']
>>> points = [50, 100, 200, 300, 1000]
>>> categories[bisect.bisect(points, 1000)]
'number >= 1000'
>>> categories[bisect.bisect(points, 1)]
'number < 50'
>>> categories[bisect.bisect(points, 50)]
'50 <= number < 100'
于 2012-10-12T14:31:27.193 回答
2

您的逻辑对于您想要在第二个 elif 上执行的操作似乎很奇怪,因为 number[i] 等于大于 200 的值,例如。350,它将附加类别“200 < number < 300”。不是 200 <= number < 1000 吗?

于 2012-10-12T14:24:15.963 回答
1

就个人而言,我偏爱这种解决方案:

number = [100,150,200,500]

def getCategory(num):
    return ['number < 50', '50 <= number < 100', '100 <= number < 200', '200 <= number < 1000', 'number >= 1000'][(num >= 50) + (num >= 100) + (num >= 200) + (num >= 1000)]

categoryNumber = map(getCategory, number)

我知道该功能的可读性会受到一些影响。我还利用了 Python 将“True”视为 1 的事实。通过将连续比较相加,我找到了正确的返回条目。

稍微清理一下,这更好看:

number = [100,150,200,500]

def getCategory(num):
    limits = [50, 100, 200, 1000]
    msgList = ['number < 50',
               '50 <= number < 100',
               '100 <= number < 200',
               '200 <= number < 1000',
               'number >= 1000']
    return msgList[reduce(lamdba c, l: c+(num >= l), [0] + limits)]

categoryNumber = map(getCategory, number)

我喜欢这个是使用'map'和'reduce'使得for循环变得不必要。

于 2012-10-12T14:30:33.870 回答
1

像这样的东西:

number = [199,75,235,1200,25,49,74,200,51,650]
dic={(1000,float('inf')):'number > 1000',
     (200,300):'200 < number < 300',
     (100,200):'100 < number < 200',
     (50,100): '50 < number < 100',
     (0,50): 'number < 50'}

for x in number:
  for y in dic:
    if x>y[0] and x<y[1]:
      print(x,"is",dic[y])

输出:

199 is 100 < number < 200
75 is 50 < number < 100
235 is 200 < number < 300
1200 is number > 1000
25 is number < 50
49 is number < 50
74 is 50 < number < 100
51 is 50 < number < 100
于 2012-10-12T14:31:31.063 回答
0

您可以简化 Martijn Pieters 的解决方案。而不是这段程序代码:

for i in number:
    for limit, label in labels:
         if i >= limit:
             categoryNumber.append(label)
             break

您可以使用准功能:

for number in numbers:
    label = next(label for limit, label in labels if number >= limit)
    categoryNumber.append(label)
于 2012-10-12T15:09:59.130 回答
0

您可以使用运算符执行以下操作:

import operator
number = [101,151,201,500,1000,45] 
ops={operator.ge:'>=', operator.gt:'>',operator.lt:'<', operator.le:'<='}
cats=(
    (1000, operator.ge,'number >= 1000'),
    (200, operator.ge,'200 <= number < 1000'),
    (100, operator.ge, '100 <= number < 200'),
    (50, operator.ge, '50 <= number < 100'),
    (50, operator.lt, 'number < 50')    
)

for i in number:
    for x, op, label in cats:
        if op(i,x): 
            print '{0:5}{1:^4}{2:5} therefore: {3}'.format(i,ops[op],x,label)
            break

印刷:

  101 >=   100 therefore: 100 <= number < 200
  151 >=   100 therefore: 100 <= number < 200
  201 >=   200 therefore: 200 <= number < 1000
  500 >=   200 therefore: 200 <= number < 1000
 1000 >=  1000 therefore: number >= 1000
   45 <     50 therefore: number < 50

或者你可以这样

for n in numbers:
    result = next('{0:5}{1:^4}{2:5} therefore: {3}'.format(n,ops[op],x,label) 
                      for limit, op, label in cats if op(n,limit))
    print result        
于 2012-10-12T15:35:49.940 回答