質問

I'm suppose to create a namedtuple which has 27 field_names. Though it has too many field_names I created a list called sub which has list of items for field_names. The result is my reference to the instance of namedtuple.

sub = [
     'MA9221', 'MC9211', 'MC9212', 'MC9213', 'MC9214',
     'MC9215', 'MC9222', 'MC9223', 'MC9224', 'MC9225',
     'MC9231', 'MC9232', 'MC9233', 'MC9234', 'MC9235',
     'MC9241', 'MC9242', 'MC9243', 'MC9244', 'MC9251',
     'MC9252', 'MC9273', 'MC9277', 'MC9283', 'MC9285']
result = namedtuple('result', ['rollno', 'name'] + sub)

Result values:

rollno = 123123
name = "Sam"
sub_value = [
     1,0,0,0,0,
     0,0,1,1,1,
     1,1,1,0,0,
     1,1,0,0,1,
     1,1,1,0,1]

Now, I don't know how the pass the elements of sub_value to result(rollno, name, ...).

役に立ちましたか?

解決

This line actually defines the type itself:

result = namedtuple('result', ['rollno', 'name'] + sub)

To create an instance, you now need to call result(...).

>>> result(rollno, name, *sub_value)
result(rollno=123123, name='Sam', MA9221=1, MC9211=0, MC9212=0, MC9213=0, MC9214=0, MC9215=0, MC9222=0, MC9223=1, MC9224=1, MC9225=1, MC9231=1, MC9232=1, MC9233=1, MC9234=0, MC9235=0, MC9241=1, MC9242=1, MC9243=0, MC9244=0, MC9251=1, MC9252=1, MC9273=1, MC9277=1, MC9283=0, MC9285=1)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top