【问题标题】:Cartesian product from user input - getting strings not a list来自用户输入的笛卡尔积 - 获取字符串而不是列表
【发布时间】:2021-05-21 00:25:53
【问题描述】:

我正在处理一项任务,并且部分完成,但坚持如何将输出作为单个列表与字符串进行比较。显然我正在学习,非常感谢您的帮助或建议!

这是评估: 计算两个列表的笛卡尔积 AxB。每个列表的数字不超过 10 个。

例如,如果用户提供了两个输入列表: A = [1,2] B = [3,4]

那么笛卡尔积输出应该是: AxB = [(1,3),(1,4),(2,3),(2,4)]

这是我目前所写的:

import itertools

input_A = []
input_B = []

input_A = input('Enter first 2 - 10 characters: ')
input_B = input('Enter second 2 - 10 characters: ')

for combination in itertools.product(input_A, input_B):
  print('AxB = ', [combination])

这是我的输出:

Enter first 2 - 10 characters: 12 #I don't get the the below output when using comma separated items
Enter second 2 - 10 characters: 34
AxB =  [('1', '3')]
AxB =  [('1', '4')]
AxB =  [('2', '3')]
AxB =  [('2', '4')]

【问题讨论】:

  • 您看到这个输出是因为input() 函数总是返回一个字符串。顺便说一句:您确定您的任务实际上是要求您接受用户输入吗?我还假设由于这是家庭作业,您的教授可能希望您编写自己的笛卡尔积函数。
  • 澄清一下:如果您输入12input_A,那么input_A == "12"。如果您改为输入1,2,则输入input_A == "1,2"。如果您确实需要接受用户的输入,那么您将需要编写一些逻辑来确定输入是数字还是字母,以及您作为程序员是否要强制使用逗号分隔值。
  • 这位教授的问题是,即使它没有明确表示获取用户输入,如果你不这样做,他也会给你打分,所以我不得不假设他想要一个......我确实尝试输入 1, 2,但输出包含“,”作为项目而不是分隔符。感谢您的建议,感谢您的帮助。
  • 对,那是因为您没有逻辑来根据逗号字符分隔输入。因此,为什么你会得到一个笛卡尔积,其中"," 被视为一个元素。
  • 另外,我再次感觉到您的教授希望您编写自己的笛卡尔积函数,而不是使用内置版本。

标签: python python-3.x user-input cartesian-product


【解决方案1】:

这里是:

import itertools

input_A = list(map(int, input("Enter multiple numbers for first list separated by commas: ").split(",")))
input_B = list(map(int, input("Enter multiple numbers for second list separated by commas: ").split(",")))
result = list(itertools.product(input_A, input_B))

print(f'A x B = {result}')

#output:
#Enter multiple numbers for first list separated by commas: 3,5,7
#Enter multiple numbers for second list separated by commas: 2,4
#A x B = [(3, 2), (3, 4), (5, 2), (5, 4), (7, 2), (7, 4)]

如果您想在用户输入非数值时捕获异常并不断提示用户输入正确,您可以这样做:

import itertools

while True:
    try:
        input_A = list(map(int, input("Enter multiple numbers for first list separated by commas: ").split(",")))
        input_B = list(map(int, input("Enter multiple numbers for second list separated by commas: ").split(",")))
        result = list(itertools.product(input_A, input_B))
        break
    except ValueError:
        print("Wrong input. Start over!")

print(f'A x B = {result}')

【讨论】:

  • itertools.product 可以直接传递给list 构造函数;无需使用累加器模式。只需执行result = list(itertools.product(input_A, input_B))。我还认为你应该向 OP 解释你的代码,并指出这个脚本对于来自用户的非整数输入是不安全的。
  • @blorgon 完成!今晚你有很多很好的建议。哈哈。
【解决方案2】:

由于您不能使用itertools,让我们构建一个函数来计算两组AB 的笛卡尔积。我假设您的脚本的用户输入部分正常工作。

我将假设您不会进行任何类型的重复删除(从技术上讲,笛卡尔积是在两个 或唯一对象的集合上执行的,但是由于您在进行用户输入时,用户可能会给出一个包含重复项的列表,而说明中没有任何内容说明如何处理)。

作为一个小例子,让A = {0, 1, 2}B = {3, 4, 5}。那么笛卡尔积是

{(0, 3), (0, 4), (0, 5), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)}

请注意,从左到右,每个元组的第一个元素似乎不经常变化。生成此元组列表的系统方法是首先从A 中获取一个元素并将其“固定”到位,同时从B 中获取每个元素并将它们配对。所以在伪代码中:

take 0 from A
    take 3 from B  ->  (0, 3)
    take 4 from B  ->  (0, 4)
    take 5 from B  ->  (0, 5)
    no more elements in B
take 1 from A
    take 3 from B  ->  (1, 3)
    take 4 from B  ->  (1, 4)
    .
    .
    .

或者更简洁:for each item in A, pair it with an element from B

在 Python 中:

>>> for a in A:
...    for b in B:
...        print(f"({a}, {b})")
...
(0, 3)
(0, 4)
(0, 5)
(1, 3)
(1, 4)
(1, 5)
(2, 3)
(2, 4)
(2, 5)

我会让你把最后一块放进去(以某种方式得到你的结果,因为这段代码所做的就是在每次迭代中打印ab 的值)。

【讨论】:

  • @KSparrow,我故意遗漏了最后一小块拼图让你弄清楚。现在我所有的代码正在做的就是打印元组。你需要自己积累这些元组。 “累积”是指使用累加器模式。初始化一个空容器,然后将元组添加到该容器而不是打印它们。
  • 谢谢@blorgon,我现在正在努力解决这个问题。我有笛卡尔(list_A,list_B),我知道你可以追加,但这就是我现在正在研究的。感谢您迄今为止提供的帮助,就像我说的那样,我希望我的教授像您一样有兴趣帮助我们理解。大多数班级似乎都处于同样的困境......
【解决方案3】:

所以我想我明白了(只要你不在用户输入中使用逗号或空格!)这必须做,教授会标记他不喜欢的东西所以这是我要做什么...感谢所有提出想法的人,我显然需要离开它一天才能更清楚地看到它。

def cartesian(list_A, list_B):
        AxB = [ ]
        for A in list_A:
            for B in list_B:
                    AxB.append((A,B))
        print('AxB =  ', AxB)

list_A = input('Enter up to 10 numbers for List A: ')

list_B = input('Enter up to 10 numbers for List B: ')

cartesian(list_A, list_B)

输出:

Enter up to 10 numbers for List A: 12
Enter up to 10 numbers for List B: 34
AxB =   [('1', '3'), ('1', '4'), ('2', '3'), ('2', '4')]

【讨论】:

    猜你喜欢
    • 2018-04-04
    • 2020-09-15
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 2015-08-08
    • 1970-01-01
    相关资源
    最近更新 更多