python - 有没有更好的方式来编写这个,更短,更容易添加更多

时间:2016-08-10 17:33:54

标签: python

我创建了一组字符串编号,以便与另一组字符串编号匹配

这是漫长而艰难的方式,是否有更好或更短的方式,我可以轻松地扩展此列表,再说500多

    a = i
    print 'grabing file',(a)
    #print (a)
    b = i+1
    print 'grabing file',(b)
    c = i+2
    print 'grabing file',(c)
    d = i+3
    print 'grabing file',(d)
    e = i+4
    print 'grabing file',(e)
    f = i+5
    print 'grabing file',(f)
    g = i+6
    print 'grabing file',(g)
    h = i+7
    print 'grabing file',(h)
    j = i+8
    print 'grabing file',(j)
    k = i+9
    print 'grabing file',(k)
    l = i+10
    print 'grabing file',(l)
    m = i+11
    print 'grabing file',(m)
    n = i+12
    print 'grabing file',(n)
    o = i+13
    print 'grabing file',(o)
    p = i+14
    print 'grabing file',(p)
    q = i+15
    print 'grabing file',(q)
    r = i+16
    print 'grabing file',(r)
    s = i+17
    print 'grabing file',(s)
    t = i+18
    print 'grabing file',(t)
    u = i+19
    print 'grabing file',(u)
    v = i+20
    print 'grabing file',(v)
    w = i+21
    print 'grabing file',(w)
    x = i+22
    print 'grabing file',(x)
    y = i+23
    print 'grabing file',(y)
    b1 = i+24
    print 'grabing file',(b1)
    c1 = i+25
    print 'grabing file',(c1)
    d1 = i+26
    print 'grabing file',(d1)
    e1 = i+27
    print 'grabing file',(e1)
    f1 = i+28
    print 'grabing file',(f1)
    g1 = i+29
    print 'grabing file',(g1)
    h1 = i+30
    print 'grabing file',(h1)
    j1 = i+31
    print 'grabing file',(j1)
    k1 = i+32
    print 'grabing file',(k1)
    l1 = i+33
    print 'grabing file',(l1)
    m1 = i+34
    print 'grabing file',(m1)
    n1 = i+35
    print 'grabing file',(n1)
    o1 = i+36
    print 'grabing file',(o1)
    p1 = i+37
    print 'grabing file',(p1)
    q1 = i+38
    print 'grabing file',(q1)
    r1 = i+39
    print 'grabing file',(r1)
    s1 = i+40
    print 'grabing file',(s1)
    t1 = i+41
    print 'grabing file',(t1)
    u1 = i+42
    print 'grabing file',(u1)
    v1 = i+43
    print 'grabing file',(v1)
    w1 = i+44
    print 'grabing file',(w1)
    x1 = i+45
    print 'grabing file',(x1)
    y1 = i+46
    print 'grabing file',(y1)
    b2 = i+47
    print 'grabing file',(b2)
    c2 = i+48
    print 'grabing file',(c2)
    d2 = i+49
    print 'grabing file',(d2)
    e2 = i+50
    print 'grabing file',(e2)
    f2 = i+51
    print 'grabing file',(f2)
    g2 = i+52
    print 'grabing file',(g2)
    h2 = i+53
    print 'grabing file',(h2)
    j2 = i+54
    print 'grabing file',(j2)
    k2 = i+55
    print 'grabing file',(k2)
    l2 = i+56
    print 'grabing file',(l2)
    m2 = i+57
    print 'grabing file',(m2)
    n2 = i+58
    print 'grabing file',(n2)
    o2 = i+59
    print 'grabing file',(o2)
    p2 = i+60
    print 'grabing file',(p2)
    q2 = i+61
    print 'grabing file',(q2)
    r2 = i+62
    print 'grabing file',(r2)
    s2 = i+63
    print 'grabing file',(s2)
    t2 = i+64
    print 'grabing file',(t2)
    u2 = i+65
    print 'grabing file',(u2)
    v2 = i+66
    print 'grabing file',(v2)
    w2 = i+67
    print 'grabing file',(w2)
    x2 = i+68
    print 'grabing file',(x2)
    y2 = i+69
    print 'grabing file',(y2)
    b3 = i+70
    print 'grabing file',(b3)
    c3 = i+71
    print 'grabing file',(c3)
    d3 = i+72
    print 'grabing file',(d3)
    e3 = i+73
    print 'grabing file',(e3)
    f3 = i+74
    print 'grabing file',(f3)
    g3 = i+75
    print 'grabing file',(g3)
    h3 = i+76
    print 'grabing file',(h3)
    j3 = i+77
    print 'grabing file',(j3)
    k3 = i+78
    print 'grabing file',(k3)
    l3 = i+79
    print 'grabing file',(l3)
    m3 = i+80
    print 'grabing file',(m3)
    n3 = i+81
    print 'grabing file',(n3)
    o3 = i+82
    print 'grabing file',(o3)
    p3 = i+83
    print 'grabing file',(p3)
    q3 = i+84
    print 'grabing file',(q3)
    r3 = i+85
    print 'grabing file',(r3)
    s3 = i+86
    print 'grabing file',(s3)
    t3 = i+87
    print 'grabing file',(t3)
    u3 = i+88
    print 'grabing file',(u3)
    v3 = i+89
    print 'grabing file',(v3)
    w3 = i+90
    print 'grabing file',(w3)
    x3 = i+91
    print 'grabing file',(x3)
    y3 = i+92
    print 'grabing file',(y3)

此代码的第二部分在这里---(xyz) - 字符串数字1到100000,基本上是文件名的一部分

   if str(a) == xyz:
       print (a)
    elif str(b) == xyz:
        print (b)
    elif str(c) == xyz:

这继续我和elif elif elif直到92次,然后我停止了:)

3 个答案:

答案 0 :(得分:3)

对第一部分使用for - 循环:

for a in range(i, i + 93):
    print 'grabing file', a

,第二部分是一个简单的if

if i <= int(xyz) <= i + 92:
    print int(xyz)

答案 1 :(得分:0)

请参阅python的control flow结构,尤其是for循环。

#1

i = 100      # Arbitrary Value
limit = 100  # Arbitrary Value
xyz = '199'  # Arbitrary Value

for num in range(i, i+limit):
    print 'grabing file',(num)

#2

if num == int(xyz):
    print num  # Do something.

答案 2 :(得分:0)

我会创建一本字典。键可以只是数字:

i = 3 #or some other number
d = {}
for x in range(0,92):  #you can also change the range to 500 etc
  d[x]=i
  i +=1

在这种情况下,这会创建一个字典或一对系统。一把钥匙和一个价值。

要打印其值与Key匹配的xyz,您可以使用以下内容:

[m for m, y in d.iteritems() if str(y) == xyz]

这部分基本上贯穿你的值,寻找匹配并打印Key