[英]Python TypeError: list indices must be integers, not str
I have a list of names:我有一个名字列表:
geneNameList = ['psaA', 'psbF', 'rpl36', 'rpoC1', 'psbK', 'atpB']
and a list of positions:和职位列表:
positionList = ['_1stpos', '_2ndpos', '_3rdpos']
I am trying to add the strings in my position list to each of the names in my list of names so I would end up with a partitionList:我正在尝试将 position 列表中的字符串添加到我的名称列表中的每个名称中,这样我最终会得到一个 partitionList:
partitionList = ['psaA_1stpos', 'psaA_2ndpos', 'psaA_3rdpos', psbF_1stpos', 'psbF_2ndpos'... 'atpB_3rdpos']
I have this code:我有这个代码:
partitionList = []
for i in geneNameList:
for k in positionList:
partition = geneNameList[i] + positionList[k] + ' = '
partitionList.append(partition)
But I keep getting this error:但我不断收到此错误:
partition = geneNameList[i] + positionList[k] + ' = '
TypeError: list indices must be integers, not str
geneNameList[i] + positionList[k] + ' = '
to i + k
to add the values of each list. geneNameList[i] + positionList[k] + ' = '
更改为i + k
以添加每个列表的值。
geneNameList[i]
implies you're trying to get a value based upon it's index in the list. geneNameList[i]
意味着您正在尝试根据它在列表中的索引来获取一个值。TypeError
is caused because you're indexing the list by a value instead of an integer.TypeError
是因为您使用值而不是 integer 来索引列表。 (eg geneNameList['psaA']
instead of geneNameList[0]
) geneNameList['psaA']
而不是geneNameList[0]
)positionList = ['_1stpos', '_2ndpos', '_3rdpos']
geneNameList = ['psaA', 'psbF', 'rpl36', 'rpoC1', 'psbK', 'atpB']
partitionList = []
for i in geneNameList:
for k in positionList:
partition = i + k
partitionList.append(partition)
print(partitionList)
['psaA_1stpos',
'psaA_2ndpos',
'psaA_3rdpos',
'psbF_1stpos',
'psbF_2ndpos',
'psbF_3rdpos',
'rpl36_1stpos',
'rpl36_2ndpos',
'rpl36_3rdpos',
'rpoC1_1stpos',
'rpoC1_2ndpos',
'rpoC1_3rdpos',
'psbK_1stpos',
'psbK_2ndpos',
'psbK_3rdpos',
'atpB_1stpos',
'atpB_2ndpos',
'atpB_3rdpos']
You can use itertools.product
to create a cross product of each of the gene names and positions and then join
them together in a list comprehension to produce your partitionList
:您可以使用
itertools.product
创建每个基因名称和位置的叉积,然后join
它们连接到列表推导中以生成您的partitionList
:
import itertools
geneNameList = ['psaA', 'psbF', 'rpl36', 'rpoC1', 'psbK', 'atpB']
positionList = ['_1stpos', '_2ndpos', '_3rdpos']
partitionList = [''.join(gp) for gp in itertools.product(geneNameList, positionList)]
print(partitionList)
Output: Output:
[
'psaA_1stpos', 'psaA_2ndpos', 'psaA_3rdpos',
'psbF_1stpos', 'psbF_2ndpos', 'psbF_3rdpos',
'rpl36_1stpos', 'rpl36_2ndpos', 'rpl36_3rdpos',
'rpoC1_1stpos', 'rpoC1_2ndpos', 'rpoC1_3rdpos',
'psbK_1stpos', 'psbK_2ndpos', 'psbK_3rdpos',
'atpB_1stpos', 'atpB_2ndpos', 'atpB_3rdpos'
]
If you want the =
on the end of each string, just change ''.join(gp)
to ''.join(gp) + ' = '
in the comprehension.如果您想要在每个字符串的末尾添加
=
,只需将理解中的''.join(gp)
更改为''.join(gp) + ' = '
。
You are iterating over objects, not over integers.您正在迭代对象,而不是整数。
Python works something like this: Python 的工作原理如下:
Imagine that you have a list假设你有一个列表
[cat (an object with it properties), dog, human] [猫(具有它属性的 object)、狗、人]
if you make:如果你做:
for i in list:
you will be iterating over cat, dog, and human, but not over the index of the list.您将遍历 cat、dog 和 human,但不会遍历列表的索引。
You have to do this:你必须这样做:
for i in range(0, len(list)):
So, now you are iterating over integers (list index) and not objects.所以,现在您正在迭代整数(列表索引)而不是对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.