我有两组列表。
list A= [(1,6),(3,10),(4,1),(0,5)]
list B = [(0,3),(0,4),(30,1),(4,10)]
现在,对于B中的每个项目,我必须检查其是否在列表A中可用,并且阈值介于-2到+2之间。
因此,对于B中使用阈值的每个点,B中的第一个值为(0,3),我发现在此范围内的(from -2 to 2, from 1 to 5)
列表A是否可用。我们可以看到最后一个项目值(0,5)
满足此条件。因此我可以说项目(0,3)
在列表A中。现在,我必须将此值放在新列表中。
根据该过程,我的新列表将是:
[(0,3),(0,4),(4,10)]
如果有人告诉我如何实现这一目标,我会感到很高兴。
答案 0 :(得分:1)
这是我想您正在寻找的
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <string.h>
struct packet_t {
char *name;
struct queue_t *queue;
};
struct queue_t {
char *name; // Name der Warteschlange
int size; // Groesse der Warteschlange
int entries; // Anzahl gueltige Eintraege in Warteschlange
int time; // Zeitstempel (werden wir erst in P6 beoetigen)
struct packet_t **packets; // Array von Zeigern auf Pakete
int read; // Lese-Position (retrieve_packet())
int write; // Schreib-Position (store_packet())
long lost; // Anzahl aller verlorenen Pakete
};
struct queue_t *queue_create(char *name, int size) {
int i;
struct queue_t *q; /* temporaerer zeiger auf Queue */
q = malloc (sizeof(struct queue_t)); //dynamisch
if (q == NULL) {
printf("Memory allocation failed");
return 0;
}
q -> name = name;
q -> size = size;
q -> entries = 0;
q -> write = 0;
q -> read = 0;
q -> lost = 0;
return (q);
}
long queue_store(struct queue_t * q, struct packet_t * p) {
int total = 0;
p -> name = q -> name;
if (q -> entries < q -> size) {
q -> entries++;
q -> write++;
total ++;
if (q -> write > q -> size) {
q -> write = 0;
}
return total;
}
else {
packet_destroy(p);
q -> lost ++;
return 0;
}
}
struct packet_t* queue_retrieve(struct queue_t *q) {
struct packet_t *p;
p = malloc (sizeof(struct packet_t));
struct packet_t *queue = NULL;
if (q -> entries > 0) {
q -> read ++;
q -> entries --;
packet_destroy(p);
if (q -> read > q -> size ) {
q -> read = 0;
}
}
return (p);
}
int queue_destroy(struct queue_t *q) {
free(q);
}
struct packet_t* packet_create(char *p) {
struct packet_t * packet;
packet = malloc (sizeof(struct packet_t));
packet -> name = p;
return (packet);
}
int packet_destroy(struct packet_t *packet) {
free(packet);
return 0;
}
int main(void) {
long i;
struct queue_t *Q; // Zeiger auf Warteschlange
struct packet_t *P; // Zeiger auf Paket
clock_t start, ende;
start = clock();
for (i = 0; i < 1000000; i++) {
P = packet_create ("tester");
}
ende = clock();
printf("Pakete erzeugen:\n");
printf("i = %ld\n", i);
printf("T[s] = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
printf("\n");
start = clock();
for (i = 0; i < 1000000; i++) {
Q = queue_create ("test-queue mit 100 Plaetzen", 100);
}
ende = clock();
printf("Queues erzeugen:\n");
printf("i =%ld\n", i);
printf("T[s] = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
printf("\n");
start = clock();
Q = queue_create ("test, 100", 100);
// Queue halb fuellen
for (i = 0; i < 50; i++) {
P = packet_create ("nix");
queue_store (Q,P);
}
// viele Pakete erzeugen
for (i = 0; i < 1000000; i++) {
P = packet_create ("nix");
queue_store (Q,P);
P=queue_retrieve(Q);
packet_destroy(P);
}
// Vorbefuellung entfernen
for (i = 0; i < 50; i++) {
P=queue_retrieve(Q);
packet_destroy(P);
}
ende = clock();
printf("Queue erzeugen, <Paket erzeugen, speichern,laden,zerstoeren>:\n");
printf("i =%ld\n", i);
printf("T[s] = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
printf("\n");
start = clock();
Q = queue_create ("test-queue fuer 1000 Pakete", 1000);
for (i = 0; i < 900; i++) {
P = packet_create ("aha");
queue_store (Q,P);
}
for (i = 0; i < 901; i++) {
P=queue_retrieve(Q);
if (NULL == P)
printf ("kein Paket\n");
else
packet_destroy(P);
}
ende = clock();
printf("Queue erzeugen, <Paket erzeugen, speichern,lasden,zerstoeren>:\n");
printf("i =%ld\n", i);
printf("T[s] = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
printf("\n");
start = clock();
Q = queue_create ("test, 1000", 1000);
for (i = 0; i < 900; i++) {
P = packet_create ((int)i,0.0,0L,NULL);
queue_store (Q,P);
}
for (i = 0; i < 450; i++) {
P=queue_retrieve(Q);
if (NULL == P)
printf ("kein Paket\n");
else
packet_destroy(P);
}
ende = clock();
printf("Queue erzeugen, <Paket erzeugen, speichern,laden,zerstoeren>:\n");
printf("i =%ld\n", i);
printf ("Q->entries = %d\n", Q->entries);
printf("T[s] = %g\n", (double) (ende - start) / CLOCKS_PER_SEC);
printf("\n");
}
输出:
A= [(1,6),(3,10),(4,1),(0,5)]
B = [(0,3),(0,4),(30,1),(4,10)]
result=[x for x in B if any(x[0]-2<=a[0]<=x[0]+2 and x[1]-2<=a[1]<=x[1]+2 for a in A)]
print(result)
答案 1 :(得分:1)
您可以使用出租车的几何形状:
Page not found
或
def manhattan(as_, b):
threshold = 4
for a in as_:
p1, p2 = a
q1, q2 = b
dist = abs(p1 - q1) + abs(p2 - q2)
if dist <= threshold:
return b
else:
continue
t = list(filter(lambda i: manhattan(listA, i), listB))
[(0, 3), (0, 4), (4, 10)]