V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
frmongo
V2EX  ›  Python

小问题

  •  
  •   frmongo · 2018-08-27 23:38:50 +08:00 · 1369 次点击
    这是一个创建于 2069 天前的主题,其中的信息可能已经有所发展或是发生改变。
    对于一个这样的 list, [[1,2],[2,3],[3,4],[4,7],[1,7]]
    每个元素都是 [ x,y ] ,如果一个元素 a 的 a[1]和一个元素 b 的 b[0]相等,2 个元素就可以“合并”成一个新元素 c.C 的第一个元素和第二个元素,是原来的 a[0]和 b[1].

    求最后完全合并的 list 里有没有一样的元素?
    这个怎么实现呢
    6 条回复    2018-08-30 14:12:56 +08:00
    frmongo
        1
    frmongo  
    OP
       2018-08-28 11:43:18 +08:00
    有没有大神感兴趣怎么实现,我自己实现的 code 不具备通用性,所以想参考下其他人的想法。目前问题纠结在,如何实现在遍历 list 的过程中还可能会删除多个元素的这个操作上
    codingKingKong
        2
    codingKingKong  
       2018-08-28 11:52:48 +08:00
    @frmongo 我用递归写了一个, 也仅仅在给出的这个上运行正常
    siteshen
        3
    siteshen  
       2018-08-28 12:45:38 +08:00
    # 实现了一个简单版的,不过有个假定:heads 和 tails 是各自唯一的。
    # 如果不是唯一,可以对各个元素增加一个 count 字段,然后比较。

    def merge(list_of_list):
    heads = {a for (a, b) in list_of_list}
    tails = {b for (a, b) in list_of_list}
    return heads - tails

    print(merge([[1,2],[2,3],[3,4],[4,7]])) # {1}
    print(merge([[1,2],[2,3],[3,4],[4,7],[7,1]])) # set()
    codingKingKong
        4
    codingKingKong  
       2018-08-28 12:49:37 +08:00
    package temp;

    import java.util.ArrayList;
    import java.util.List;

    public class D {

    private List<Node> data = new ArrayList<>();

    {
    data.add(new Node(1, 2));
    data.add(new Node(2, 3));
    data.add(new Node(3, 4));
    data.add(new Node(4, 7));
    data.add(new Node(1, 7));
    }

    public static void main(String[] args) {
    D start = new D();
    List<Node> nn = start.check(start.data);
    nn.forEach(n -> System.out.println(n.x + ":" + n.y));
    }

    private List<Node> check(List<Node> nodes){
    if (nodes.size() == 1) {
    return nodes;
    }
    List<Node> result = new ArrayList<>();
    boolean can = false;
    for (int i = 0; i < nodes.size(); i++) {
    for (int j = i+1; j < nodes.size(); j++) {
    if (nodes.get(i).y == nodes.get(j).x) {
    result.add(new Node(nodes.get(i).x, nodes.get(j).y));
    i++;
    can = true;
    break;
    }else {
    if (!result.contains(nodes.get(i))) {
    result.add(nodes.get(i));
    }
    if (!result.contains(nodes.get(j))) {
    result.add(nodes.get(j));
    }
    }
    }
    if (i == nodes.size() -1) {
    if (!result.contains(nodes.get(i))) {
    result.add(nodes.get(i));
    }
    }
    }
    if (can)
    result = check(result);
    return result;
    }

    class Node{
    Node(int x, int y) {
    this.x = x;
    this.y = y;
    }

    int x;
    int y;
    }

    }
    frmongo
        5
    frmongo  
    OP
       2018-08-28 13:50:45 +08:00
    我写了一个,但是 fusion 是一个有缺陷的函数,需要改进

    from random import choice

    def combine_two(a,b):
    c = [None,None]
    if a[1]==b[0]:
    c[0]=a[0]
    c[1]=b[1]
    elif b[1] == a[0]:
    c[0]=b[0]
    c[1]=a[1]
    return c

    def fusion(list):
    ss = list[:]
    nn = len(ss)
    for i in range(100):
    ele_1 = choice(ss)
    ele_2 = choice(ss)
    c = combine_two(ele_1,ele_2)
    if c != [None,None]:
    ss.remove(ele_1)
    ss.remove(ele_2)
    ss.append(c)
    return ss

    def check(list):
    n = len(list)
    for i in range(n):
    for j in range(n):
    if i != j:
    if list[i][0] == list[j][0] and list[i][1] == list[j][1]:
    return False
    return True

    jj = [[2,3],[3,4],[1,2],[4,7],[11,13],[1,7]]
    m = fusion(jj)
    print m
    n = check(m)
    print n
    aaaaasam
        6
    aaaaasam  
       2018-08-30 14:12:56 +08:00
    ```python
    list_a = [[1,2],[2,3],[3,4],[4,7],[1,7]]
    for i in list_a:
    list_test = [(i[0],x[1]) for x in list_a if i != x and i[1] == x[0]]
    if list_test:
    print(i, list_test)
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2691 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:22 · PVG 10:22 · LAX 19:22 · JFK 22:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.