在if语句中自动增加一个 - “数字”

时间:2022-11-11 18:22:15

I have a problem in Python.

我在Python中遇到了问题。

How can I increment by one fully automatically the -"number" present in this if statement?

如何完全自动地增加一个if语句中的“数字”?

An example to understand me. I have this situation

一个例子来了解我。我有这种情况

def name1:
    if (dictionary[class.methodclass()][constant - 1] == dictionary[class.methodclass()][constant - 2] == dictionary[class.methodclass()][constant - 3] == ldictionary[class.methodclass()][constant - 4] == dictionary[class.methodclass()][constant - 5]):
        blablabla

def name2:
    if (dictionary[class.methodclass()][constant - 1] == dictionary[class.methodclass()][constant - 2] == dictionary[class.methodclass()][constant - 3] == ldictionary[class.methodclass()][constant - 4]):
        blabla

def name3:
    if (dictionary[class.methodclass()][constant - 1] == dictionary[class.methodclass()][constant - 2] == dictionary[class.methodclass()][constant - 3]):
        blabla

I repeat the same code, so how to avoid this?

我重复相同的代码,那么如何避免这种情况呢?

Thank you

谢谢

Edit: I have a normal dictonary like

编辑:我有一个正常的dictonary喜欢

dictonary = {"Element": ["Key1", "Key2"]}

I wanna have a cycle that confirm me to work with "Key1" == "Key2" case.

我想要一个循环,确认我使用“Key1”==“Key2”的情况。

3 个解决方案

#1


1  

This is a way:

这是一种方式:

if all ( [ dictionary[class.methodclass()][constant - 1] ==  
           dictionary[class.methodclass()][ noConstant]  
           for noConstant in range(constant - 2,  constant - 6, -1 )  ]
       ):
    blablabla

#2


0  

The core aim I can summarize from your code is: to judge if values in certain section of a list are all the same.

我可以从您的代码中总结出来的核心目标是:判断列表某些部分的值是否完全相同。

According to this logic, we can write a function to archive:

根据这个逻辑,我们可以编写一个存档函数:

def is_list_section_repeated(li, start, end):
    section = li[start:end]
    if section.count(section[0]) == len(section):
        return True
    return False

With this function your code could be:

使用此功能,您的代码可以是:

def name1:
    if is_list_section_repeated(dictionary[class.methodclass()], constant - 5, constant - 1):
        blablabla

def name2:
    if is_list_section_repeated(dictionary[class.methodclass()], constant - 4, constant - 1):
        blablabla

....

As the situation is a kind of complex as to most common cases, I think write a function to handle it specially is better that use tricky tricks which may make code unclear and hard to read :P

由于情况对于大多数常见情况来说是一种复杂的情况,我认为写一个函数来处理它特别是使用棘手的技巧可能会使代码不清楚并且难以阅读:P

#3


0  

Your task has two parts:

你的任务有两个部分:

a) You have several functions that repeat the same complex test.

a)您有几个功能可以重复相同的复杂测试。

Solution: If the test is complex enough, factor it out into a separate function that returns True or False. Use it in each of your nameN functions.

解决方案:如果测试足够复杂,请将其分解为一个返回True或False的单独函数。在每个nameN函数中使用它。

b) You have a test that checks whether a series of elements are all the same. Using the simple version in your edit: Given a list of dictionary keys, check that all values are the same.

b)您有一个测试,检查一系列元素是否都相同。在编辑中使用简单版本:给定字典键列表,检查所有值是否相同。

Solution: Fetch all the values, form a set, and check its size:

解决方案:获取所有值,形成一个集合,并检查其大小:

tocheck = [ "Key1", "Key2" ]
values = set( mydict[k] for k in tocheck )
if len(values) == 1:
    print "equal"
else:
    print "unequal"

You can adapt the same approach to your original code: Write a comprehension (or any other kind of loop) that collects into a set all the values you're comparing. If you do this a lot, it'll be worth your while to set up a simple function that encapsulates this kind of test.

您可以对原始代码采用相同的方法:编写一个理解(或任何其他类型的循环),将您收集的所有值集合到一个集合中。如果你做了很多,那么设置一个封装这种测试的简单函数是值得的。

#1


1  

This is a way:

这是一种方式:

if all ( [ dictionary[class.methodclass()][constant - 1] ==  
           dictionary[class.methodclass()][ noConstant]  
           for noConstant in range(constant - 2,  constant - 6, -1 )  ]
       ):
    blablabla

#2


0  

The core aim I can summarize from your code is: to judge if values in certain section of a list are all the same.

我可以从您的代码中总结出来的核心目标是:判断列表某些部分的值是否完全相同。

According to this logic, we can write a function to archive:

根据这个逻辑,我们可以编写一个存档函数:

def is_list_section_repeated(li, start, end):
    section = li[start:end]
    if section.count(section[0]) == len(section):
        return True
    return False

With this function your code could be:

使用此功能,您的代码可以是:

def name1:
    if is_list_section_repeated(dictionary[class.methodclass()], constant - 5, constant - 1):
        blablabla

def name2:
    if is_list_section_repeated(dictionary[class.methodclass()], constant - 4, constant - 1):
        blablabla

....

As the situation is a kind of complex as to most common cases, I think write a function to handle it specially is better that use tricky tricks which may make code unclear and hard to read :P

由于情况对于大多数常见情况来说是一种复杂的情况,我认为写一个函数来处理它特别是使用棘手的技巧可能会使代码不清楚并且难以阅读:P

#3


0  

Your task has two parts:

你的任务有两个部分:

a) You have several functions that repeat the same complex test.

a)您有几个功能可以重复相同的复杂测试。

Solution: If the test is complex enough, factor it out into a separate function that returns True or False. Use it in each of your nameN functions.

解决方案:如果测试足够复杂,请将其分解为一个返回True或False的单独函数。在每个nameN函数中使用它。

b) You have a test that checks whether a series of elements are all the same. Using the simple version in your edit: Given a list of dictionary keys, check that all values are the same.

b)您有一个测试,检查一系列元素是否都相同。在编辑中使用简单版本:给定字典键列表,检查所有值是否相同。

Solution: Fetch all the values, form a set, and check its size:

解决方案:获取所有值,形成一个集合,并检查其大小:

tocheck = [ "Key1", "Key2" ]
values = set( mydict[k] for k in tocheck )
if len(values) == 1:
    print "equal"
else:
    print "unequal"

You can adapt the same approach to your original code: Write a comprehension (or any other kind of loop) that collects into a set all the values you're comparing. If you do this a lot, it'll be worth your while to set up a simple function that encapsulates this kind of test.

您可以对原始代码采用相同的方法:编写一个理解(或任何其他类型的循环),将您收集的所有值集合到一个集合中。如果你做了很多,那么设置一个封装这种测试的简单函数是值得的。