对于涉及元组和最后一个元组的范围内

发布于 2024-10-12 15:45:34 字数 270 浏览 2 评论 0原文

我正在运行 for 来检查元组列表。 。

for i in range of b:
   actual=i
   temp1=(actual[0]+1,actual[1])
   temp2=(actual[0],actual[1]-1)
   temp3=(actual[0],actual[1]+1)
   temp4=(actual[0]-1,actual[1])

我想确保临时值永远不会采用之前在循环中验证的元组的值 关于如何做到这一点有什么想法吗?

im running a for to check a list of tuples. something in the lines of

for i in range of b:
   actual=i
   temp1=(actual[0]+1,actual[1])
   temp2=(actual[0],actual[1]-1)
   temp3=(actual[0],actual[1]+1)
   temp4=(actual[0]-1,actual[1])

And i want to make sure the temp's NEVER take the value of the tuple verified on the cycle before. Any idea on how to do this?

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

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

发布评论

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

评论(2

请远离我 2024-10-19 15:45:34

首先,您的代码似乎有问题。 range 接受整数输入,因此如果 b 是整数,for i in range(b) 将为您提供整数 [0, 1, 2, .. , b-1 ] 在列表中。您无法像在接下来的两行中那样使用 []i 进行索引。

如果 b 不是整数,而是集合,那么您应该使用类似以下内容的内容:

# Assuming b is a collection
for i in range(len(b)):
   actual=b[i]
   temp1=(actual[0]+1,actual[1])
   temp2=(actual[0],actual[1]-1)
   temp3=(actual[0],actual[1]+1)
   temp4=(actual[0]-1,actual[1])

   # Check if this is the first one.  If it is, previous won't exist.
   if i == 0:
       continue

   previous = b[i-1]
   if previous in [ temp1, temp2, temp3, temp4 ]:
       # This is what you want not to happen.  Deal with it somehow.
       pass

First, it seems that there's a problem with your code. range accepts integer input, so if b is an integer, for i in range(b) will give you integers [0, 1, 2, .. , b-1 ] in a list. You can't index into i using [], as you do on the next two lines.

If b is not an integer, but a collection, then you should use something like:

# Assuming b is a collection
for i in range(len(b)):
   actual=b[i]
   temp1=(actual[0]+1,actual[1])
   temp2=(actual[0],actual[1]-1)
   temp3=(actual[0],actual[1]+1)
   temp4=(actual[0]-1,actual[1])

   # Check if this is the first one.  If it is, previous won't exist.
   if i == 0:
       continue

   previous = b[i-1]
   if previous in [ temp1, temp2, temp3, temp4 ]:
       # This is what you want not to happen.  Deal with it somehow.
       pass
旧街凉风 2024-10-19 15:45:34

这是我的两分钱。
请注意,如果有匹配,这将使 temp(1-4) None 。

# assuming b is a collection
for i in range(len(b)):
    actual=b[i]
    if i!=0:
        prev = b[i-1]
    if i==0:
        prev = [[['something']],[['ridiculous']]] #this is so that the rest of the code works even if index is 0
    if (actual[0]+1,actual[1]) != prev: #if it is not the previous item
        temp1=(actual[0]+1,actual[1]) #assign temp1
    else:
        temp1 = None  #temp1 would otherwise automatically take on the value of (b[i-1][0]+1,b[i-1][1])
    if (actual[0],actual[1]-1) != prev:
        temp2=(actual[0],actual[1]-1)
    else:
        temp2 = None
    if (actual[0],actual[1]+1) != prev:
        temp3=(actual[0],actual[1]+1)
    else:
        temp3 = None
    if (actual[0]-1,actual[1]) != prev:
        temp4=(actual[0]-1,actual[1])
    else:
        temp4 = None

Here's my two cents.
Note that this will make temp(1-4) None if any match.

# assuming b is a collection
for i in range(len(b)):
    actual=b[i]
    if i!=0:
        prev = b[i-1]
    if i==0:
        prev = [[['something']],[['ridiculous']]] #this is so that the rest of the code works even if index is 0
    if (actual[0]+1,actual[1]) != prev: #if it is not the previous item
        temp1=(actual[0]+1,actual[1]) #assign temp1
    else:
        temp1 = None  #temp1 would otherwise automatically take on the value of (b[i-1][0]+1,b[i-1][1])
    if (actual[0],actual[1]-1) != prev:
        temp2=(actual[0],actual[1]-1)
    else:
        temp2 = None
    if (actual[0],actual[1]+1) != prev:
        temp3=(actual[0],actual[1]+1)
    else:
        temp3 = None
    if (actual[0]-1,actual[1]) != prev:
        temp4=(actual[0]-1,actual[1])
    else:
        temp4 = None
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文