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

时间:2015-01-08 02:02:20

标签: python list append

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']
然后我索引:#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

结果:#它表示唯一匹配的aaa数字是aaa664847

aaa664847

我希望结果如下:

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

3 个答案:

答案 0 :(得分:0)

代码存在多个问题,结果会显示在您的问题中,但经过一段时间 - 太多 - 时间花在解读您的问题上,我认为如果我已经管理了以下内容,您会想要做什么理解它:

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

答案 1 :(得分:0)

编辑:海报现在在评论中说#34;他需要附加k"当他计算时,他意味着回来:

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

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

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

list2,我们只会使用theks - 因此,我会评论list2的使用并替换{{1} }:

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中的成员资格的速度一样快。

答案 2 :(得分:0)

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()

使用dict

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])