我试图使用mergesort - 我得到 - 来计算列表中的拆分反转次数(也就是说,未排序列表的前半部分中的元素应该出现在后半部分中的给定元素之后)未排序的列表;例如[3 2 1 4]将包含拆分反转(3,1),但不包含(3,2),因为3和2都在前半部分).当我得到最终的打印语句时,我得到了我期望的答案 - 在这种情况下为9 - 但返回值都是因为它通过递归返回拆分值而变得很难.我已经尝试了各种索引组合无济于事.有帮助吗?(使用Python 2.7)
(为了记录,这是一个Coursera的家庭作业问题,但我只是为了好玩而学习 - 除了我以外,没有人对此进行评分.)
def mergesort(lst):
'''Recursively divides list in halves to be sorted'''
if len(lst) is 1:
return lst
middle = int(len(lst)/2)
left = mergesort(lst[:middle])
right = mergesort(lst[middle:])
sortedlist = merge(left, right)
return sortedlist
def merge(left, right):
'''Subroutine of mergesort to sort split lists. Also returns number
of split inversions (i.e., each occurence of a number from the sorted second
half of the list appearing before a …Run Code Online (Sandbox Code Playgroud) 我在 OS 10.8.5 上运行 Python 2.7.2
尝试使用 pymssql,但出现以下错误:
File "time_reporting.py", line 32, in <module>
import pymssql
File "/Users/xx/.virtualenvs/oracle/lib/python2.7/site-packages/pymssql.py", line 30, in <module>
import _mssql, types, string, time, datetime, warnings
ImportError: No module named _mssql
Run Code Online (Sandbox Code Playgroud)
我试过用 pip 和 brew 安装 mssql 只是被告知没有那个名字的包。文档似乎没有涵盖安装。
编辑:
当我尝试时,pip install pymssql我得到:
Downloading/unpacking pymssql
Running setup.py egg_info for package pymssql
setup.py: platform.system() => 'Darwin'
setup.py: platform.architecture() => ('32bit', '')
setup.py: platform.linux_distribution() => ('', '', '')
setup.py: platform.libc_ver() => ('', '')
setup.py: Detected Darwin/Mac OS X. …Run Code Online (Sandbox Code Playgroud) 我已经阅读了与此类似的所有SO问题,我很丢失.我收到以下错误:
2015-09-14 22:59:40.455 guess-who[60143:9602686] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7c0f8e30 UIImageView:0x7b6efb60.top == _UILayoutGuide:0x7c0f67a0.top>",
"<_UILayoutSupportConstraint:0x7c0f8ae0 V:[_UILayoutGuide:0x7c0f67a0(0)]>", …Run Code Online (Sandbox Code Playgroud) 我正在寻找能让我在Apple News文档中将一个项目放在其他项目前面的东西 - 相当于CSS中的z-index.我在文档中找不到任何似乎适用的内容.
我正试图以我的酥脆形式制作我的一些田地.我原以为这会很简单,但它不起作用.没有错误,只是没有读取.
class ApplicationForm(BaseItemForm):
jira_bucket = forms.CharField(
widget=autocomplete_light.ChoiceWidget('JiraAutocomplete'))
def __init__(self, *args, **kwargs):
''' Change form based on permissions of the user '''
super(ApplicationForm, self).__init__(*args, **kwargs)
# app = self.instance
if not self.user.has_perm('Assets.sarbox_fields'):
disabled_widget = forms.CheckboxInput(attrs={'readonly': True})
self.fields['sarbox'].widget = disabled_widget
self.fields['pii'].widget = disabled_widget
self.fields['pci'].widget = disabled_widget
if 'sarbox_approver' in self.fields:
self.fields['sarbox_approver'].widget.attrs['disabled'] = True
# it's about to get crispy
self.helper = FormHelper()
# self.helper.form_class = 'application-form' # done in template
self.helper.form_tag = False
self.helper.disable_csrf = True
self.helper.layout = Layout(
Div(
Field('name'),
Field('jira_bucket'), …Run Code Online (Sandbox Code Playgroud) 我想使用 django_compressor 但它在我的生产环境中不起作用。
在开发 ( DEBUG=True) 中,它正在工作并创建了.sass-cache&CACHE文件夹。
我的settings.py是
DEBUG = False
TEMPLATE_DEBUG = False
INSTALLED_APPS = (
...,
'django.contrib.staticfiles',
'compressor',
'myapp',
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'com.app.static')?
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
COMPRESS_ENABLED = True
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_PRECOMPILERS = (
('text/x-scss', 'sass --scss {infile} {outfile}'),
)
MEDIA_URL = '/media/'
Run Code Online (Sandbox Code Playgroud)
scss 文件将模板目录放在 app.scss 文件中。
{% load staticfiles %}
{% load compress %}
<html> …Run Code Online (Sandbox Code Playgroud) 我在Storyboard中定义了一个标签,我试图以编程方式更改其位置.关于SO的一些现有问题似乎已经解决了这个问题,但是没有一个解决方案似乎有效(即标签不会移动).我删除了标签上的所有现有约束无济于事.我试过了:
class LandingViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.frame = CGRectMake(100, 25, titleLabel.frame.size.width, titleLabel.frame.size.height)
}
Run Code Online (Sandbox Code Playgroud)
我也试过了
titleLabel.center = CGPointMake(120, 150)
代替 titleLabel.frame
我错过了什么吗?
我无法amp-img显示这个。它用作图像,当我单击src-link时,它会正确加载到单独的页面上。将其更改为amp-img使其消失。我background-color: red在上设置amp-img,该框正确显示,但是它是一个空的红色框。
HTML:
<li>
<a href="/">
<amp-img src="https://d12v9rtnomnebu.cloudfront.net/oursite/logo_white.png" alt="site logo" width="264" height="96"/>
</a>
</li>
Run Code Online (Sandbox Code Playgroud)
CSS(可能有很多多余的东西需要修剪,但在工作之前不知道什么是重要的):
.amp .site-menu li {
height: 32px;
line-height: 32px;
}
.amp .site-menu li:first-child {
float: left;
min-width: 50px;
max-width: 180px;
width: 100%;
display: block;
}
.amp .site-menu a {
min-width: 50px;
max-width: 200px;
width: 100%;
display: block;
}
.amp .site-menu amp-img {
height: 32px;
min-width: 50px;
max-width: 200px;
width: 100%;
float: left;
display: block;
background-color: red;
}
Run Code Online (Sandbox Code Playgroud) 我目前有一个'博客'应用程序,它将根据用户名显示不同用户的博客帖子.这就是我的urls.py看起来像.
urlpatterns = patterns('blog.views',
url(r'^(?P<blog_author>[^/]+)/$', 'entries', name='blog'),
url(r'^(?P<blog_author>[^/]+)/(?P<entry_slug>[^/]+)/$', 'blog_entry', name='blog_entry'),
)
Run Code Online (Sandbox Code Playgroud)
因此,blog/authorname/将列出该特定作者的所有blogpost条目,blog/authorname/foo-post/并将呈现特定的博客帖子.我想要做的是永久地将校友成员的博客重定向到blog/alumni/authorname和blog/alumni/authorname/foo-post/.我在我的用户模型中添加了一个字段,用于指示用户是否是校友.
对于普通用户和校友,views功能基本相同.这是我到目前为止一直在尝试做的事情:在entries视图函数中,我添加了一些可以检查校友成员的行.如果某一个博客作者是校友,会HttpResponsePermanentRedirect以blogs/alumni/alumni_author/和自渲染部分是一样的,调用同一个entries视图功能.所以基本上,我的entries功能是尝试检查成员类型,为普通和校友成员呈现视图.
我正在制作一个注册表格供用户使用 Django 1.8 和 python 3.5 进行注册
我创建了一个用户(使用自定义模型扩展 AbstractUser 扩展用户模型)(即,我想将自定义字段添加到默认的 django 用户表中,例如生物、出生日期等)
这是我的 mainpage/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
Run Code Online (Sandbox Code Playgroud)
这是 signup/view.py
from django.shortcuts import render
from mainpage.models import User
def signup(request):
form1=User(request.POST or None)
if form1.is_valid():
form1.save()
context = {
"form1": form1,
}
return render(request, "signup.html",context)
Run Code Online (Sandbox Code Playgroud)
追溯
Environment:
Request Method: GET
Request URL: http://localhost:8000/signup/
Django Version: 1.8
Python Version: 3.5.4
Installed Applications:
('django.contrib.admin', …Run Code Online (Sandbox Code Playgroud)