简体   繁体   English

如何在python中划分列表?

[英]How to divide a list in python?

I have this list example list1=[['p1', 'p2', 'p3', 'p4'], ['p5', 'p6', 'p7']] and if any variable is the same as other variable them split it in a group. 我有这个列表示例list1=[['p1', 'p2', 'p3', 'p4'], ['p5', 'p6', 'p7']]并且如果任何变量与其他变量相同他们把它分成一组。 Lets say p1 and p4 are the same and p5 and p6. 让我们说p1和p4是相同的,p5和p6。 So I want a new list that looks like list2=[['p1', 'p4'], ['p2', 'p3'], ['p5', 'p6'], 'p7'] . 所以我想要一个新列表,看起来像list2=[['p1', 'p4'], ['p2', 'p3'], ['p5', 'p6'], 'p7'] So how I need to divide them, pls help. 所以我需要如何划分它们,请帮忙。 I am using the newest version of python. 我正在使用最新版本的python。

Ok to be more specific for "the same" in my program, I use p1 and p4 and if they give the same result for a certain character then I merge them in one group. 好的,对于我的程序中的“相同”更具体,我使用p1和p4,如果它们为某个字符给出相同的结果,那么我将它们合并到一个组中。 Example

if dictionary.get(p1, character) is dictionary.get(p4, character)

if you have more questions just ask me. 如果你有更多问题,请问我。

The following will give you that result: 以下将给出结果:

list1=[[1, 2, 2, 1], [3, 4, 3]]

print [[item]*lst.count(item) for lst in list1 for item in list(set(lst))]

[OUTPUT]
[[1, 1], [2, 2], [3, 3], [4]]

Example 1 例1

list1=[['hello', 'hello', 'hello', 'what'], ['i', 'am', 'i']]

print [[item]*lst.count(item) for lst in list1 for item in list(set(lst))]

[OUTPUT]
[['what'], ['hello', 'hello', 'hello'], ['i', 'i'], ['am']]

Example 2 例2

list1=[[1,2,3,2,1],[9,8,7,8,9],[5,4,6,4,5]]

print [[item]*lst.count(item) for lst in list1 for item in list(set(lst))]

[OUTPUT]
[[1, 1], [2, 2], [3], [8, 8], [9, 9], [7], [4, 4], [5, 5], [6]]

Using the unique_everseen recipe and collections.Counters : 使用unique_everseen配方和collections.Counters

from collections import Counter

def solve(lst):
    counters = map(Counter, lst)
    return [ [uniq]*c[uniq] for seq, c in zip(lst, counters)
                                                for uniq in unique_everseen(seq)]

Demo: 演示:

>>> print(solve([[1, 2, 2, 1], [3, 4, 3]]))
[[1, 1], [2, 2], [3, 3], [4]]
>>> print(solve([['hello', 'hello', 'hello', 'what'], ['i', 'am', 'i']]))
[['hello', 'hello', 'hello'], ['what'], ['i', 'i'], ['am']]
>>> print(solve([[1,2,3,2,1],[9,8,7,8,9],[5,4,6,4,5]]))
[[1, 1], [2, 2], [3], [9, 9], [8, 8], [7], [5, 5], [4, 4], [6]]

As you can see this also preserves the order of the items. 如您所见,这也保留了项目的顺序。


Code for unique_everseen recipe: unique_everseen配方的代码:

from itertools import filterfalse

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in filterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

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

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