如果其他元素匹配,则将一个列表的元素插入另一个列表

2024-06-28 20:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我不太明白。你知道吗

这是我的电流输出:

['Direct General Auto Insurance', 'IT Project Manager', 'Nashville', 'TN', '(615) 331-7540']
['Direct General Auto Insurance', 'IT Systems Manager', 'Nashville', 'TN', '(615) 340-6291']
['Direct General Auto Insurance', 'Junior web Developer', 'Nashville', 'TN', '(615) 399-4700']
['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516']
['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000']
['Direct General Auto Insurance', 'Data Scientist', 'Nashville', 'TN', '(615) 726-3091']
['Direct General Auto Insurance', 'Janitor', 'Nashville', 'TN', '(615) 831-2600']

标题是FIRMNAME, JOBTITLE, CITY, STATE, NUMBER。你知道吗

对于相同的FIRMNAME,我需要在同一行中所有相应的JOBTITLE。因此,如果FIRMNAME匹配,则将相应的JOBTITLE添加到该行。你知道吗

所以输出如下:

['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 331-7540']
['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516']
['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000']

如果列表中已经有一个匹配的FIRMNAME,我试着循环它并使用.insert(),但是不能完全理解逻辑。你知道吗

谢谢你的帮助。你知道吗

编辑:

这是所需的输出:

['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 331-7540']
['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 340-6291']
['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 399-4700']
['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 726-3091']
['Direct General Auto Insurance', 'IT Project Manager, IT Systems Manager, Junior Web Developer, Data Scientist, Janitor', 'Nashville', 'TN', '(615) 831-2600']
['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516']
['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000']

我想保留所有的数字。你知道吗


Tags: projectdeveloperautodatamanagerittngeneral
1条回答
网友
1楼 · 发布于 2024-06-28 20:18:48

使用dict索引表。对于每一行,检查您是否已经看到该公司并在那里插入。如果您需要变得更复杂(例如,同一家公司有不同的位置),那么索引键可以成为唯一信息的元组。你知道吗

my_list = [
    ['Direct General Auto Insurance', 'IT Project Manager', 'Nashville', 'TN', '(615) 331-7540'],
    ['Direct General Auto Insurance', 'IT Systems Manager', 'Nashville', 'TN', '(615) 340-6291'],
    ['Direct General Auto Insurance', 'Junior web Developer', 'Nashville', 'TN', '(615) 399-4700'],
    ['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516'],
    ['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000'],
    ['Direct General Auto Insurance', 'Data Scientist', 'Nashville', 'TN', '(615) 726-3091'],
    ['Direct General Auto Insurance', 'Janitor', 'Nashville', 'TN', '(615) 831-2600'],
]

index = {}

for item in my_list:
    existing = index.get(item[0])
    if not existing:
        index[item[0]] = item
        # or this, if you want don't want to modify the existing table
        # index[item[0]] = item[:]
    else:
        existing.insert(1, item[1])

for item in sorted(index.values()):
    print item

编辑

你编辑的结果是完全不同的-你想插入所有的工作到公司的每个原始条目。在这种情况下,我们只需要建立每个公司的工作列表,然后返回到原始列表并插入它们。你知道吗

import collections

my_list = [
    ['Direct General Auto Insurance', 'IT Project Manager', 'Nashville', 'TN', '(615) 331-7540'],
    ['Direct General Auto Insurance', 'IT Systems Manager', 'Nashville', 'TN', '(615) 340-6291'],
    ['Direct General Auto Insurance', 'Junior web Developer', 'Nashville', 'TN', '(615) 399-4700'],
    ['Grand Slam Universal, LLC', 'IT Specialist', 'Nashville', 'TN', '(615) 457-3516'],
    ['Ingram Content Group', 'Desktop Engineer', 'La Vergne', 'TN', '(615) 793-5000'],
    ['Direct General Auto Insurance', 'Data Scientist', 'Nashville', 'TN', '(615) 726-3091'],
    ['Direct General Auto Insurance', 'Janitor', 'Nashville', 'TN', '(615) 831-2600'],
]

index = collections.defaultdict(list)

# generate list of jobs per firm
for item in my_list:
    index[item[0]].append(item[1])

# insert into existing table
for item in my_list:
    del item[1]
    item[1:2] = index[item[0]]

for item my_list:
    print item

相关问题 更多 >