0

I'm trying to read in a polynomial in GF(2) finite field, which is basically only 1 or 0 for the coefficients or constants and the only number that really differentiates 1 part of polynomial from the other is the exponent. But the exponent ends up only marking the 'place' or index of the location in the resulting list that is then only marked as a 1. Every other position in the resulting list is a 0.

Some code with an example:

a = "x**14 + x**1 + x**0"
#b = [int(s) for s in a.split() if s.isdigit()]
import re
b = re.findall(r'\d+', a)
print b
c = [int(x) for x in b]
print c
d = []; m = max(c)
print c[1]
for i in range(m):
    d.append(0)
#    if c[i]>=0: d.insert(i,1)
#    else: d.insert(i,0)

for i in c:
    del d[i-1]
    d.insert(i,1)

#d.insert(m,1);
d.reverse()
print d,len(d)

So as you can see, this outputs:

>>> 
['14', '1', '0']
[14, 1, 0]
1
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1] 14
>>> 

But is should output a 1 in the head of the list (polynomial degrees go high-to-low from left-to-right and so should the list). The length is right, since that's the highest degree. I'm teaching myself Python right now, so I'm sure that the pro Python people are cringing at my code. To those who are pro, please feel free to suggest a more Pythonic solution, or anything else constructive as I'm trying to rid myself of all beginner habits as quickly as possible. I'll put this into a function (actually it goes into a function within a class) later, this is just to get the basic idea down.


EDIT:

From the answer below (I'm not taking credit for the idea just some of the code), I made this which seems Pythonic but not sure how to best integrate the code where I extract using regex and convert to int (in the c list):

a = "x**14 + x**1 + x**0"
#b = [int(s) for s in a.split() if s.isdigit()]
import re
b = re.findall(r'\d+', a)
print b
c = [int(x) for x in b]
m = max(c)
e = []
#[e.append(1) if i in c else e.append(0) for i in range(m,-1,-1)]
e = [1 if x in c else 0 for x in xrange(m, -1, -1)] ##better solution
print e

I'm wondering how to streamline this code and make it more Pythonic.

4

1 回答 1

2

range(14)返回从 0 到 13 的数字,不包括 14:

尝试:

d = []
#no need of two loops here, if the number is present in c then append 1 else 0
for i in range(m, -1, -1): #goes from 14 to 0, -1 is not inclusive
    if i in c:
        d.append(1)
    else:
        d.append(0)

#one-liner : d = [1 if x in c else 0  for x in xrange(14, -1, -1)]
print d,len(d)
#prints [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1] 15

您的代码的较短版本:

import re
a = "x**14 + x**1 + x**0"
c = [int(m.group(0)) for m in re.finditer(r'\d+', a)] #re.finditer returns an iterator
m = max(c)
d = [1 if x in c else 0  for x in xrange(m, -1, -1)]
print d,len(d)
于 2013-06-24T03:18:45.997 回答