我无法解决这个问题。
这是我目前的输出:
['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']
如果列表中已经存在匹配的.insert()
,我尝试使用FIRMNAME
进行循环,但是无法弄清楚逻辑。
感谢您的帮助。
修改:
这是所需的输出:
['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']
我想保留所有数字。
答案 0 :(得分:1)
使用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