使用any()在另一个字符串中搜索多个字符串(Searching for Multiple Strings within another String using any())
我目前正在使用Python 3.4.2但是在尝试搜索另一个字符串中的多个字符串时没有得到预期的结果。
我创建一个包含字符串格式的项目的集合,类似于TEXT.NA [Y] ABC。 我试图只捕获包含.NA,.SA或.EU的集合中的项目作为示例。
testset = set() testset.add(('Blah','TEXT.NA[Y]ABC')) testset.add(('Bleh','OTHER.AU[X]DEF')) region = ['.NA', '.SA', '.EU'] for text,key in testset: if any(sym in region for sym in key): print(key)
我期待上面打印TEXT.NA [Y] ABC同时跳过OTHER.AU [X] DEF想知道我在我的迭代中做错了什么。
谢谢!
I'm using Python 3.4.2 at the moment but am not getting the expected results when attempting to search for a number of strings within another string.
I create a set which contains items with a string format similar to TEXT.NA[Y]ABC. I'm trying to only catch items of the set which contain a .NA, .SA or .EU as an example.
testset = set() testset.add(('Blah','TEXT.NA[Y]ABC')) testset.add(('Bleh','OTHER.AU[X]DEF')) region = ['.NA', '.SA', '.EU'] for text,key in testset: if any(sym in region for sym in key): print(key)
I was expecting the above to print TEXT.NA[Y]ABC while skipping OTHER.AU[X]DEF Wondering what I'm doing wrong in my iterable.
Thanks!
原文:https://stackoverflow.com/questions/36479599
最满意答案
rect_size
保存一个值,并一次又一次地覆盖它,直到它保存最后一个矩形。你想要保持一个矩形列表并检查所有矩形的碰撞。
代替
rect_size = pygame.Rect(position_x + x, position_y + y, position_x, position_y)
你想做的事情如下:
rect_sizes.append(pygame.Rect(position_x + x, position_y + y, position_x, position_y))
然后迭代
rect_sizes
并检查每个的碰撞。rect_sizes = [] for x in range(-50, 500, position_x + 20): for y in range(-50, 400, position_y + 20): rect_size = pygame.Rect(position_x + x, position_y + y, position_x, position_y) pygame.draw.rect(screen, colour, rect_size, 0) rect_sizes.append(rect_size) if event.type == pygame.MOUSEBUTTONDOWN: for rect_size in rect_sizes: click = rect_size.collidepoint(pygame.mouse.get_pos()) if click == 1: print("Clicked") pygame.draw.rect(screen, white, rect_size, 0)
rect_size
holds one value at a time, and you override it again and again until it holds the last rectangle.you want to hold a list of the rectangles and check collision with all of them.
instead of
rect_size = pygame.Rect(position_x + x, position_y + y, position_x, position_y)
you want to do something like:
rect_sizes.append(pygame.Rect(position_x + x, position_y + y, position_x, position_y))
and then iterate
rect_sizes
and check collision with each one.rect_sizes = [] for x in range(-50, 500, position_x + 20): for y in range(-50, 400, position_y + 20): rect_size = pygame.Rect(position_x + x, position_y + y, position_x, position_y) pygame.draw.rect(screen, colour, rect_size, 0) rect_sizes.append(rect_size) if event.type == pygame.MOUSEBUTTONDOWN: for rect_size in rect_sizes: click = rect_size.collidepoint(pygame.mouse.get_pos()) if click == 1: print("Clicked") pygame.draw.rect(screen, white, rect_size, 0)
相关问答
更多-
Pygame矩形不动?(Pygame the rectangle is not moving?)[2022-05-23]
在绘图之前使用surface.fill。 我使用的[0, 0, 0]在RGB代码中是黑色的。 你应该声明类似的东西 BLACK = (0, 0, 0) 作为常数避免重复。 所以请改变它并像上面一样使用它。 import pygame, sys, time pygame.init() red = (255, 0, 0) gameDisplay = pygame.display.set_mode((800, 600)) pygame.display.set_caption('MyGame') move_x ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
你while running循环的第一行是goingDown = True 。 这会在循环的每次迭代开始时将变量goingDown设置为true。 你需要在循环之前放置一行goingDown = True 。 它应该如下所示: #GameLoop goingDown = True while running: ... The first line of your while running loop is goingDown = True. This sets the variable going ...
-
在pygame中做矩形跳转(Make rectangle jump in pygame)[2022-06-16]
你必须设置变量即。 jump = True并在每个循环中增加ycoor直到你获得500。 或多或少像这样 xcoor = 0 #the leader of the group of blocks ycoor = 550 jump = False fall = False while not gameExit: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if even ... -
Pygame矩形和鼠标pos Collidepoint无法正常工作(Pygame Rectangle and Mouse pos Collidepoint not working)[2022-02-10]
检查函数的这些行中的逻辑: x = P1[0][0].get_rect() if x.collidepoint(a,b): return True return False 你的代码取决于这一点: a = checkCollide(event) if a: DISPLAYSURF.fill(WHITE) 所以你永远不会评估这件作品是真的。 I just realized what was wrong. When I do x = P1[0].get_rect() it creates a ... -
将列表分配给变量时,它指向同一列表,因此更改列表时,变量会更改。 你想要做的是像这样分配: new_list = old_list[:] 要么 new_list = list(old_list) 例如: 之前: >>> ol = [] >>> nl = ol >>> ol.append(90) >>> ol [90] >>> nl [90] >>> 现在: >>> old_list = [] >>> new_list = old_list[:] >>> old_list.append(90) >> ...
-
如果你正在开发游戏。 我建议你使用Pygame。 我假设您正在讨论使用精灵矩形的碰撞检查? 无论如何这里是一个检查两个矩形之间碰撞的算法。 我没有为您编写一个函数来复制粘贴并在程序中使用它。 这只是您理解的逻辑,并且您可以自己编写一段代码,根据您的应用程序从此逻辑中编写。 def check_collision( A, B ) { #If any of the sides from A are outside of B if( bottomA <= topB ) { return false; ...
-
在鼠标单击的位置绘制矩形(Draw rectangle where mouse clicked)[2021-11-04]
您的绘图应该在面板的Paint事件处理程序中执行。 单击面板时,创建矩形(在面板的MouseUp事件中)并将其存储在矩形集合(例如字典)中。 然后刷新面板。 在面板的Paint事件中,绘制所有矩形。 这是一个简单的例子: Dictionary> rectangles = new Dictionary >(); private void panel1_Paint(object sender, PaintEventArg ... -
rect_size保存一个值,并一次又一次地覆盖它,直到它保存最后一个矩形。 你想要保持一个矩形列表并检查所有矩形的碰撞。 代替 rect_size = pygame.Rect(position_x + x, position_y + y, position_x, position_y) 你想做的事情如下: rect_sizes.append(pygame.Rect(position_x + x, position_y + y, position_x, position_y)) 然后迭代rect_siz ...
-
pygame OOP移动一个矩形(pygame OOP moving a rectangle)[2022-05-06]
这可能会让你开始。 import pygame WHITE = (255,255,255) class init_window(object): def __init__(self, width, height): self.width = width self.height = height self.gameDisplay = pygame.display.set_mode((self.width, self.height)) ...