我有以下情况。
list1=['10/22/2017 10:00','10/22/2017 10:00','10/22/2017 10:00',
'10/22/2017 11:00','10/22/2017 11:00','10/22/2017 11:00',
'10/22/2017 12:00','10/22/2017 12:00','10/22/2017 12:00',
....
]
list2 = [1,2,5,4,5,3,3,5,6,......] #(list2 size will be equal to no. of unique elements of list1)
我的问题是如何显示list3,其值如下所示。
list3=[1,1,1,
2,2,2,
5,5,5,
...]
意味着没有。对于list1的连续重复元素,每个list2元素应该多次附加到list3中。
答案 0 :(得分:1)
您可以使用itertools
:
import itertools
list1=['10/22/2017 10:00','10/22/2017 10:00','10/22/2017 10:00',
'10/22/2017 11:00','10/22/2017 11:00','10/22/2017 11:00',
'10/22/2017 12:00','10/22/2017 12:00','10/22/2017 12:00']
list2 = [1,2,5,4,5,3,3,5,6]
convert = {}
for a, b in zip(list1, list2):
if a not in convert:
convert[a] = b
new_data = list(itertools.chain(*[[convert[a] for c in range(len(list(b)))] for i, [a, b] in enumerate(itertools.groupby(list1))]))
输出:
[1, 1, 1, 4, 4, 4, 3, 3, 3]
答案 1 :(得分:1)
您可以使用this one来实现此目标。
from itertools import groupby
list1 = ['a', 'a', 'a', 'b', 'c', 'c']
list2 = [1, 2, 5]
sum(([i] * len(list(g)) for (k, g), i in zip(groupby(list1), list2)), [])
# [1, 1, 1, 2, 5, 5]
这将list1分成相等元素的块(实际上这些块本身是[key,chunk-generator]对),用list2中的相应项来压缩那些块,并使用块的长度和list2中的项来汇编最后的列表,使用旧的sum(lists, [])
技巧,这不是压缩列表列表的最佳方法,但非常简洁。如果性能很重要,那就使用嵌套理解:
[x for l in ((i for _ in g) for (_, g), i in zip(groupby(list1), list2)) for x in l]
答案 2 :(得分:0)
我使用OrderedDict:
>>> from collections import OrderedDict
>>> list1 = ['a', 'a', 'b', 'b', 'c', 'c']
>>> list2 = [1, 2, 3]
>>> dictionary = dict(zip(OrderedDict(zip(list1, list1)), list2))
>>> [dictionary[k] for k in list1]
[1, 1, 2, 2, 3, 3]
这样做的好处是保留值的字典,以便在需要再次将键转换为值时非常有用。诀窍是在将新的字典中的两个列表配对之前创建一个有序集(OrderedDict的特例)。
答案 3 :(得分:0)
也可以使用collections.Counter()
import from collections import Counter
list1 = [...]
list2 = [...]
list1_counts = Counter(list1)
# list1_counts is now a dict of {uniqueitem: num_of_occurences}
list2_iter = iter(list2)
list3 = []
for u in list1_counts:
# for each unique item in list1
c2 = next(list2_iter) # pick the next value in list2
list3.extend([c2 for _ in range(list1_counts[u])])
请注意,这不一定会保留list1
答案 4 :(得分:-1)
我快速尝试使用计数器的想法,当下一个元素与前一个元素不同时(我假设列表是有序的),它会递增。
它适用于您输入的值,您需要仔细检查完整数据集:
list1=['10/22/2017 10:00','10/22/2017 10:00','10/22/2017 10:00',
'10/22/2017 11:00','10/22/2017 11:00','10/22/2017 11:00',
'10/22/2017 12:00','10/22/2017 12:00','10/22/2017 12:00'
]
list2 = [1,2,5,4,5,3,3,5,6]
list3 = []
previous = None
counter = -1
for i in list1:
if previous != i:
counter += 1
list3.append(list2[counter])
previous = i
print list3
#[1, 1, 1, 2, 2, 2, 5, 5, 5]