简体   繁体   English

总结python中的列表列表并创建一个新矩阵

[英]summarizing a list of lists in python and make a new matrix

I have a list of lists like this small example:我有一个像这个小例子这样的列表:

[['chr19', '35789598', '35789629', '21', 'chr19', '35510000', '36200000'], ['chr19', '35789598', '35789629', '24', 'chr19', '35510000', '36200000'], ['chr19', '35789598', '35789629', '52', 'chr19', '35510000', '36200000'], ['chr19', '35789598', '35789629', '88', 'chr19', '35510000', '36200000'], ['chr19', '35798974', '35799005', '56', 'chr19', '35510000', '36200000'], ['chr19', '35883830', '35883861', '16', 'chr19', '35510000', '36200000'], ['chr19', '35884320', '35884351', '51', 'chr19', '35510000', '36200000']]

as you see every inner list has 7 elements.如您所见,每个内部列表都有 7 个元素。 I want to make a new list of lists in which there is no inner list with similar 1st, 2nd and 3rd elements.我想创建一个新的列表列表,其中没有具有类似 1st、2nd 和 3rd 元素的内部列表。 in fact if there are some inner lists in which 1st, 2nd and 3rd elements are similar, I would take only the 1st inner list and remove the other inner lists.expected output for the small example would look like this:事实上,如果有一些内部列表的第一个、第二个和第三个元素相似,我将只取第一个内部列表并删除其他内部列表。小示例的预期输出将如下所示:

expected output:预期输出:

[['chr19', '35789598', '35789629', '21', 'chr19', '35510000', '36200000'], ['chr19', '35798974', '35799005', '56', 'chr19', '35510000', '36200000'], ['chr19', '35883830', '35883861', '16', 'chr19', '35510000', '36200000'], ['chr19', '35884320', '35884351', '51', 'chr19', '35510000', '36200000']]

here is the code in python which does not return what I expect:这是python中的代码,它没有返回我期望的内容:

result = []
for i in mat:
    for j in i:
        if j == j-1:
            result.append(j)

I would use pandas:我会使用熊猫:

import pandas as pd
data = [['chr19', '35789598', '35789629', '21', 'chr19', '35510000', '36200000'], 
        ['chr19', '35789598', '35789629', '24', 'chr19', '35510000', '36200000'],
        ['chr19', '35789598', '35789629', '52', 'chr19', '35510000', '36200000'], 
        ['chr19', '35789598', '35789629', '88', 'chr19', '35510000', '36200000'], 
        ['chr19', '35798974', '35799005', '56', 'chr19', '35510000', '36200000'], 
        ['chr19', '35883830', '35883861', '16', 'chr19', '35510000', '36200000'], 
        ['chr19', '35884320', '35884351', '51', 'chr19', '35510000', '36200000']]
# Convert your list of list to a DataFrame
df = pd.DataFrame(data)
       0         1         2   3      4         5         6
0  chr19  35789598  35789629  21  chr19  35510000  36200000
1  chr19  35789598  35789629  24  chr19  35510000  36200000
2  chr19  35789598  35789629  52  chr19  35510000  36200000
3  chr19  35789598  35789629  88  chr19  35510000  36200000
4  chr19  35798974  35799005  56  chr19  35510000  36200000
5  chr19  35883830  35883861  16  chr19  35510000  36200000
6  chr19  35884320  35884351  51  chr19  35510000  36200000

df = df.drop_duplicates([0, 1, 2], keep='first')
       0         1         2   3      4         5         6
0  chr19  35789598  35789629  21  chr19  35510000  36200000
4  chr19  35798974  35799005  56  chr19  35510000  36200000
5  chr19  35883830  35883861  16  chr19  35510000  36200000
6  chr19  35884320  35884351  51  chr19  35510000  36200000

# If you need the data as the list of lists still output like this:
output = df.values
array([['chr19', '35789598', '35789629', '21', 'chr19', '35510000', '36200000'],
       ['chr19', '35798974', '35799005', '56', 'chr19', '35510000', '36200000'],
       ['chr19', '35883830', '35883861', '16', 'chr19', '35510000', '36200000'],
       ['chr19', '35884320', '35884351', '51', 'chr19', '35510000', '36200000']], 
       dtype=object)

# Otherwise you can continue to use the DataFrame for your analysis

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM