简体   繁体   English

Python 类型错误:列表索引必须是整数,而不是 str

[英]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
  • Change 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]意味着您正在尝试根据它在列表中的索引来获取一个值。
    • You are already iterating over the values, so you don't need to index the list.您已经在迭代这些值,因此您不需要为列表编制索引。
    • Specifically, the 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]
  • This just resolves the issue with your code without changing your way of writing it.这只是解决了您的代码问题,而不会改变您的编写方式。
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.

相关问题 TypeError:列表索引必须是整数,而不是Python中的str - TypeError: list indices must be integers, not str in Python 'TypeError:列表索引必须是整数,而不是str'Python 3 - 'TypeError: list indices must be integers, not str' Python 3 类型错误:列表索引必须是整数,而不是 str - python - TypeError: list indices must be integers, not str - python TypeError:list indices必须是整数,而不是str Python - TypeError: list indices must be integers, not str Python Python:TypeError:list indices必须是整数,而不是str - Python: TypeError: list indices must be integers, not str Python-> TypeError:列表索引必须是整数,而不是str - Python -> TypeError: list indices must be integers, not str Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List Python列表循环错误:TypeError:列表索引必须是整数,而不是str - Python List Loop Error: TypeError: list indices must be integers, not str Python和JSON解析。 TypeError:列表索引必须是整数,而不是str - Python & JSON Parsing. TypeError: list indices must be integers, not str 使用 Python TypeError 解析 JSON:列表索引必须是整数或切片,而不是 str - Parsing JSON with Python TypeError: list indices must be integers or slices, not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM