我是python的新手,我坚持这个练习.我应该输入一个句子并找到最长的单词.如果有两个或多个具有相同最长长度的单词,则返回第一个单词.这是我到目前为止:
def find_longest_word(word_list):
longest_word = ''
for word in word_list:
print(word, len(word))
words = input('Please enter a few words')
word_list = words.split()
find_longest_word(word_list)
Run Code Online (Sandbox Code Playgroud)
但我不知道如何比较列表并返回第一个/最长的单词.
link = "http://blog.test.com/54321&sa=U&ei=1sTFVJLfOtOC8gW0jIHYCw&ved=0CBMQFjAA"
pat = re.compile("(.*)\&(.*)")
match = re.search(pat,link)
print match.group(1)
Run Code Online (Sandbox Code Playgroud)
结果是 http://blog.test.com/54321&sa=U&ei=1sTFVJLfOtOC8gW0jIHYCw
但我需要的是http://blog.test.com/54321
如何匹配我想要的结果?请指导我,谢谢
我刚刚开始学习Angular JS,我正在尝试使用ng-repeat创建一个8X8表.我无法获得8个表头.所有表头都在一列中.这是我的代码.
<table border="1px solid black">
<tr ng-repeat="k in [0,1,2,3,4,5,6,7,8]">
<th>
column
</th>
</tr>
<tr ng-repeat="i in [0,1,2,3,4,5,6,7,8]">
<td ng-repeat="j in [0,1,2,3,4,5,6,7,8]">
[{{i}},{{j}}]
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
这是我的代码的输出.我不确定为什么会这样.

我想在用户提供的字段中进行搜索。
到目前为止我有这个:
def search_engine(model, given_field, text):
# Stuff
result = model.objects.filter(given_field__icontains=text)
return result
Run Code Online (Sandbox Code Playgroud)
过滤器内的“given_field”将是函数中给定的参数,它是一个变量。
我正在尝试在Django中创建如下的用户和关注者关系
id | user_id | follower_id
1 | 20 | 45
2 | 20 | 53
3 | 32 | 20
Run Code Online (Sandbox Code Playgroud)
为此,我做了以下事情:
class UserFollower(models.Model):
user_id = models.ForeignKey(User)
follower_id = models.ForeignKey(User)
def __str__(self):
return "{} following {}".format(self.follower_id.username, self.user_id.username)
Run Code Online (Sandbox Code Playgroud)
这里User是django.contrib.auth.models.User模型.在运行时makemigrations,我收到以下错误:
ERRORS:
AppName.UserFollower.follower_id: (fields.E304) Reverse accessor for 'UserFollower.follower_id' clashes with reverse accessor for 'UserFollower.user_id'.
HINT: Add or change a related_name argument to the definition for 'UserFollower.follower_id' or 'UserFollower.user_id'.
AppName.UserFollower.user_id: (fields.E304) Reverse accessor for 'UserFollower.user_id' clashes with reverse …Run Code Online (Sandbox Code Playgroud) 我有一个Survey和一个Choice模型,每个调查都有许多与之相关的选择.当我使用所有选项呈现实际的HTML调查页面时,我使用以下Django模板代码:
{% for choice in survey.choice_set.all %}
<li class="ui-state-default" choice_id={{ choice.id }}>{{ choice.choice_text }}</li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
然而,我不希望每次都以相同的顺序出现选择,而是希望它们以随机顺序填充以减少任何潜在的偏差效应(例如,有人可能更有可能投票选出首先出现在列表中的选项).
如果有一种方法可以在模板本身内执行此操作,那就太棒了,但似乎我更需要在views.py中的后端执行某些操作.我已经试过这个,没有效果:
class DetailView(generic.DetailView):
model = Survey
...
def get_context_data(self, **kwargs):
context = super(DetailView, self).get_context_data(**kwargs)
...
survey = get_object_or_404(Survey, survey_link__iexact=survey_link)
...
if randomize_choice_order:
survey.choice_set.order_by('?')
...
return context
Run Code Online (Sandbox Code Playgroud)
知道我怎么能做到这一点?也许我需要开发一个JS函数来在对象已经放置后随机化它们?
我正在学习Python和DJango,而且我对Linux很有用.当我创建DJango项目时,我有manage.py一个我可以执行的文件./manage.py runserver.然而,当我手动创建一些Python程序时,看起来我的Linux试图使用Bash而不是Python来执行它.所以,我需要写python foo.py代替./foo.py.这两个文件的属性manage.py和foo.py是相同的(-rwx--x---).所以我的问题是:差异在哪里以及如何在不指定的情况下执行python程序python?任何文件的链接都非常感谢.谢谢.
我试图根据 query_params 在视图集上使用不同的分页类集。
class BlockViewSet(viewsets.ModelViewSet):
pagination_class = defaultPaginationClass
def get_queryset(self):
queryset = Block.objects.all()
user = self.request.query_params.get('user', None)
if user is no None:
queryset = queryset.filter(user=user)
return queryset
Run Code Online (Sandbox Code Playgroud)
因此,如果向 发出 get 请求/block/,我想使用 defaultPagination 类,而如果向 发出请求/block/?user=1,我想使用 customPagination 类。
做一些测试.我怀疑,哪个选项更好,使用a map或a for(基于性能,空间,时间等).
map(myfunction,xrange(n))
Run Code Online (Sandbox Code Playgroud)
要么
for element in xrange(n):
myfunction(element)
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在创建一种上传文件的方法,但是我想检查文件大小,因为我只想允许 5mb 作为最大限制。
我想做这样的事情
def handle_uploaded_file(thisFile):
if thisFile > 5mb:
return "This file is more than 5mb"
else:
with open('some/file/' + str(thisFile), 'wb+') as destination:
for chunk in thisFile.chunks():
destination.write(chunk)
return "File has successfully been uploaded"
Run Code Online (Sandbox Code Playgroud) 假设我想在查询的两列之间选择最大属性。它不是整个表最大。
类似的东西:
SELECT MAX(t1.c1, t2.c2)
FROM Table1 as t1, Table2 as t2
WHERE t1.id = t2.t1_id
AND ...... here goes more conditions.
Run Code Online (Sandbox Code Playgroud)
我想每一行都有代表之间的最大映射值t1.c1和t2.c2
那可能吗 ?
python ×8
django ×6
angularjs ×1
file-upload ×1
filesystems ×1
for-loop ×1
html ×1
html-table ×1
linux ×1
map ×1
pagination ×1
postgresql ×1
python-2.7 ×1
python-3.x ×1
regex ×1
search ×1
sql ×1
url ×1