将一个列表中的多个结果附加到另一个列表

append multiple results from one list to another

from operator import itemgetter, attrgetter, methodcaller
from itertools import chain

list1 = []
list2 = []

oso = openStationOrder()
pi = prodInfo()
for result in oso:
    result1 = result.split(',')
    list1.append(result1)
    result2 = sorted(list1, key = itemgetter(4))

for aaa in result2:
print aaa    

结果:

['aaa664847', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000']
['aaa665487', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000']
['aaa661965', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000']
['aaa669696', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000']
['aaa665376', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000']
['aaa661966', ' Completed', ' location, , ' mode', ' 2014-xx-ddT00:00:00.000']
['aaa664855', ' Completed', ' location, , ' mode', ' 2014-xx-ddT00:00:00.000']
['aaa665488', ' Completed', ' location, , ' mode', ' 2014-xx-ddT00:00:00.000']
['aaa664510', ' Completed', ' location, , ' mode', ' 2014-xx-ddT00:00:00.000']

i 然后索引:#first list

for res in result2:
    res1 = res[0]
    list1.append(res1)

结果:

['aaa664847', 'aaa665487', 'aaa661965'...]

第二个列表:

for k in pi:
    k1 = k.split(' ')[0]
    list2.append(k1)

print k

结果:

aaa664288 Image  1,
aaa664847 Image  6,
aaa664847 Video  12

    print list2

[aaa664288, aaa664847, aaa664847]

然后我使用交集在两个列表中找到匹配的 'aaa123456' 数字

match = set(list1).intersection(list2)

for m in match:
    print m

result: # 表示唯一匹配的 aaa 号码是 aaa664847

aaa664847

我希望结果是:

['aaa664847', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000', Image 6, Video 12]

您的问题中的代码和结果显示方式存在多个问题,但经过相当长的时间——太多的——时间来破译你的问题,我认为以下内容可以满足你的要求,如果我'我已经理解它了:

from operator import itemgetter

oso = [
  'aaa669696, Completed, location, mode, 2014-xx-ddT00:00:00.000',
  'aaa664847, Completed, location, mode, 2014-xx-ddT00:00:00.000',
  'aaa661965, Completed, location, mode, 2014-xx-ddT00:00:00.000',
  'aaa665376, Completed, location, mode, 2014-xx-ddT00:00:00.000',
  'aaa665487, Completed, location, mode, 2014-xx-ddT00:00:00.000',
  'aaa664510, Completed, location, mode, 2014-xx-ddT00:00:00.000',
  'aaa664855, Completed, location, mode, 2014-xx-ddT00:00:00.000',
  'aaa665488, Completed, location, mode, 2014-xx-ddT00:00:00.000',
  'aaa661966, Completed, location, mode, 2014-xx-ddT00:00:00.000',
]

list1 = []
for result in oso:
    result1 = result.split(',')
    list1.append(result1)

result2 = sorted(list1, key=itemgetter(4))
print 'result2:'
for aaa in result2:
    print ' ', aaa
print
result2:
  ['aaa669696', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
  ['aaa664847', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
  ['aaa661965', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
  ['aaa665376', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
  ['aaa665487', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
  ['aaa664510', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
  ['aaa664855', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
  ['aaa665488', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
  ['aaa661966', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']


list1 = []
for res in result2:
    res1 = res[0]
    list1.append(res1)

print 'list1:', list1
print
list1: ['aaa669696', 'aaa664847', 'aaa661965', 'aaa665376', 'aaa665487', 
        'aaa664510', 'aaa664855', 'aaa665488', 'aaa661966']


pi = [
  'aaa664288 Image  1',
  'aaa664847 Image  6',
  'aaa664847 Video  12',
]

print 'pi:'
list2 = []
for k in pi:
    k1 = k.split(' ')[0]
    list2.append(k1)
    print ' ', k

print
print 'list2:', list2
pi:
  aaa664288 Image  1
  aaa664847 Image  6
  aaa664847 Video  12

list2: ['aaa664288', 'aaa664847', 'aaa664847']


matches = set(list1).intersection(list2)
print 'matches:', matches
print
matches: set(['aaa664847'])


print 'results:'
for m in sorted(matches):
    for row in result2:
        if row[0] == m:
            result = row[:]  # make separate copy
            for k in pi:
                k1 = k.partition(' ')
                if k1[0] == m:
                    result.append(k1[2])
            print ' ', result
results:
  ['aaa664847', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000', 
   'Image  6', 'Video  12']

编辑:发帖者现在在评论中说 "he needs the k appended" 他的意思是在计算时返回:

for k in pi:
    k1 = k.split(' ')[0]
    list2.append(k1)

这当然需要保留 "the k",dict 看起来很适合。因此,让我们将此循环更改为:

theks = {}
for k in pi:
    pieces = k.split(' ')
    theks[k[0]] = k[1:]
不再需要

list2,我们将只使用 theks——因此下面我将注释掉 list2 的用法并替换为 theks

list1  = [
  ['aaa664847', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000'],
  ['aaa665487', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000'],
  ['aaa661965', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000'],
  ['aaa669696', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000'],
  ['aaa665376', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000'],
  ['aaa661966', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000'],
  ['aaa664855', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000'],
  ['aaa665488', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000'],
  ['aaa664510', ' Completed', ' location', ' mode', ' 2014-xx-ddT00:00:00.000']
]

for row in list1:
    if row[0] in theks:
        print(row + theks[row[0]])

检查 dict theks 中的成员资格与我之前版本中检查 set 中的成员资格一样快。

d1 = [','.join(items) for items in [    
    ['aaa664847', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000'],
    ['aaa665487', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000'],
    ['aaa661965', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000'],
    ['aaa669696', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000'],
    ['aaa665376', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000'],
    ['aaa661966', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000'],
    ['aaa664855', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000'],
    ['aaa665488', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000'],
    ['aaa664510', ' Completed', ' location' , ' mode', ' 2014-xx-ddT00:00:00.000']]]

d2 = '''aaa664288 Image  1,
aaa664847 Image  6,
aaa664847 Video  12'''.splitlines()

使用字典

from __future__ import print_function  # for python3 style
from operator import itemgetter, attrgetter, methodcaller
from itertools import chain
from collections import defaultdict

data1 = {}
data2 = defaultdict(list)

oso = d1  #openStationOrder()
pi = d2  #prodInfo()
for result in oso:
    result1 = result.split(',')
    data1[result1[0]] = result1

result2 = sorted(data1.values(), key=itemgetter(4))
for aaa in result2:
    print(aaa)

#for res in result2:
#    res1 = res[0]
#    list1.append(res1)
list1 = list(data1.keys())
print(list1)

for k in pi:
    k = k.split(' ', 1)
    print(k)
    data2[k[0]].append(k[1])
list2 = list(data2.keys())
print(list2)

match = set(list1).intersection(list2)
for m in match:
    print(data1[m] + data2[m])