小编Eva*_*olo的帖子

如何遍历包含列表和删除项目的dict值?

Python新手在这里.我有一个列表字典,如下:

d = {
  1: ['foo', 'foo(1)', 'bar', 'bar(1)'],
  2: ['foobaz', 'foobaz(1)', 'apple', 'apple(1)'],
  3: ['oz', 'oz(1)', 'boo', 'boo(1)']
}
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚如何循环字典的键和相应的列表值,并删除列表中的每个字符串与parantheses尾部.到目前为止,这就是我所拥有的:

for key in keys:
    for word in d[key]...: # what else needs to go here?
        regex = re.compile('\w+\([0-9]\)')
        re.sub(regex, '', word)  # Should this be a ".pop()" from list instead?
Run Code Online (Sandbox Code Playgroud)

我想用列表理解来做这个,但正如我所说,我找不到有关循环通过dict键和列表的相应dict值的更多信息.设置它的最有效方法是什么?

python python-2.7

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

Web应用程序在加载AngularJS之前尝试检索数据

我正在添加图像到divs ng-repeat工作正常.图像位置数据嵌套在一个数组中.图像显示为预期.我的部分看起来像:

<div class="image-wrapper">
  <img src={{item.user.img_src}}>
</div>
Run Code Online (Sandbox Code Playgroud)

但是,看起来页面是在加载AngularJS之前尝试检索字符串.我得到以下控制台警告:

GET http://localhost:3000/%7B%7Bitem.user.img_src%7D%7D 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)

我该如何防止这种情况?

angularjs

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

计算 HTML5 画布上绘制的形状内的像素数?

我正在使用sketch.js在 HTML5 画布上动态绘制非常简单的形状。

有没有办法计算画布上绘制的闭合形状内的像素数?

html jquery html5-canvas

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

我如何告诉 jQuery 在加载时获取元素的 value 属性?

我正在使用模板引擎将用户名插入到 div 内容中:

<div class="username" value="#{user}"></div>
Run Code Online (Sandbox Code Playgroud)

我如何告诉 jQuery 获取value页面加载时的内容?

jquery

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

Python:`choice()`选择相同的选择

我正在尝试编写一个简单的数学表达式生成器.我遇到的问题是使用从一个范围内选择的随机数表达式,并在每个数字之间插入一个随机运算符.

这是我到目前为止所拥有的:

from random import randint
from random import choice

lower = int(raw_input("Enter a lower integer constraint: "))
higher = int(raw_input("Enter a higher integer constraint: "))

def gen_randoms(lower, higher):
    integers = list()
    for x in xrange(4):
        rand_int = randint(lower, higher)
        integers.append(rand_int)
    return integers

def gen_equations(integers):
    nums = map(str, integers)
    print nums
    operators = ['*', '+', '-']
    equation = 'num op num op num op num'
    equation = equation.replace('op', choice(operators))
    equation = equation.replace('num', choice(nums))
    print equation

nums = gen_randoms(lower, higher)
gen_equations(nums) …
Run Code Online (Sandbox Code Playgroud)

python

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

标签 统计

jquery ×2

python ×2

angularjs ×1

html ×1

html5-canvas ×1

python-2.7 ×1