引用元组列表中的项目 [英] Referencing Items in a List of Tuples
问题描述
虽然使用元组列表可能非常常见,但我的所有
五本Python书籍或Google搜索都没有告诉我如何引用特定项目
每个
元组。我找到了对元组列表进行排序的参考,但没有根据内容提取元组。
在我的例子中,我有一个9元组的列表。每个元组有30个项目。第一个
两个项目是3个字符的字符串,剩下的28个主题是浮点数。
我想从每个元组创建一个新列表。但是,我希望选择
元组,以及它们对新列表的分配,基于每个元组中前两项的值。
。
如果我尝试,例如,写一下:
for mainlist中的项目:
if mainlist [item] [ 0] ==''eco''和主列表[item] [1] ==''con'':
ec.Append(主列表[item] [2:])
python不喜欢非数字索引。
我真的很感激指针,所以我可以学习如何操作列表
元组通过引用每个元组中的特定项(字符串或浮点数)。
Rich
2月25日凌晨3:01,rshep ... @nospam.appl-ecosys.com写道:
在我的情况下,我有一个清单9个元组。每个元组有30个项目。第一个
两个项目是3个字符的字符串,剩下的28个主题是浮点数。
我想从每个元组创建一个新列表。但是,我希望选择
元组,以及它们对新列表的分配,基于每个元组中前两项的值。
。
如果我尝试,例如,写一下:
for mainlist中的项目:
if mainlist [item] [ 0] ==''eco''和主列表[item] [1] ==''con'':
ec.Append(主列表[item] [2:])
python不喜欢非数字索引。
试试这个:
for item在主列表中:
如果item [0] ==''eco''和item [1] ==''con'':
ec.append(item [item] 2:])
如果你想要数字地址,试试:
for i in range(len(mainlist)):
如果主列表[i] [0] ==''eco''等
2月25日下午1:01,rshep ... @ nospam.appl-ecosys.com写道:
虽然使用元组列表可能非常合适mmon,没有我的
五本Python书籍或Google搜索告诉我如何在每个元组中引用特定项目
。我找到了对元组列表进行排序的参考,但没有根据内容提取元组。
在我的例子中,我有一个9元组的列表。每个元组有30个项目。第一个
两个项目是3个字符的字符串,剩下的28个主题是浮点数。
我想从每个元组创建一个新列表。但是,我希望选择
元组,以及它们对新列表的分配,基于每个元组中前两项的值。
。
如果我尝试,例如,写下:
for mainlist中的项目:
项是你的30元素元组之一,...
if mainlist [item] [0] ==''eco''和主列表[item] [1] ==''con'':
所以这样做:
if item [0] ==''eco''和item [1] ==''con'':
ec.Append(mainlist [item] [2: ])
和
ec.append(item [2:])#note append,not append
>
python不喜欢非数字索引。
如果没有。 Python不喜欢非数字索引,无论你做什么都非常b / b :-)
I真的很感激指针
对不起,只有讨厌的语言有指针:-)
所以我可以通过引用每个元组中的特定项(字符串或浮点数)来学习如何操作列表元数
元组。
HTH,
John
" Rune Strand" ; < ru ********* @ gmail.comwrites:
如果你想要数字地址,试试:
for我在范围内(len(主列表)):
如果主列表[i] [0] ==''eco''等等。
优先:
for i,m in enumerate(mainlist):
如果m [0] ==''eco''等。
While working with lists of tuples is probably very common, none of my
five Python books or a Google search tell me how to refer to specific items
in each tuple. I find references to sorting a list of tuples, but not
extracting tuples based on their content.
In my case, I have a list of 9 tuples. Each tuple has 30 items. The first
two items are 3-character strings, the remaining 28 itmes are floats.
I want to create a new list from each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.
If I try, for example, writing:
for item in mainlist:
if mainlist[item][0] == ''eco'' and mainlist[item][1] == ''con'':
ec.Append(mainlist[item][2:])
python doesn''t like a non-numeric index.
I would really appreciate a pointer so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).
Rich
On Feb 25, 3:01 am, rshep...@nospam.appl-ecosys.com wrote:In my case, I have a list of 9 tuples. Each tuple has 30 items. The first
two items are 3-character strings, the remaining 28 itmes are floats.
I want to create a new list from each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.
If I try, for example, writing:
for item in mainlist:
if mainlist[item][0] == ''eco'' and mainlist[item][1] == ''con'':
ec.Append(mainlist[item][2:])
python doesn''t like a non-numeric index.
try this instead:
for item in mainlist:
if item[0] == ''eco'' and item[1] == ''con'':
ec.append(item[2:])
if you want numeric adressing, try:
for i in range(len(mainlist)):
if mainlist[i][0] == ''eco'' etc.
On Feb 25, 1:01 pm, rshep...@nospam.appl-ecosys.com wrote:While working with lists of tuples is probably very common, none of my
five Python books or a Google search tell me how to refer to specific items
in each tuple. I find references to sorting a list of tuples, but not
extracting tuples based on their content.
In my case, I have a list of 9 tuples. Each tuple has 30 items. The first
two items are 3-character strings, the remaining 28 itmes are floats.
I want to create a new list from each tuple. But, I want the selection of
tuples, and their assignment to the new list, to be based on the values of
the first two items in each tuple.
If I try, for example, writing:
for item in mainlist:item is one of your 30-element tuples, ...
if mainlist[item][0] == ''eco'' and mainlist[item][1] == ''con'':so do this:
if item[0] == ''eco'' and item[1] == ''con'':
ec.Append(mainlist[item][2:])and
ec.append(item[2:]) # note append, not Append
>
python doesn''t like a non-numeric index.If nothing. Python doesn''t like a non-numeric index, quite
irrespective of what you do :-)
I would really appreciate a pointerSorry, only nasty languages have pointers :-)
so I can learn how to manipulate lists
of tuples by referencing specific items in each tuple (string or float).HTH,
John
"Rune Strand" <ru*********@gmail.comwrites:if you want numeric adressing, try:
for i in range(len(mainlist)):
if mainlist[i][0] == ''eco'' etc.Preferable:
for i,m in enumerate(mainlist):
if m[0] == ''eco'' etc.
这篇关于引用元组列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!