【问题标题】:How to get output in set without it being typecased to list?如何在不将其类型化列出的情况下在集合中获取输出?
【发布时间】:2019-08-28 19:46:50
【问题描述】:

我正在从文本文件中读取输入,并且在某些情况下,我必须执行一些操作。输入文件的第一行包含测试用例的数量。下一行包含输入的大小。下一行由元素组成。

在接受输入并遵循正确的操作之后。由于需要维护订单,因此我正在对生成的输出进行排序。

import sys

test_case = int(sys.stdin.readline())

t = 0
while t < test_case:
    len_a, len_b = map(int, input().split())

    A = set()
    B = set()

    for element in sys.stdin.readline().split():
        A.add(int(element))

    for element in sys.stdin.readline().split():
        B.add(int(element))     

    operation = int(sys.stdin.readline())

    if operation == 1: print(sorted(set(A.intersection(B))))
    elif operation == 2: print(sorted(set(A.union(B))))
    elif operation == 3: print(sorted(set(A.symmetric_difference(B))))
    elif operation == 4: print(sorted(set(A.difference(B))))
    elif operation == 5: print(sorted(set(B.difference(A))))

    t += 1

预期的输出是这样的:

{10000、20000、30000}

{10000、20000、30000、40000、50000、60000、80000、90000}

{40000、50000、60000、80000、90000}

{40000, 50000, 60000}

{80000, 90000}

{}

但我得到了:

[10000, 20000, 30000]

[10000, 20000, 30000, 40000, 50000, 60000, 80000, 90000]

[40000, 50000, 60000, 80000, 90000]

[40000, 50000, 60000]

[80000, 90000]

设置()

【问题讨论】:

    标签: python-3.x set


    【解决方案1】:

    希望以下内容对您有所帮助。

    代码

    import sys
    
    test_case = int(sys.stdin.readline())
    
    t = 0
    while t < test_case:
        len_a, len_b = map(int, input().split())
    
        A = set()
        B = set()
    
        for element in sys.stdin.readline().split():
            A.add(int(element))
    
        for element in sys.stdin.readline().split():
            B.add(int(element))     
    
        operation = int(sys.stdin.readline())
    
        if operation == 1: print(set(sorted(A.intersection(B))))
        elif operation == 2: print(set(sorted(A.union(B))))
        elif operation == 3: print(set(sorted(A.symmetric_difference(B))))
        elif operation == 4: print(set(sorted(A.difference(B))))
        elif operation == 5: print(set(sorted(B.difference(A))))
    
        t += 1
    

    编辑 (对于您的情况,为空集打印 {} 但我猜不建议这样做)

    import sys
    
    test_case = int(sys.stdin.readline())
    
    t = 0
    while t < test_case:
        len_a, len_b = map(int, input().split())
    
        A = set()
        B = set()
    
        for element in sys.stdin.readline().split():
            A.add(int(element))
    
        for element in sys.stdin.readline().split():
            B.add(int(element))     
    
        operation = int(sys.stdin.readline())
    
        if operation == 1: 
          if len(A.intersection(B))!=0:
            print(set(sorted(A.intersection(B))))
          else:
            print('{}')
        elif operation == 2: 
          if len(A.union(B))!=0:
            print(set(sorted(A.union(B))))
          else:
            print('{}')
        elif operation == 3: 
          if len(A.symmetric_difference(B))!=0:
            print(set(sorted(A.symmetric_difference(B))))
          else:
            print('{}')
        elif operation == 4: 
          if len(A.difference(B))!=0:
            print(set(sorted(A.difference(B))))
          else:
            print('{}')
        elif operation == 5: 
          if len(B.difference(A))!=0:
            print(set(sorted(B.difference(A))))
          else:
            print('{}')
    
        t += 1
    

    我已经测试过了,工作正常。希望这能解决您的问题!

    【讨论】:

    • 它适用于每个输入。不幸的是,当输出应该是 {} 时,它会打印出 set()。有什么解决办法吗?
    • 是的,我收到 set() 的空结果,我会检查是否有更好的处理方式
    • 实际上,{} 表示一个空的 dict ,只是因为 Python 将空集打印为 set(),这是我从一些来源得到的解决方案。
    • 如果您仍希望 {} 为您的案例显示,我可以为该案例添加一个编辑代码
    • 如果有帮助我很高兴:)
    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 2013-02-24
    • 2013-03-05
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 2013-02-28
    • 1970-01-01
    相关资源
    最近更新 更多