我在这里是一个蟒蛇新手,我已经遇到了一个相当简单的问题 - 我正在寻找解决这个问题的最有效方法.所以,我有5个列表如下:
a,b,c,d,score
Run Code Online (Sandbox Code Playgroud)
以上列表都具有相同的大小(在我的情况下为500).a,b,c,d是字符串列表,score是一个int列表.
我想做的是a,b,c,d基于升序或降序排序score.因此,我首先要score根据降序模式进行排序,然后a,b,c,d根据排序的分数列表(按相同顺序)对相应的元素进行排序.
我enumerate想要实现这一点,但我想知道是否itertools可以在这里使用它来使它更快更有效.
关于如何实现这一目标的任何指导都将非常感谢&&抱歉,如果这是一个101问题.
我对EC2非常陌生,今天我已经在免费套餐下设置了所有内容.所以,我登录到我的ec2使用(假设默认用户ubuntu):
ssh -i ~/.ssh/mykeypaid.pem ubuntu@<my ip address>
Run Code Online (Sandbox Code Playgroud)
现在,我已经编辑了我的sudoers文件,如下所示:
ubuntu ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)
然后修改/etc/ssh/sshd_config如下:
Protocol 2
PermitRootLogin no
AuthorizedKeysFile %h/.ssh/authorized_keys
PermitEmptyPasswords no
PasswordAuthentication no
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试ssh并尝试:
sudo su -
Run Code Online (Sandbox Code Playgroud)
我被要求输入一个ubuntu密码 - 我没有这个密码.我一直在努力遵循这里的指示,但我不确定当他说:
Edit your ~/.ssh/authorized_keys and put your public key inside it. Make sure you can login without a password now once your key is in place.
我应该在〜/ .ssh/authorized_keys中包含哪些公钥?当我看到~/.ssh/authorized_keys我已经看到键开头
ssh-rsa .....
Run Code Online (Sandbox Code Playgroud)
我很感激任何建议来解决这个问题.现在,我无法回到sudoers文件 - 因为它要求我输入ubuntu我认为奇怪的密码(?)
我应该终止我的实例并重新开始吗?谢谢.
我有一个元组列表,不幸的是包含重复项,如下所示:
[(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c'), (64, u'a-b-c-ca-d-ab-ea-d-c-c'), (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (61, u'top-coldestcitiesinamerica'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, u's-s-main'), (44, u'vehicle-propulsion'), (43, u'x_file')]
Run Code Online (Sandbox Code Playgroud)
问题是元组的第一个元素(基于0的排序)是我想要检查重复的条目.所以,我可以看到:
(67, u'top-coldestcitiesinamerica')
(61, u'top-coldestcitiesinamerica')
Run Code Online (Sandbox Code Playgroud)
..重复,我想删除其中一个(类似于a set).所以,最后,我想有一个干净的元组列表,没有像这样的重复项(即元组的第一个元素没有重复):
[(67, u'top-coldestcitiesinamerica'), (66, u'ecofriendlyideastocelebrateindependenceday-phpapp'), (65, u'a-b-c-ca-d-ab-ea-d-c-c') (63, u'alexandre-meybeck-faowhatisclimate-smartagriculture-backgroundopportunitiesandchallenges'), (62, u'ghgemissions'), (58, u'infographicthe-stateofdigitaltransformationaltimetergroup'), (57, u'culture'), (55, u'cas-k-ihaveanidea'), (54, u'trendsfor'), (53, u'batteryimpedance'), (52, u'evs-howey-full'), (51, u'bericht'), (49, u'classiccarinsurance'), (47, u'uploaded_file'), (46, u'x_file'), (45, …Run Code Online (Sandbox Code Playgroud) 是否可以在同一行中声明2个数组列表?例如:
List<String> mm= new ArrayList<String>();
List<String> kk= new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
那么可以做以下事情:
List<String> mm,kk= new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
显然,当我执行上述操作时出现编译错误,并且想知道如何在同一行中声明2个列表.
是否有可能在Java中将多个for循环组合成一个循环?
例如,我有
for (i : lista)
{
//do something
}
for (j : listb)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
是否有可能将两者结合在一起?
我是python的新手,我现在非常震惊了几天,试图用urllib2发送一个cookie.所以,基本上,在我想要的页面上,我从firebug看到有一个"发送的cookie",看起来像:
list_type=height
Run Code Online (Sandbox Code Playgroud)
..基本上按照一定的顺序排列页面上的列表.
我想通过urllib2发送上面的cookie信息,以便渲染的页面实现上面的设置 - 这是我试图编写的代码,使其工作:
class Networksx(object):
def __init__(self):
self.cj = cookielib.CookieJar()
self.opener = urllib2.build_opener\
#socks handler
self.opener.addheaders = [
('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13'),
('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
('Keep-Alive', '115'),
('Connection', 'keep-alive'),
('Cache-Control', 'max-age=0'),
('Referer', 'http://www.google.com'),
("Cookie", {"list_type":"height"}),
]
urllib2.install_opener(self.opener)
self.params = { 'Set-Cookie': "list_type":"height"}
self.encoded_params = urllib.urlencode( self.params )
def fullinfo(self,url):
return self.opener.open(url,self.encoded_params).read()
Run Code Online (Sandbox Code Playgroud)
..你可以看到,我尝试了几件事:
但是,这些似乎并不像我想的那样在特定的list_order(height)中呈现页面.我想知道是否有人可以指出我如何使用urllib2发送cookie信息的正确方向
谢谢.
我对Django很新,尤其是CBV.所以,我有一个带有post方法的listView,我试图输出一个JSON,如下所示:
from django.utils import simplejson
class MyCoolListView(ListView):
# template declaration and other stuff
def get_context_data(self, **kwargs):
# do some stuff
return context
def get_queryset(self):
# do some stuff
return queryset
def post( self, request, *args, **kwargs ):
# check if user is authenticated and return json
return HttpResponse( simplejson.dump({ "score": blogpost.score }) , content_type='application/json')
Run Code Online (Sandbox Code Playgroud)
但是,POST上的HttpResponse,我得到:
TypeError: dump() takes at least 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
我不完全确定我做错了什么(我已经搜索了这个问题,但是还没有运气) - 我想知道是否有人遇到过这样的情况/错误信息.任何解决这个问题的指导都将非常感激.
我对bash脚本编程比较陌生,而且我有以下脚本,它没有给我我期望的结果.所以,我的脚本看起来像这样:
#!/bin/bash
echo "Today is $(date)"
shopt -s nullglob
FILES=/some/empty/dir/with/no/text/files/*.txt
#echo $FILES
if [ -z "$FILES" ]
then
echo 'FILES variable is empty'
exit
else
echo 'FILES variable is not empty'
echo 'done' > write_file_out.dat
fi
Run Code Online (Sandbox Code Playgroud)
因此,我尝试使用的目录FILES是完全空的 - 但是,当我这样做if [ -z "$FILES" ]时似乎说它不是空的.
对此感到困惑 - 想知道是否有人可以指出我正确的方向.
我有一个字符串,它有三种形式:
XhYmZs or YmZs or Zs
Run Code Online (Sandbox Code Playgroud)
其中,h,m,s 表示小时、分钟、秒,X、Y、Z 是相应的值。
如何在 python2.7 中有效地将这些字符串转换为秒?
我想我可以做这样的事情:
s="XhYmZs"
if "h" in s:
hours=s.split("h")
elif "m" in s:
mins=s.split("m")[0][-1]
Run Code Online (Sandbox Code Playgroud)
...但这对我来说似乎不是很有效:(
我发现有两种使用 python-reuqests 下载图像的方法。
使用 PIL,如文档中所述(https://requests.readthedocs.io/en/master/user/quickstart/#binary-response-content):
from PIL import Image
from io import BytesIO
i = Image.open(BytesIO(r.content))
Run Code Online (Sandbox Code Playgroud)
使用流式响应内容:
r = requests.get(url, stream=True)
with open(image_name, 'wb') as f:
for chunk in r.iter_content():
f.write(chunk)
Run Code Online (Sandbox Code Playgroud)
然而,推荐的下载图像方式是什么?我认为两者都有其优点,我想知道最佳方法是什么。
python ×5
java ×2
amazon-ec2 ×1
bash ×1
django ×1
django-views ×1
ec2-ami ×1
json ×1
linux ×1
loops ×1
python-2.7 ×1
python-3.x ×1
simplejson ×1
ubuntu-12.04 ×1
urllib2 ×1