Find intersection of two nested lists?

I know how to get an intersection of two flat lists: b1 = [1,2,3,4,5,9,11,15] b2 = [4,5,6,7,8] b3 = [val for val in b1 if val in b2] or def intersect(a, b): return list(set(a) & set(b)) print intersect(b1, b2) But when I have to find intersection for nested lists then my problems starts: c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1,

找到两个嵌套列表的交集?

我知道如何获得两个平面列表的交集: b1 = [1,2,3,4,5,9,11,15] b2 = [4,5,6,7,8] b3 = [val for val in b1 if val in b2] 要么 def intersect(a, b): return list(set(a) & set(b)) print intersect(b1, b2) 但是当我必须找到嵌套列表的交集时,我的问题就开始了: c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]] 最后,我想收到: c3 =

Efficiently checking Euclidean distance for a large number of objects in Python

In a route planning algorithm, I'm trying to perform a filter on a list of nodes based on distance to another node. I'm actually pulling the lists from a crude scene graph. I use the term "cell" to refer to a volume within a simple scenegraph from which we've fetched a list of nodes that are close to each other. Right now, I'm implementing this as: # SSCCE version o

在Python中高效地检查大量对象的欧几里得距离

在路线规划算法中,我试图根据到另一个节点的距离在节点列表上执行过滤器。 我实际上是从粗略的场景图中拉出列表。 我使用术语“单元格”来表示一个简单场景图中的一个卷,从中我们已经获取了彼此接近的节点列表。 现在,我正在实施这个: # SSCCE version of the core function def nodes_in_range(src, cell, maxDist): srcX, srcY, srcZ = src.x, src.y, src.z maxDistSq = maxDist ** 2 for node in cell:

Chess final: king and rook vs king in python

I've got some issues with this simple chess code in python. It is part of a weekly assignment; this is what I got so far: from math import sqrt from random import randint,shuffle,seed def rand_pos(): return [randint(0,7),randint(0,7)] #first, classes defining the kings and the rook; #their only attribute is a randomly generated position on the chessboard. #Each of them has its speci

国际象棋决赛:国王和白嘴鸦vs国王在蟒蛇

我在Python中使用这个简单的国际象棋代码时遇到了一些问题。 它是每周任务的一部分; 这是我到目前为止: from math import sqrt from random import randint,shuffle,seed def rand_pos(): return [randint(0,7),randint(0,7)] #first, classes defining the kings and the rook; #their only attribute is a randomly generated position on the chessboard. #Each of them has its special print method that will be

Get enemy's possible moves in chess to a 2D array

I have three arrays, chess = [["c","d","e","f","g","e","d","c"], ["b","b","b","b","b","b","b","b"], ["a","a","a","a","a","a","a","a"], ["a","a","a","a","a","a","a","a"], ["a","a","a","a","a","a","a","a"], ["a","a","a","a","a","a","a","a"], ["h","h","h","h","h","h","h","h"], ["i","j","k","l","m","k","j","i"]] that stores the position

在国际象棋中获得敌人可能的移动到2D阵列

