首先,免责声明:我对YAML不太熟悉.我正在尝试将YAML文档解析为关键值对(不要担心我是如何做的.我已经处理好了这一点)
我的文件过去看起来像:
world:
people:
name:Suzy
address:chez-bob
Run Code Online (Sandbox Code Playgroud)
然后,有人去改变它.
world:
people:
name:!$uzy
address:chez-bob
Run Code Online (Sandbox Code Playgroud)
我得到这个解析错误:
yaml.constructor.ConstructorError: could not determine a constructor for the tag '!$uzy'
Run Code Online (Sandbox Code Playgroud)
这甚至意味着什么?我怎么会去解释它!$只有两个字符?我只想要一个字符串键和值的字典!此外,编辑yaml文件不是一个选项.必须使用解析器在代码中修复问题.
我想用列表中的子列表替换a另一个子列表.像这样的东西:
a=[1,3,5,10,13]
Run Code Online (Sandbox Code Playgroud)
让我们说我想要一个子列表,如:
a_sub=[3,5,10]
Run Code Online (Sandbox Code Playgroud)
并替换它
b_sub=[9,7]
Run Code Online (Sandbox Code Playgroud)
所以最终的结果将是
print(a)
>>> [1,9,7,13]
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我在模块mod1中的类中创建一个方法,并按如下方式调用它:
class blahblah:
def foobar(self, bvar, **dvar)
////
return dvar
Run Code Online (Sandbox Code Playgroud)
并将其称为:
obj1 = mod1.blahblah()
dvar1 = obj1.foobar(True, **somedictionary)
Run Code Online (Sandbox Code Playgroud)
它扔了一个 Attribute error: blahblah has no attribute named foobar
你能帮帮我吗?提前致谢
我正在创建一个从列表中返回最长字符串值的函数.当只有一个字符串最多的字符串时,我的代码才有效.我试图让它打印所有最长的字符串,如果有多个字符串,我不希望它们被重复.当我运行它时,它只返回'hello',而我希望它还返回'ohman'和'yoloo'.我觉得这个问题在线if item not list:,但我已经尝试了一切,但它不起作用.
list = ['hi', 'hello', 'hey','ohman', 'yoloo', 'hello']
def length(lists):
a = 0
answer = ''
for item in lists:
x = len(item)
if x > a:
a = x
answer = item
elif x == a:
if item not in list:
answer = answer + ' ' + item
return answer
print length(list)
Run Code Online (Sandbox Code Playgroud) 我是期货模块的新手,有一项可以从并行化中受益的任务; 但我似乎无法弄清楚如何设置线程的功能和进程的功能.我很感激任何人都可以帮助解决这个问题.
我正在运行粒子群优化(PSO).在没有详细了解PSO本身的情况下,这里是我的代码的基本布局:
有一个Particle类,有一个getFitness(self)方法(计算一些指标并存储它self.fitness).PSO模拟具有多个粒子实例(对于某些模拟,容易超过10; 100s甚至1000s).
每隔一段时间,我就要计算粒子的适应度.目前,我在for循环中执行此操作:
for p in listOfParticles:
p.getFitness(args)
Run Code Online (Sandbox Code Playgroud)
但是,我注意到每个粒子的适应度可以彼此独立地计算.这使得该适应度计算成为并行化的主要候选者.的确,我能做到map(lambda p: p.getFitness(args), listOfParticles).
现在,我可以轻松地做到这一点futures.ProcessPoolExecutor:
with futures.ProcessPoolExecutor() as e:
e.map(lambda p: p.getFitness(args), listOfParticles)
Run Code Online (Sandbox Code Playgroud)
由于调用的副作用p.getFitness存储在每个粒子本身,我不必担心从中返回futures.ProcessPoolExecutor().
到现在为止还挺好.但现在我注意到ProcessPoolExecutor创建了新进程,这意味着它会复制内存,这很慢.我希望能够共享内存 - 所以我应该使用线程.这很好,直到我意识到在每个进程中运行多个线程的几个进程可能会更快,因为多个线程仍然只运行在我的甜蜜的8核机器的一个处理器上.
这就是我遇到麻烦的地方:
根据我见过的例子,ThreadPoolExecutor对a进行操作list.那样做ProcessPoolExecutor.所以我不能做任何迭代ProcessPoolExecutor到农场,ThreadPoolExecutor因为那样ThreadPoolExecutor就会得到一个单一的对象(参见我的尝试,发布在下面).
另一方面,我不能自己切片listOfParticles,因为我想做ThreadPoolExecutor自己的魔术来弄清楚需要多少线程.
所以,最重要的问题(最后):
我应该如何构建我的代码,以便我可以使用进程和线程有效地并行化以下内容:
for p in listOfParticles:
p.getFitness()
Run Code Online (Sandbox Code Playgroud)
这是我一直在尝试的,但我不敢尝试运行它,因为我知道它不会起作用:
>>> def threadize(func, L, …Run Code Online (Sandbox Code Playgroud) python multithreading multiprocessing python-3.3 concurrent.futures
我知道我可以将奇异值作为键中的哈希值dict.例如,我可以将哈希5作为其中一个键dict.
我目前面临一个问题,需要我散列一系列值.
基本上,我需要一种更快的方法来做到这一点:
if 0 <= x <= 0.1:
# f(A)
elif 0.1 <= x <= 0.2:
# f(B)
elif 0.2 <= x <= 0.3:
# f(C)
elif 0.3 <= x <= 0.4:
# f(D)
elif 0.4 <= x <= 0.5:
# f(E)
elif 0.5 <= x <= 0.6:
# f(F)
Run Code Online (Sandbox Code Playgroud)
其中x是一些float任意精度的参数.
我能想到的最快的方法是散列,但问题在于:我可以(0.1, 0.2)用作键,但这仍然会花费我O(n)运行时间,并且最终并不比elifs的更好(我必须迭代键并检查是否key[0] <= x <= key[1]).
有没有办法散列一系列值,以便我可以检查哈希表0.15,仍然得到#execute B? …
我有一组字符串数组,我想从这里删除重复的元素...
String[] arr1 = {"a1","b1"};
String[] arr2 = {"a2","b2"};
Set<String[]> mySet = new HashSet<String[]>();
mySet.add(arr1);
mySet.add(arr2);
mySet.add(new String[] {"a1","b1"});
System.out.print(mySet.size());
Run Code Online (Sandbox Code Playgroud)
目前mySet看起来像这样:
[{"a1","b1"},{"a2","b2"},{"a1","b1"}]
Run Code Online (Sandbox Code Playgroud)
但我想这样:
[{"a1","b1"},{"a2","b2"}]
Run Code Online (Sandbox Code Playgroud)
我知道一些方法......
我编写了一个函数来返回一个生成器,该生成器包含给定长度的每个子串的唯一组合,该子串包含来自主串的n个元素.
作为说明:
如果我有'abcdefghi'和长度为2的探测器,并且每个列表的阈值为4个元素我想得到:
['ab', 'cd', 'ef', 'gh']
['ab', 'de', 'fg', 'hi']
['bc', 'de', 'fg', 'hi']
Run Code Online (Sandbox Code Playgroud)
我对此问题的第一次尝试涉及返回列表列表.这最终溢出了计算机的内存.作为一个粗略的二次解决方案,我创建了一个类似的东西.问题是我创建了一个自己调用的嵌套生成器.当我运行这个函数时,它似乎只是循环内部for循环而不再实际调用自己.我认为生成器会在必要时在递归孔之前,直到它达到yield语句.有什么线索发生了什么?
def get_next_probe(self, current_probe_list, probes, unit_length):
if isinstance(current_probe_list, list):
last_probe=current_probe_list[-1]
available_probes = [candidate for candidate in probes if candidate.start>last_probe.end]
else:
available_probes = [candidate for candidate in probes if candidate.start<unit_length]
if available_probes:
max_position=min([probe.end for probe in available_probes])
available_probes2=[probe for probe in available_probes if max_position+1>probe.start]
for new_last_probe in available_probes2:
new_list=list(current_probe_list)
new_list.append(new_last_probe)
self.get_next_probe(new_list, probes, unit_length)
else:
if len(current_probe_list)>=self.num_units:
yield current_probe_list
Run Code Online (Sandbox Code Playgroud)
如果将产量更改为打印,则此工作正常!我很感激能得到的任何帮助.我意识到这不是这种类型的搜索问题的最佳实现,它似乎从最后一次调用get_next_probe返回找到的位置列表,并为不重叠的元素过滤此列表new_last_probe.end会更有效率......但这对我来说要容易得多.任何算法输入仍然会被欣赏.
谢谢!
如何使用Scrapy迭代网站?我想提取匹配的所有网站的正文http://www.saylor.org/site/syllabus.php?cid=NUMBER,其中NUMBER是1到400左右.
我写过这个蜘蛛:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from syllabi.items import SyllabiItem
class SyllabiSpider(CrawlSpider):
name = 'saylor'
allowed_domains = ['saylor.org']
start_urls = ['http://www.saylor.org/site/syllabus.php?cid=']
rules = [Rule(SgmlLinkExtractor(allow=['\d+']), 'parse_syllabi')]
def parse_syllabi(self, response):
x = HtmlXPathSelector(response)
syllabi = SyllabiItem()
syllabi['url'] = response.url
syllabi['body'] = x.select("/html/body/text()").extract()
return syllabi
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我知道它正在寻找start_url中的链接,这不是我想要的.我想迭代这些网站.合理?
谢谢您的帮助.
在我写这篇文章的时候,对我来说,我实际上遇到了这个问题.
我有一个对象列表.这些对象中的每一个都是Individual我编写的类的实例.
因此,传统智慧说isinstance(myObj, Individual)应该回归True.但事实并非如此.所以我认为我的编程中存在一个错误并打印出来type(myObj),令我惊讶的是打印instance并myObj.__class__给了我Individual!
>>> type(pop[0])
<type 'instance'>
>>> isinstance(pop[0], Individual) # with all the proper imports
False
>>> pop[0].__class__
Genetic.individual.Individual
Run Code Online (Sandbox Code Playgroud)
我很难过!是什么赋予了?
编辑:我的个人课程
class Individual:
ID = count()
def __init__(self, chromosomes):
self.chromosomes = chromosomes[:] # managed as a list as order is used to identify chromosomal functions (i.e. chromosome i encodes functionality f)
self.id = self.ID.next()
# other methods
Run Code Online (Sandbox Code Playgroud)