[英]Extract one common element from two lists and create a dictionary with mapping from one list to other by avoiding duplicates
I am trying to extract same values from one position of a nested list and its corresponding values from another position.我正在尝试从嵌套列表的一个 position 中提取相同的值,并从另一个 position 中提取相应的值。
JJ = [['HC', 0, ' 3.6'],
['HC', 1, ' 3.9'],
['HC', 2, ' 7.0'],
['NC', 7, ' 0.3'],
['NC', 8, ' 0.4'],
['NC', 9, ' 0.5'],
['NC', 10, ' 0.6'],
['NC', 11, ' 0.7'],
['DC', 12, ' 0.8'],
[['DC','NC'], 13, ' 0.9']]
This is a list where the first element is repeated in some of the sub-lists.这是一个列表,其中第一个元素在某些子列表中重复。 I am trying to write a code which will take all the third elements from each of the sub-list and map it with its corresponding first element using two lists.我正在尝试编写一个代码,它将使用两个列表从每个子列表和 map 中获取所有第三个元素及其对应的第一个元素。 Ignore the second element.忽略第二个元素。 The required output is supposed to be:所需的 output 应该是:
list1 = [['HC'],['NC'],['DC'],['DC','NC']]
list2 = [[3.6, 3.9, 7], [0.3, 0.4, 0.5, 0.6, 0.7],[0.8],[0.9]]
where each element in list1
now corresponds to the element of the same position in list2
, or can be a dictionary mapping.其中list1
中的每个元素现在对应于list2
中相同 position 的元素,或者可以是字典映射。 I have tried this:我试过这个:
list1 = []
list2 = []
for i in range(0,len(JJ)-1):
#i_set = set(i)
#for j in range(len(i)):
if JJ[i][0] == JJ[i+1][0]:
list1.append(JJ[i][0])
list2.append(JJ[i][2])
list1 = set(list1)
This gives me same elements again and again in list1
and if I convert it to a set, the mapping/correspondence between elements is lost.这在list1
中一次又一次地给我相同的元素,如果我将它转换为一个集合,元素之间的映射/对应就会丢失。 Also, since the last element is len(JJ)-1
(without subtracting 1 the code shows index error), I cannot collect the last element.此外,由于最后一个元素是len(JJ)-1
(不减去 1,代码显示索引错误),我无法收集最后一个元素。 Is there any other way to do this?还有其他方法吗?
With a simple for-loop: The basic idea is to keep track of the previous first element and the current first element and if it differs, append a new list to list1
and list2
.使用简单的 for 循环:基本思想是跟踪前一个第一个元素和当前第一个元素,如果不同,则 append 一个新列表到list1
和list2
。
list1 = []
list2 = []
prev = curr = None
for sublist in JJ:
curr = sublist[0]
if curr != prev:
list1.append([curr])
list2.append([])
list2[-1].append(float(sublist[2]))
prev = curr
groupby
from the itertools
module could be useful itertools
模块中的groupby
可能很有用
from itertools import groupby
JJ = [['HC', 0, ' 3.6'], ['HC', 1, ' 3.9'], ['HC', 2, ' 7.0'], ['NC', 7, ' 0.3'],
['NC', 8, ' 0.4'], ['NC', 9, ' 0.5'], ['NC', 10, ' 0.6'], ['NC', 11, ' 0.7'],
['DC', 12, ' 0.8'], [['DC','NC'], 13, ' 0.9']]
list1, list2 = [], []
# groupby the first elements in the inner lists
for key, group in groupby(JJ, lambda x: x[0]):
# append keys as lists
list1.append(key if isinstance(key, list) else [key])
# append the last element of the sublists
list2.append([float(e[-1]) for e in group])
print(list1)
# [['HC'], ['NC'], ['DC'], ['DC', 'NC']]
print(list2)
# [[3.6, 3.9, 7.0], [0.3, 0.4, 0.5, 0.6, 0.7], [0.8], [0.9]]
You can do it like this without any additional imports:您可以这样做而无需任何其他导入:
JJ = [['HC', 0, ' 3.6'],
['HC', 1, ' 3.9'],
['HC', 2, ' 7.0'],
['NC', 7, ' 0.3'],
['NC', 8, ' 0.4'],
['NC', 9, ' 0.5'],
['NC', 10, ' 0.6'],
['NC', 11, ' 0.7'],
['DC', 12, ' 0.8'],
[['DC','NC'], 13, ' 0.9']]
dict_ = {}
for k, _, n in JJ:
if isinstance(k, list):
k = tuple(k)
dict_.setdefault(k, []).append(float(n))
list1 = []
list2 = []
for k, v in dict_.items():
if isinstance(k, tuple):
k = list(k)
list1.append(k)
list2.append(v)
print(list1)
print(list2)
Output: Output:
['HC', 'NC', 'DC', ['DC', 'NC']]
[[3.6, 3.9, 7.0], [0.3, 0.4, 0.5, 0.6, 0.7], [0.8], [0.9]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.