【问题标题】:Produce the most unique elements with least lists用最少的列表产生最独特的元素
【发布时间】:2020-07-18 06:24:43
【问题描述】:

我是新来的。希望能在下面举例说明一下。

example1: What is your name?
example1: Where are you from?
example1: How are you doing?
example2: What is your name?
example2: Where are you from?
example2: How are you doing?
example2: When did you move here?
example9: What is your name?
example3: Where are you from?
example23: Who gave you this book?

在上面的例子中,我想通过考虑例子的数量来打印独特的问题。所以尝试类似

预期输出

 example2: What is your name?
 example2: Where are you from?
 example2: How are you doing?
 example2: When did you move here?
 example23: Who gave you this book?

在这里,我通过考虑较少的示例来搜索文件中的独特问题。

我玩弄了一些东西并将它放在下面。

import collections 
s = collections.defaultdict(list)
u_s = set()
with open ('file.txt', 'r') as s1:
    for line in s1:
        data = line.split(':', maxsplit=1)
        start = data[0]
        end = data[-1]
        if end not in u_s:
            u_s.add(end)
            s[start] += [end]
   for start, ends in s.items():
       print(start, ends[0])

       for end in ends[1:]:
           print(start, end)

我得到的结果:

   example1  What is your name?
   example1  Where are you from?
   example1  How are you doing?
   example2  When did you move here?
   example23  Who gave you this book?

在这里,我不想打印 example1,而是想考虑 example2,因为它提供了更多问题。

我尝试根据行的重复对行进行排序。我无法通过它。我感谢您的帮助。谢谢

【问题讨论】:

  • 欢迎来到 Stack Overflow。 example2 包含几个与example1 相同的问题,不会添加到s[start]
  • 您可以详细说明您想要实现的目标。我无法获得您的预期输出以及结果与此的差异。
  • @ChrisTang 谢谢。我正在尝试的是而不是去示例 1,而是想去示例 2,这样我就可以获得最大数量的问题。所以我在想的是首先对重复多次的行进行排序,然后选择唯一的一次。我坚持排序。简而言之,我想用更少的例子打印更多的问题。
  • 我想您想要的是打印最少的问题示例集以产生最独特的问题。对吗?
  • @ChrisTang 完全正确

标签: python-3.x list sorting


【解决方案1】:

您的代码实现的是打印所有独特的问题,但不能比较或打印整个集合。

除了排序之外,我会将问题表述为比较示例集的组合,并选择包含最独特问题和最少集的问题,所以你的问题对我来说更多是关于算法的。

import collections


def calculate_contrib(values, set):
    '''To calculate the contribution on the unique questions' number, based on values to add.
    values: the list of question set to choose.
    set: the already-added question set.'''
    contrib = 0
    for value in values:
        if value not in set:
            contrib += 1
    return contrib


def print_result(x):
    '''To print the result, x, as a dictionary, without repetition.'''
    u_s = set()
    for key, values in x.items():
        for value in values:
            if value not in u_s:
                print(key,value)
                u_s.add(value)


s = collections.defaultdict(list)
# get all questions in examples
with open('file.txt', 'r') as s1:
    for line in s1:
        data = line.split(':', maxsplit=1)
        start = data[0]
        end = data[-1]
        s[start] += [end]

# Get the initial contribution on the unique questions' number for each example set
contrib = dict()
u_s = set()
result = dict()
for key,values in s.items():
    contrib.update({key: calculate_contrib(s[key], u_s)})

# Execute the while loop when there are unique questions to add to u_s
while not(all([x == 0 for x in contrib.values()])):
    # Add the example set with maximum contribution
    max_contrib = 0
    max_key = ""
    for key, value in contrib.items():
        if max_contrib < value:
            max_key = key
            max_contrib = value
    result.update({max_key: s[max_key]})
    u_s.update(s[max_key])
    del s[max_key]
    del contrib[max_key]
    for key, values in s.items():
        contrib[key] = calculate_contrib(values, u_s)

# print the result
print_result(result)

上面是一个简单的实现,即每次添加唯一数增加最多的示例集,直到没有唯一问题。

可以进行进一步的改进。希望它能给你一些见解。

【讨论】:

  • 谢谢你。如果问题重复,它会完美运行。如果问题重复,则打印两次。我不想打印两次。
  • @perkinsroyal 对代码进行了少量修改,以便在不重复的情况下打印结果。希望对你有用。
猜你喜欢
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
相关资源
最近更新 更多