Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较列表列表中字符串的出现情况与python中的字典_Python_List_Dictionary - Fatal编程技术网

比较列表列表中字符串的出现情况与python中的字典

比较列表列表中字符串的出现情况与python中的字典,python,list,dictionary,Python,List,Dictionary,我有一个列表,每个列表中有几个字符串。我想为列表中的一个项目指定一个ID。通过所需的输入和输出更容易解释 输入(将是较大的数据集,但具有相同的结构): [u'ID1',u'Loreal',u'Loreal',u'P&G',1,2],[u'ID1',u'P&G',u'Loreal',u'P&G',1,2],[u'ID1',u'Loreal',u'Loreal',u'Unilever',2,1]] 期望输出: [u'ID1',u'Loreal',u'Loreal',u'P&G',1,2,1,[u'I

我有一个列表,每个列表中有几个字符串。我想为列表中的一个项目指定一个ID。通过所需的输入和输出更容易解释

输入(将是较大的数据集,但具有相同的结构): [u'ID1',u'Loreal',u'Loreal',u'P&G',1,2],[u'ID1',u'P&G',u'Loreal',u'P&G',1,2],[u'ID1',u'Loreal',u'Loreal',u'Unilever',2,1]]

期望输出: [u'ID1',u'Loreal',u'Loreal',u'P&G',1,2,1,[u'ID1',u'P&G',(u'Loreal',u'P&G'),1,2,2,[u'ID1',u'Loreal',u'Loreal',u'Unilever',2,1,1]

ID应根据位于每个列表索引1中的公司名称给出。例如,Loreal为1,p&G为2,等等

我的思路是创建一个字典并检查每一行[1]是否是字典中的键。如果是,则添加该键的值(即ID)。否则,在键和ID的字典中创建一个键值对(使用计数器)

到目前为止,我的代码是:

stageTwoDic = {}
dictionaryCounter = 1
for row in stageTwoList:
    for key in stageTwoDic:
        if key == row[1]:
            stageTwoList.append(stageTwoDic[row[1]])
    else:
        stageTwoDic[row[1]] = dictionaryCounter
        stageTwoList.append(dictionaryCounter)
        dictionaryCounter += 1
我收到以下错误消息:

 if key == row[1]:
 TypeError: 'int' object has no attribute '__getitem__'

代码中的基本问题是,您将整数(计数器)作为直接值附加到
stageTwoList
,而不是作为元素附加到其子列表(也就是说,您应该将其附加到
而不是
stageTwoList

这会导致
stageTwoList
中的最后一个元素是您附加的整数,因此当迭代到达该元素时,
row
int
,因此尝试执行
row[1]
会导致您得到的错误

此外,您不需要在字典中循环抛出键来检查键是否存在,您可以使用
in
操作符来检查

您应该改为执行以下操作-

stageTwoDic = {}
dictionaryCounter = 1
for row in stageTwoList:
    if row[1] in stageTwoDic:
        row.append(stageTwoDic[row[1]])
    else:
        stageTwoDic[row[1]] = dictionaryCounter
        row.append(dictionaryCounter)
        dictionaryCounter += 1
演示-

>>> stageTwoList = [[u'ID1', u'Loreal', (u'Loreal', u'P&G'), 1, 2], [u'ID1', u'P&G', (u'Loreal', u'P&G'), 1, 2], [u'ID1', u'Loreal', (u'Loreal', u'Unilever'), 2, 1]]
>>> stageTwoDic = {}
>>> dictionaryCounter = 1
>>> for row in stageTwoList:
...     if row[1] in stageTwoDic:
...         row.append(stageTwoDic[row[1]])
...     else:
...         stageTwoDic[row[1]] = dictionaryCounter
...         row.append(dictionaryCounter)
...         dictionaryCounter += 1
...
>>> stageTwoList
[['ID1', 'Loreal', ('Loreal', 'P&G'), 1, 2, 1], ['ID1', 'P&G', ('Loreal', 'P&G'), 1, 2, 2], ['ID1', 'Loreal', ('Loreal', 'Unilever'), 2, 1, 1]]


为了完成,代码中的另一个问题是,您实际上正在更新
stageTwoList
,即使
行[1]在字典中找到了
,因为在
if
块中没有
break
else
for..else的一部分如果
for
循环正常退出(没有
break
语句或异常),将执行
.

每个公司的数值是预先定义的还是动态分配的?换句话说,这些值是存储在数据库中还是类似的东西中,还是在函数运行时计算并分配?如果是前者(预先定义的值),将它们放入字典表单中,然后用
检查出现在索引中的公司,每个子列表中的一个应该完成这项工作。。。