我有三个阵列, chess = [["c","d","e","f","g","e","d","c"], ["b","b","b","b","b","b","b","b"], ["a","a","a","a","a","a","a","a"], ["a","a","a","a","a","a","a","a"], ["a","a","a","a","a","a","a","a"], ["a","a","a","a","a","a","a","a"], ["h","h","h","h","h","h","h","h"], ["i","j","k","l","m","k","j","i"]] ('a'是空的,'b&#

Chess engine speed in python

Hi! I've written a chess engine in python. For search i'm using a minimax algorithm with alpha beta pruning. My evaluation function is very basic, it just assignes a score to the board based on the piece values still on the board. For board representation i use a mailbox approach, 0x88 board representation in particular. The problem is though, that my search is very very slow. I h

国际象棋引擎在python中的速度

嗨! 我已经用python编写了一个国际象棋引擎。 为了搜索我正在使用alpha beta修剪的minimax算法。 我的评价功能非常基础,只是根据棋盘上的棋子值给棋盘评分。 对于董事会代表我使用邮箱方法,特别是0x88板表示。 问题是,我的搜索速度非常慢。 我还没有实现任何一种移动排序算法,但是仍然需要两分钟(有时甚至是两倍)来搜索4层。每个位置的平均移动次数约为23,并且搜索4在开始的位置,它只看到约20 000个节点。 在

Modeling a chess board in wxpython

I'm learning wxPython to make a chess GUI, but I don't exactly know how it should be done, what widgets to use. I'm thinking that I make every square a button with the piece's picture and then position them on the board right next to each other, so I have 64 buttons on the board. When I move one piece, I put the button on the target square and the origin square will then be rep

在wxpython中建模一个国际象棋棋盘

我正在学习wxPython来制作一个国际象棋GUI,但我不完全知道它应该如何完成,以及使用哪些小部件。 我在想每个方块都带有一张照片,然后将它们放在相邻的电路板上,所以我在电路板上有64个按钮。 当我移动一块时,我将按钮放在目标方块上,原点方块将被替换为代表空方块的按钮。 这是在wxPython中做到这一点的正确方法吗? 我真的被困在这里,因为在wxPython中没有关于国际象棋GUI的例子。 啊,用wxPython创建一个国际象

Chess program OO design

In my chess program, I have a class called Move. It stores where the piece was taken and put. What was the piece and what was the piece captured. The problem is though, in order to get the piece that was moved and captured, I pass the whole Board object to the __init__ method. And so IMO it seems like I should store all the Move class methods to the Board class instead, which too has a metho

国际象棋程序OO设计

在我的国际象棋程序中,我有一个名为Move的课程。 它存储了该片被拍摄和放置的位置。 那件作品是什么,以及那件作品是什么。 但问题是,为了获得被移动和捕获的作品,我将整个Board对象传递给__init__方法。 所以IMO似乎应该将所有Move类方法存储到Board类中,而这些方法也有一个方法可以在给定的正方形上获取该块。 我刚刚开始学习面向对象,所以关于这方面的一些建议,也许一些更一般的设计决定,我们非常感谢。 Move

How to read Avro file in PySpark

I am writing a spark job using python. However, I need to read in a whole bunch of avro files. This is the closest solution that I have found in Spark's example folder. However, you need to submit this python script using spark-submit. In the command line of spark-submit, you can specify the driver-class, in that case, all your avrokey, avrovalue class will be located. avro_rdd = sc.new

如何在PySpark中读取Avro文件

我正在使用python写一个spark工作。 但是,我需要阅读大量的avro文件。 这是我在Spark的示例文件夹中找到的最接近的解决方案。 但是,您需要使用spark-submit提交此python脚本。 在spark-submit的命令行中,您可以指定驱动程序类,在这种情况下,您所有的avrokey,avrovalue类将被定位。 avro_rdd = sc.newAPIHadoopFile( path, "org.apache.avro.mapreduce.AvroKeyInputFormat", "org.apache.avro

How do I design a class in Python?

I've had some really awesome help on my previous questions for detecting paws and toes within a paw, but all these solutions only work for one measurement at a time. Now I have data that consists off: about 30 dogs; each has 24 measurements (divided into several subgroups); each measurement has at least 4 contacts (one for each paw) and each contact is divided into 5 parts and has

我如何在Python中设计一个类?

对于我以前的问题,在爪子内部检测爪子和脚趾方面,我有一些非常棒的帮助,但所有这些解决方案一次只能用于一次测量。 现在我拥有包含关闭的数据: 约30只狗; 每个测量有24个测量值(分成几个子组); 每个测量至少有4个触点(每个爪子一个)和 每个联系人分为5个部分和 有几个参数,如接触时间,位置,总力等。 显然,将所有东西都粘在一个大对象上并不会削减它,所以我想我需要使用类而不是当前的函数。 但是,

replace a string not located between two specific words

Given a string, I need to replace a substring with another in an area not located between two given words. For example: substring: "ate" replace to "drank", 1st word - "wolf", 2nd word - "chicken" input: The wolf ate the chicken and ate the rooster output: The wolf ate the chicken and drank the rooster Currently, the only solution I have is extremely unclean: 1) Replace the string located

替换不在两个特定单词之间的字符串

给定一个字符串,我需要用不在两个给定单词之间的区域中的另一个替换一个子字符串。 例如: substring: "ate" replace to "drank", 1st word - "wolf", 2nd word - "chicken" input: The wolf ate the chicken and ate the rooster output: The wolf ate the chicken and drank the rooster 目前,我唯一的解决方案是非常不洁净的: 1)将位于两个单词之间的字符串替换为临时子字符串,方法是替换位于其间的字符串 2)