在Python中使用条件条件

发布于 2025-01-04 04:13:01 字数 296 浏览 0 评论 0原文

我有一个 python 函数,其中包含一个 if 语句来检查某些内容。

def my_func(a, b, c):
  if a < b and b < c:
    #do complicated stuff

我想修改该函数,以便调用者可以通过传入布尔值来确定是否执行 if 语句检查。

def my_func(a, b, c, perform_check=True):

我试图弄清楚如何使条件仅有时被调用,或在其他时候被跳过。

I have a python function that contains an if-statement to check something.

def my_func(a, b, c):
  if a < b and b < c:
    #do complicated stuff

I want to modify the function so that the caller can determine whether or not to perform the if-statement check, by passing in a boolean.

def my_func(a, b, c, perform_check=True):

I'm trying to figure out how to make the conditional only be invoked sometimes, or skipped other times.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻安 2025-01-11 04:13:01

我想你需要的是这样的东西:

if include_already_seen or not data[person].get(item, 0):

I guess something like this is what you need:

if include_already_seen or not data[person].get(item, 0):
最偏执的依靠 2025-01-11 04:13:01
def find_recommendations(person, using=pearson_correlation, data=critics, include_already_seen=True):
  totals  = {}
  sim_sum = {}

  for critic in critics:
    if critic != person:
      similarity_score = using(data, person, critic)
      print "the similarity score between %s and %s is %d" % (person, critic, similarity_score)
      if similarity_score <= 0: continue    # ignore similarity_scores below 0

      for item in data[critic]:

        if include_already_seen or (item not in data[person] or data[person][item] == 0):
          totals.setdefault(item,0)
          totals[item] += data[critic][item] * similarity_score

          sim_sum.setdefault(item,0)
          sim_sum[item] += similarity_score

  #create the normalized list
  rankings = [ (total/sim_sum[item],item) for item, total in totals.items()]

  rankings_sorted = sorted(rankings)
  rankings_sorted.reverse()
  return rankings_sorted
def find_recommendations(person, using=pearson_correlation, data=critics, include_already_seen=True):
  totals  = {}
  sim_sum = {}

  for critic in critics:
    if critic != person:
      similarity_score = using(data, person, critic)
      print "the similarity score between %s and %s is %d" % (person, critic, similarity_score)
      if similarity_score <= 0: continue    # ignore similarity_scores below 0

      for item in data[critic]:

        if include_already_seen or (item not in data[person] or data[person][item] == 0):
          totals.setdefault(item,0)
          totals[item] += data[critic][item] * similarity_score

          sim_sum.setdefault(item,0)
          sim_sum[item] += similarity_score

  #create the normalized list
  rankings = [ (total/sim_sum[item],item) for item, total in totals.items()]

  rankings_sorted = sorted(rankings)
  rankings_sorted.reverse()
  return rankings_sorted
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文