-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.

同じFIRMNAMEs の場合、対応するすべてJOBTITLEの s が同じ行に必要です。したがって、FIRMNAMEs が一致する場合、対応するJOBTITLEs をその行に追加します。

したがって、出力は次のようになります。

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

すべての番号を保持したい。

4

1 に答える 1

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
于 2014-12-10T17:47:08.463 に答える