【发布时间】: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