我有一个字典y = {6:34,5:40,3:70,2:80}和列表m = [5,2,3],其中仅包含字典y的某些键,我必须根据字典的值进行排序,y = {2:80,3:70,5:40,6:34}并且仅考虑列表m中存在的键,并且最终结果应该已排序m =[2,3,5]
给定一个字符串中datetime的格式strftime('%b %d, %Y %I:%M %p')
如:'Apr 17, 2016 02:00 AM'
有没有办法检查这段时间是否已经过去了?喜欢可能比较
datetime.datetime.now().strftime('%b %d, %Y %I:%M %p')
Run Code Online (Sandbox Code Playgroud)
不知何故到字符串 'Apr 17, 2016 02:00 AM'
由于服务器GPU限制,我们无法使用 K 渲染 上的隐式方程VPS,下面是我们用来从 中的方程生成 3D 模型的示例代码mayavi:
import numpy as np
from mayavi import mlab
mlab.clf()
x, y, z = np.mgrid[-2.5:2.5:.125, -2.5:2.5:.125, -2.5:2.5:.125]
a,b,c = 0.0,-5.0,11.8
values = x**4+y**4+z**4+a*(x**2+y**2+z**2)**2+b*(x**2+y**2+z**2)+c
mlab.contour3d(x, y, z, values, contours=[0], color=(.1,1,.5))
mlab.savefig("shape.obj")
Run Code Online (Sandbox Code Playgroud)
以 3D 形式输出此形状
我的问题是,我怎样才能完成同样的事情vispy另一个 python 模块(如果存在)完成同样的事情
我们没有显卡VPS,我们在 Windows Server 2008 上使用 python 3.5.1 (anaconda)(不可能使用 linux),并且我们已替换opengl32.dll为 system32 中的 mesa(GL 版本 3.0 mesa 10.2.8)(VTK 抛出错误 GL 版本 2.1 及其gpu_shader4扩展不受支持...)
好吧,我正在编写一个代码,它可以为您输入的加扰字母提供所有可能的组合.这里是:
import random, math
words = []
original = raw_input("What do you need scrambled? ")
def word_scramble(scrambled):
original_length = len(scrambled)
loops = math.factorial(original_length)
while loops > 0:
new_word = []
used_numbers = []
while len(new_word) < original_length:
number = random.randint(0, original_length - 1)
while number in used_numbers:
number = random.randint(0, original_length - 1)
while number not in used_numbers:
used_numbers.append(number)
new_word.append(scrambled[number])
if new_word not in words:
words.append(("".join(str(x) for x in new_word)))
loops -= 1
word_scramble(original)
print ("\n".join(str(x) for x in words)) …Run Code Online (Sandbox Code Playgroud) 所以我想比较两个矩阵(大小:98000 x 64)。比较应该逐个元素进行,我想将每个比较的最小值存储在具有相同维度的第三个矩阵中。我还希望在不使用循环的情况下进行比较!
这是一个小例子:
a=np.array([1,2,3])
b=np.array([4,1,2])
Run Code Online (Sandbox Code Playgroud)
比较 1 和 4、2 和 1 以及 3 和 2 并将其存储在向量 c 中的函数
回答
c=[1,1,2]
Run Code Online (Sandbox Code Playgroud)
有没有一种有效的方法来做到这一点?
我正在寻找做类似事情的最pythonic方式:
a = [1,2,3,4,5,6,7,8]
b = ['a','b','c']
c = replace(a,b,2)
c is now [1,2,'a','b','c',6,7,8]
Run Code Online (Sandbox Code Playgroud) 如果我想从句子的编号中拆分字符串: "It was amazing in 2016"
我用:
re.split('\s*((?=\d+))
out: 'It was amazing in', '2016'
Run Code Online (Sandbox Code Playgroud)
现在我想做相反的事情,所以如果一个句子以一个数字开头,然后跟着一个字符串,比如: '2016 was amazing'
我希望结果是: '2016', 'was amazing'
我有 3 个 numpy 数组,用于存储形状 (4,100,100) 的图像数据。
arr1= np.load(r'C:\Users\x\Desktop\py\output\a1.npy')
arr2= np.load(r'C:\Users\x\Desktop\py\output\a2.npy')
arr3= np.load(r'C:\Users\x\Desktop\py\output\a3.npy')
Run Code Online (Sandbox Code Playgroud)
我想将所有 3 个数组合并为 1 个数组。我已经尝试过这种方式:
merg_arr = np.zeros((len(arr1)+len(arr2)+len(arr3), 4,100,100), dtype=input_img.dtype)
Run Code Online (Sandbox Code Playgroud)
现在这制作了一个所需长度的数组,但我不知道如何复制这个数组中的所有数据。可能正在使用循环?
我有一个.txt文件,其中生成了许多Snort警报.我想搜索此文件并删除重复的警报,并只保留其中一个.到目前为止我使用以下代码:
with open('SnortReportFinal', 'r') as f:
file_lines = f.readlines()
cont_lines = []
for line in range(len(file_lines)):
if re.search('\d:\d+:\d+', file_lines[line]):
cont_lines.append(line)
for idx in cont_lines[1:]: # skip one instance of the string
file_lines[idx] = "" # replace all others
with open('SnortReportFinal', 'w') as f:
f.writelines(file_lines)
Run Code Online (Sandbox Code Playgroud)
正则表达式匹配我正在搜索的字符串,即1:234:5,如果它找到相同字符串的多个实例,我希望它删除它们并且只保留一个.这不起作用,因为所有其他字符串都被删除,并且它只保留表达式匹配的一个字符串.
文件包含这样的文字:
[1:368:6] ICMP PING BSDtype [**]
[1:368:6] ICMP PING BSDtype [**]
[1:368:6] ICMP PING BSDtype [**]
[1:368:6] ICMP PING BSDtype [**]
Run Code Online (Sandbox Code Playgroud)
部分[1:368:6]可以是数字的变体,即[1:5476:5].
我希望我的预期输出只是:
[1:368:6] ICMP PING BSDtype [**]
[1:563:2] ICMP PING BSDtype [**]
Run Code Online (Sandbox Code Playgroud)
其余的字符串被删除,休息我的意思是数字的差异是好的,但不是重复的数字.
如果我有一个列表,说[7,6,5,4,3,2,1]如何让它每隔一个数字添加upp,例如7 + 5 + 3 + 1?
我尝试过添加mylist[0] + mylist[2]等,但这非常繁琐.
python ×10
list ×2
numpy ×2
python-3.x ×2
regex ×2
3d ×1
arrays ×1
datetime ×1
dictionary ×1
mayavi ×1
python-2.7 ×1
replace ×1
vispy ×1