【问题标题】:comparing two sets and finding equation about them python比较两组并找到关于它们的方程python
【发布时间】:2022-11-29 10:39:39
【问题描述】:

我有两套

list1 = {1,2,3,4,5,6,7,8,9,10}
list2 = {10,20,30,40,50,60,70,80,90,100}

我想让 python 查看每个数字之间是否存在关系,以及每个数字之间的关系是否相同(对于此示例,每个数字都相同并且关系为 *10)或者如果不是,则打印他们没有关系

【问题讨论】:

  • 对不起,我的意思是集合,我对 python 很陌生
  • 集合是无序的。不存在集合的xth 元素这样的东西。
  • 如果我将集合转换为列表,那么您将如何编写比较检查器的代码

标签: python list compare


【解决方案1】:

如果使用集合,则无法定义一对一关系,因为它们是无序的。

如果你有列表,你可以使用:

list1 = [1,2,3,4,5,6,7,8,9,10]
list2 = [10,20,30,40,50,60,70,80,90,100]

ratios = [b/a for a,b in zip(list1, list2)]

输出:[10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0]

或者使用集合理解:

S = {round(b/a, 3) for a,b in zip(list1, list2)}
# {10.0}

# check there is only one possibility
if len(S) != 1:
    print('there is not a unique ratio')

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 1970-01-01
    • 2020-07-03
    • 2017-11-22
    • 2016-08-29
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    相关资源
    最近更新 更多