我有一个算法计算每个点p(我的元组中表示的坐标值)与元组列表中的每个其他元组的距离.
点数列表:
centerList = [(54, 2991),
(1717, 2989),
(1683, 2991),
(1604, 2991),
(114, 2991),
(919,222),
(930,233)]
Run Code Online (Sandbox Code Playgroud)
距离functoin:
def getDistance(p0, p1):
return math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2)
Run Code Online (Sandbox Code Playgroud)
用于计算p元组列表中每个其他点的距离的算法.
i = 0
distanceList = []
for p in range(len(centerList)):
while i < len(centerList):
print centerList[p], centerList[i], getDistance(centerList[p], centerList[i])
distance = getDistance(centerList[p], centerList[i])
if distance < 20:
distanceList.append(distance)
i += 1
i = p + 2
Run Code Online (Sandbox Code Playgroud)
我当前的算法以不冗余的方式递增,但在当前状态下,它对于实际应用来说太粗暴.我的问题在于我的实际centerList包含数千个元组.
可以做些什么来提高这种元组比较算法的时间效率?
我想找到字典是否包含该词。来自列表的单词按循环递增。如果您在下面没有收到问题评论,请提出建议。
n = int(input())
d = {}
for i in range(n):
text = input().split()
d[text[0]] = text[1]
list = []
for i in range(n):
list.append(input())
for i in range(n):
***`if list[i] in d == True:`***
print(d[i])
else:
print("Not Found")
Run Code Online (Sandbox Code Playgroud) 需要按最后一个元素对元组列表进行排序,元组可以为空.如果元组不为空,我知道如何排序:
sorted(lst, key=lambda p: p[-1]);
但是当列表有():IndexError: tuple index out of range.
我找不到如何避免它.
我的目标是在 C 中为二维 int 数组动态重新分配内存。我知道已经有几个关于该主题的问题,但不幸的是我的代码无法正常运行,我不知道出了什么问题。
首先我分配内存:
int n = 10;
int m = 4;
int** twoDimArray;
twoDimArray = (int**)malloc(n * sizeof(int*));
for(int i = 0; i < n; i++) {
twoDimArray[i] = (int*)malloc(m * sizeof(int));
}
Run Code Online (Sandbox Code Playgroud)
并用整数初始化数组:
for(int i = 0; i < n; i++) {
for(j = 0; j < 4; j++) {
twoDimArray[i][j] = i * j;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我用来realloc()动态重新分配内存:
int plus = 10;
int newArraySize = n + plus;
twoDimArray = (int**)realloc(twoDimArray, newArraySize * sizeof(int)); …Run Code Online (Sandbox Code Playgroud) 有没有办法加上列表的第一个元素很容易Wat我需要做的是这样的:
Lista1 = [[5,7,6,4,3], [8,7,6,14,5],[5,7,8,6,9]]
Run Code Online (Sandbox Code Playgroud)
结果
18 21 20 24 17
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的字符串:
"asdf {FIELD1}\n adf {FIELD2} asdf adsf{FIELD3}asdf {FIELD4}"
Run Code Online (Sandbox Code Playgroud)
我需要的是一个包含字符串"FIELD1","FIELD2","FIELD3","FIELD4"的数组.换句话说:查找{和}括起来的所有文本并将其放入数组中.使用Python 3.5有一个简单/聪明的方法吗?
我正在做一个需要将输入值存储在两个数组中的代码.我要做一个例子.
输入:1,2,3,4,5,6,7,8
Array1= []
Array2= []
Run Code Online (Sandbox Code Playgroud)
我想要做的是将输入的第一个值存储在array1中,将第二个值存储在array2中.最终结果将是这样
Array1=[1,3,5,7]
Array2=[2,4,6,8]
Run Code Online (Sandbox Code Playgroud)
可以在python3中做到这一点吗?谢谢
我尝试过类似的东西,但是没有用
arr1,arr2 = list(map(int, input().split()))
Run Code Online (Sandbox Code Playgroud) 我有这个代码:
from itertools import groupby
a = [[1,'n'],[2,'n'],[3,'n'],[4,'d'],[5,'n']]
b = [list(group) for key, group in groupby(a, lambda x: x[1] if x[1]=='n' else None)]
print(b)
Run Code Online (Sandbox Code Playgroud)
输出:
[[[1, 'n'], [2, 'n'], [3, 'n']], [[4, 'd']], [[5, 'n']]]
Run Code Online (Sandbox Code Playgroud)
预期产量:
[[[1, 'n'], [2, 'n'], [3, 'n']], [[5, 'n']]]
Run Code Online (Sandbox Code Playgroud) 即使我通过var传递了它要求的值,我仍然收到此错误。我不确定为什么会这样。
class process_raw_snowplow_event_data(luigi.Task):
dataset_date = luigi.DateParameter(default=date.today() - timedelta(days=1))
# force_run = luigi.BoolParameter()
_start = luigi.DateSecondParameter(default=datetime.utcnow())
file_root = luigi.Parameter()
@staticmethod
def download_s3_file(self, s3_filename):
local_filename = "/Users/xxx/etl/%s" % s3_filename
s3_file_full_path = re.compile(r"snowplow-enrich-output/enriched/archive/run=" + strftime("%Y-%m-%d") +r"-\d{2}-\d{2}-\d{2}/")
try:
s3.download_file(Bucket=os.environ.get('SP_BUCKET'), Key=s3_filename, Filename=local_filename)
except Exception as e:
logger.error("%s - Could not retrieve %s because: %s" % ("download_s3_file()", s3_filename, e))
raise
Run Code Online (Sandbox Code Playgroud)
class process_sp_data(process_raw_snowplow_event_data):
def run(self):
s3_filename = "part_%s.%s.json.gz" % (self.file_root, (self.dataset_date + timedelta(days=1)).strftime("%Y-%m-%d"))
infile_name = self.download_s3_file(s3_filename)
match_files = self.list_files(os.environ.get('SP_BUCKET'))
with gzip.open(self.output().path, "wb") as outfile:
with gzip.open(infile_name, "rb") …Run Code Online (Sandbox Code Playgroud) // both() reads integers and prints the numbers in their original order
// and then in reverse order.
// effects: reads input, produces output
void both(void) {
int n = read_int();
if(n != READ_INT_FAIL) {
printf("%d\n", n);
both();
printf("%d\n", n);
}
}
int main(void) {
both();
}
Run Code Online (Sandbox Code Playgroud)
因此,此代码读取int并以其原始顺序和相反的顺序打印数字。read_int()是我的老师实现输入的一种方式。无论如何,假设输入为1、2、3、4、5。预期的输出为1、2、3、4、5、5、4、3、2、1(显然,这是换行符而不是逗号,但是我不想浪费垂直空间)。
所以我的问题是,这如何工作?
据我所知,both()它被main调用,并且在第二个printf()代码可以访问之前一直被调用,直到整个代码结束为止,因为当无效值(两个5之后的任意随机字母)都不会被调用时输入。
这是如何运作的?
python ×8
python-3.x ×3
c ×2
list ×2
algorithm ×1
arrays ×1
dictionary ×1
distance ×1
grouping ×1
point ×1
python-2.7 ×1
realloc ×1
sorting ×1
tuples ×1