任务是找到整数中的奇数个数.
count_odd_digits(n):
Run Code Online (Sandbox Code Playgroud)
给定一个非负整数,计算它的奇数位数.
例:
count_odd_digits(123450) ? 3 #digits 1,3, and 5 are odd
Run Code Online (Sandbox Code Playgroud)
我到目前为止:
def count_odd_digits(n):
ans = 0
for i in str(n):
if int(n) %2 == 1:
ans += 1
elif n[i]==0:
return None
Run Code Online (Sandbox Code Playgroud)
但我仍然没有通过测试,我的代码出了什么问题?
我想用Python3安装IPython
sudo pip install ipython
在openSUSE 13.1上.由于我只为Python3安装了pip,它应该可以工作,实际上适用于其他软件包.我得到了以下错误:
Downloading/unpacking ipython
Downloading ipython-4.1.2.tar.gz (5.0MB): 5.0MB downloaded
Running setup.py egg_info for package ipython
error in ipython setup command: Invalid environment marker: sys_platform == "darwin" and platform_python_implementation == "CPython"
Complete output from command python setup.py egg_info:
error in ipython setup command: Invalid environment marker: sys_platform == "darwin" and platform_python_implementation == "CPython"
Run Code Online (Sandbox Code Playgroud)
有趣的是,IPython可以工作,但是在通过YaST安装后从Python 2.7.6开始.出了什么问题?
我有一个列表,我随机打印其中一个项目,但我想从列表中打印另一个随机项目,我想100%确定它不是前一个.
import random
i = 0
Names = ["Andrew", 'John', 'Jacob','Bob']
for l in Names:
i += 1
c = random.randrange(0,i)
print(Names[c])
Run Code Online (Sandbox Code Playgroud) 本质上,我有一个巨大的文件,所有文件每行包含多个单词,每个单词用空格分隔。有点像这样:
WORD WORD WORD WORD
ANOTHER
WORD SCRABBLE BLAH
YES NO
Run Code Online (Sandbox Code Playgroud)
我想要做的是将文件中的所有单词放入一个巨大的列表中,我尝试使用 split 但这并没有考虑到新行(\ n)
什么是正确的anaconda加速功能来检查cuda?
使用numba-pro,您可以使用:
>>> from numbapro import check_cuda
numbapro:1: ImportWarning: The numbapro package is deprecated in favour of the accelerate package. Please update your code to use equivalent functions from accelerate.
>>> check_cuda()
CUDA is not available...
Run Code Online (Sandbox Code Playgroud)
要么
>>> numbapro.check_cuda()
------------------------------libraries detection-------------------------------
Finding cublas
located at /home/usr/miniconda3/envs/cuda/lib/libcublas.so.7.0.28
trying to open library... ok
Finding cusparse
located at /home/usr/miniconda3/envs/cuda/lib/libcusparse.so.7.0.28
trying to open library... ok
Finding cufft
located at /home/usr/miniconda3/envs/cuda/lib/libcufft.so.7.0.35
trying to open library... ok
Finding curand
located at /home/usr/miniconda3/envs/cuda/lib/libcurand.so.7.0.28
trying to open library... …Run Code Online (Sandbox Code Playgroud) 目前,我只是测试它,并在我理解后将其添加到我的程序中.我只想知道如何在列表中找到列表的长度.
我试过了:
l = [["1","2","3"],["1","2","3","4"],["1","2","3","4","5"]]
[print(len(x)) for x in l[0]]
Run Code Online (Sandbox Code Playgroud)
但它只打印了3次.我只需要打印(或返回)每个列表的长度l.
如何将我的PopupWindow放入系统桌面或其他应用程序?我该如何使用此权限:INTERNAL_SYSTEM_WINDOW
任何例子?
我创建了一个模板类,我做了一个removeMiddle像这样的方法:
template <typename T> void List<T>::removeMiddle(int pos){
((getNodeAt(pos)->next)->prev) = (getNodeAt(pos)->prev);
((getNodeAt(pos)->prev)->next) = (getNodeAt(pos)->next);
delete getNodeAt(pos);
}
Run Code Online (Sandbox Code Playgroud)
该方法getNodeAt(int)运行列表并返回所需的特定节点.
我所做的可以导致我的应用程序性能下降吗?因为我想这样做:
template <typename T> void List<T>::removeMiddle(int pos){
node *aux = getNodeAt(pos);
(aux->next)->prev = aux->prev;
(aux->prev)->next = aux->next;
delete aux;
}
Run Code Online (Sandbox Code Playgroud)
第二种方法比第一种方法更快吗?
我是Python新手,在重写旧Python脚本的过程中,我遇到了以下几点:
value1 = 'some val 1'
value2 = 'some val 2'
some_list = #list of values
if (value1, value2) in some_list:
Run Code Online (Sandbox Code Playgroud)
这是否检查value1,并value2在列表中?
我用谷歌搜索了如何做到这一点,答案显示了不同的方法,我没有看到任何使用上述代码的建议.
这是在做别的吗?我应该保留还是改变它?
我正在尝试获取包括3XX在内的http状态代码,但是无法从我的代码中打印出来。
这是代码:
import urllib
import urllib.request
import urllib.error
urls = ['http://hotdot.pro/en/404/', 'http://www.google.com', 'http://www.yandex.ru', 'http://www.python.org', 'http://www.voidspace.org.uk']
fh = open("example.txt", "a")
def getUrl(urls):
for url in urls:
try:
with urllib.request.urlopen(url) as response:
requrl = url
the_page = response.code
fh.write("%d, %s\n" % (int(the_page), str(requrl)))
except (urllib.error.HTTPError, urllib.error.URLError) as e:
requrl = url
print (e.code)
fh.write("%d, %s\n" % (int(e.code), str(requrl)))
getUrl(urls)
Run Code Online (Sandbox Code Playgroud)
有人可以帮我弄这个吗?
我用 Python 编写了一个简单的游戏,但出现错误。
import pygame
import random
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
RED = ( 255, 0, 0)
BLUE = ( 0, 0, 255)
class Block(pygame.sprite.Sprite):
def __init__(self, color):
super().__init__()
self.image = pygame.Surface([20, 15])
self.image.fill(color)
self.rect = self.image.get_rect()
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface([15, 20])
self.image.fill(RED)
self.rect = self.image.get_rect()
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos[0]
class Bullet(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.Surface([4, 10])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
def update(self): …Run Code Online (Sandbox Code Playgroud) import urllib.request
import re
import csv
import pandas as pd
from bs4 import BeautifulSoup
columns = []
data = []
f = open('companylist.csv')
csv_f = csv.reader(f)
for row in csv_f:
stocklist = row
print(stocklist)
for s in stocklist:
print('http://finance.yahoo.com/q?s='+s)
optionsUrl = urllib.request.urlopen('http://finance.yahoo.com/q?s='+s).read()
soup = BeautifulSoup(optionsUrl, "html.parser")
stocksymbol = ['Symbol:', s]
optionsTable = [stocksymbol]+[
[x.text for x in y.parent.contents]
for y in soup.findAll('td', attrs={'class': 'yfnc_tabledata1','rtq_table': ''})
]
if not columns:
columns = [o[0] for o in optionsTable] #list(my_df.loc[0])
data.append(o[1] for o …Run Code Online (Sandbox Code Playgroud) python ×9
python-3.x ×8
accelerate ×1
anaconda ×1
android ×1
c++ ×1
datareader ×1
file ×1
finance ×1
ipython ×1
linked-list ×1
list ×1
loops ×1
nested-lists ×1
numba-pro ×1
opensuse ×1
pandas ×1
performance ×1
pygame ×1
string ×1
urllib ×1
windows ×1