小编vin*_*lai的帖子

通过python在列表中找到不同的对

我有一个列表,我想在列表中找到不同的对.我实现了一个函数 - > different()

import numpy as np


def different(array):
    res = []
    for (x1, y1), (x2, y2) in array:
        if (x1, y1) != (x2, y2):
            res.append([(x1, y1), (x2, y2)])
    return res


a = np.array([[[1, 2], [3, 4]],
              [[1, 2], [1, 2]],
              [[7, 9], [6, 3]],
              [[3, 3], [3, 3]]])

out = different(a)  # get [[(1, 2), (3, 4)],
                    #      [(7, 9), (6, 3)]]
Run Code Online (Sandbox Code Playgroud)

还有其他更好的方法吗?我想提高我的功能不同.列表大小可能大于100,000.

python performance numpy

14
推荐指数
2
解决办法
951
查看次数

通过python获取矢量角度

我有一个矢量列表。我想在每个向量中获得角度。

import numpy as np

v = np.array([[-3, 4],
              [-2, -5],
              [2, 6],
              [3, -10]])

inv = np.arctan(v[:, 1] / v[:, 0])
degree = np.degrees(inv)
print(degree)  # get [-53.13010235,  68.19859051,  71.56505118, -73.30075577]

# use arctan2
inv = np.arctan2(v[:, 1] / v[:, 0])
degree = np.degrees(inv)
print(degree)  # get [ 126.86989765, -111.80140949,   71.56505118,  -73.30075577]
Run Code Online (Sandbox Code Playgroud)

但我想得到 [127, 248 , 71, 286] (0~360 角)。怎么解决?

python performance numpy

2
推荐指数
1
解决办法
2179
查看次数

如何使用 python Tkinter 迭代图像?

如何使用python Tkinter迭代图像?

import tkinter as tk
from PIL import ImageTk, Image

win = tk.Tk()
win.geometry('800x500')  # set window size
win.resizable(0, 0)  # fix window

images = ['01.jpg', '02.jpg', '03.jpg']

def next_img():
   # show next image

for img in images:
    img = Image.open(img)
    img = ImageTk.PhotoImage(img)
    panel = tk.Label(win, image=img)
    panel.pack()
    btn = tk.Button(text='Next image', command=next_img)
    btn.pack()
win.mainloop()
Run Code Online (Sandbox Code Playgroud)

但我的面板没有显示任何图像。我希望面板等待我,然后我单击按钮以显示下一个图像。怎么解决呢。

python tkinter

0
推荐指数
1
解决办法
4102
查看次数

python字符串替换为\u

我有一个字符串。

m = 'I have two element. <U+2F3E> and <U+2F8F>'
Run Code Online (Sandbox Code Playgroud)

我想替换为:

m = 'I have two element. \u2F3E and \u2F8F' # utf-8
Run Code Online (Sandbox Code Playgroud)

我的代码:

import re

p1 = re.compile('<U+\+') # start "<"
p2 = re.compile('>+')    # end   ">"
m = 'I have two element. <U+2F3E> and <U+2F8F>'

out = pattern.sub('\\u', m) # like: 'I have two element. \u2F3E> and \u2F8F>'
Run Code Online (Sandbox Code Playgroud)

但我收到此错误消息:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX escape
Run Code Online (Sandbox Code Playgroud)

我该如何解决。谢谢。

python

-1
推荐指数
1
解决办法
75
查看次数

标签 统计

python ×4

numpy ×2

performance ×2

tkinter ×1