在列表中打印命名元组的值。

huangapple go评论64阅读模式
英文:

Print named tuple value in a list

问题

一个函数返回给我以下命名元组的列表:

[popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]

我想打印元组中 'path' 的值。怎么做?请帮帮我,亲爱的社区。

我尝试了遵循在线文章,但没有解决。

英文:

A function is returning me the below list of named tuples:

[popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]

I want to print the value of 'path' in the tuple. How to do this? Please help dear community.

I tried to follow online articles but no solve.

答案1

得分: 1

为了明确起见,请参阅以下代码:

from collections import namedtuple

popenfile = namedtuple('popenfile', 'path fd position mode flags')

tuple_list = [popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]

for ntup in tuple_list:
    print(ntup.path)

这段代码会输出:

/home/giampaolo/monit.py
/var/log/monit.log
英文:

To make it clear see below code:

from collections import namedtuple

popenfile = namedtuple('popenfile', 'path fd position mode flags')

tuple_list = [popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768), popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]

for ntup in tuple_list:
    print(ntup.path)

which prints

/home/giampaolo/monit.py
/var/log/monit.log

huangapple
  • 本文由 发表于 2023年5月29日 00:14:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76352424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定