2014-12-10 6 views
-1

나는 이것을 알아낼 수 없습니다.다른 요소가 일치하는 경우 하나의 목록의 요소를 다른 목록에 삽입

['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'] 

.

+0

불편을 끼쳐 드려 죄송 합니다만, 원하는 것이 맞습니까? 귀하의 예제 출력에 [기업, 직업, ... 직업, ..., 번호]가 있으며 그 번호는 첫 번째 업무에만 해당합니다. 그게 의도 된거야? – PeterE

답변

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