我想将元组的整数转换为元组的字符串.例如:
data = [(2,3,4,...),(23,42,54,...),......]
Run Code Online (Sandbox Code Playgroud)
会导致:
d = [('2','3','4',...),('23','42','54',....)......]
Run Code Online (Sandbox Code Playgroud)
请注意元组是如何大而且列表也很大.
我尝试了下面的代码,但它没有给我我想要的结果:
data = [(2,3,4,...),(23,42,54,...),......]
d=[]
for t in data:
d.append(str(t))
Run Code Online (Sandbox Code Playgroud) ugly = "[{'ride': 1, 'pickup_time': datetime(2016, 3, 17, 15, 36, 35, 976202)},
{'ride': 2, 'pickup_time': datetime.datetime(2016, 3, 17, 15, 41, 35, 976202)}]"
# The actual variable contains a lot more dictionnaries...`
Run Code Online (Sandbox Code Playgroud)
我想将丑陋的变量转换为真正的Python对象.我试过了json.loads(),ast.literal_eval()但它只适用于一个字典.
这就是为什么在此之前,我试图将这个字符串拆分成几个字典,但该split()方法只有一个分隔符,所以看起来我需要一个REGEX才能做到这一点.
最简单的方法是什么?谢谢.
我正在使用以下代码来使用 BeautifulSoup 检索一堆链接。它返回所有链接,但我想获取第三个链接,解析该链接,然后从该链接获取第三个链接,依此类推。我如何修改下面的代码来实现这一点?
import urllib
from BeautifulSoup import *
url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# Retrieve all of the anchor tags
tags = soup('a')
for tag in tags:
print tag.get('href', None)
print tag.contents[0]
Run Code Online (Sandbox Code Playgroud) 我想减去dict1 - dict2.如果结果是狗的负数并不重要,我只是有兴趣学习这个概念:)
owner = ['Bob', 'Sarah', 'Ann']
dict1 = {'Bob': {'shepherd': [4,6,3],'collie': [23, 3, 45], 'poodle':[2,0,6]}, {'Sarah': {'shepherd': [1,2,3],'collie': [3, 31, 4], 'poodle': [21,5,6]},{'Ann': {'shepherd': [4,6,3],'collie': [23, 3, 45], 'poodle': [2,10,8]}}
dict2 = {'Bob': {'shepherd': 10,'collie': 15,'poodle': 34},'Sarah': {'shepherd': 3,'collie': 25,'poodle': 4},'Ann': {'shepherd': 2,'collie': 1,'poodle': 0}}
Run Code Online (Sandbox Code Playgroud)
我想要:
wanted = dict1 = {'Bob': {'shepherd': [-6,-4,-7],'collie': [8, -12, 30], 'poodle':[-32,-34,-28]}, {'Sarah': {'shepherd': [-2,-1,0],'collie': [-22, 6, -21], 'poodle': [17,1,2]},{'Ann': {'shepherd': [2,4,1],'collie': [22, 2, 44], 'poodle': [2,10,8]}}
Run Code Online (Sandbox Code Playgroud)
我仍然是字典的新手,所以这里是我一直在尝试但得到NoneType错误.我假设它是因为dict1有比dict2更多的值,但我找不到执行上述计算的方法.
wanted = {}
for i …Run Code Online (Sandbox Code Playgroud) 我正在尝试打开终端并在其中运行命令.我在用
os.system("gnome-terminal -e 'bash -c \"exec bash; MY_COMMAND; exec bash\" '")
Run Code Online (Sandbox Code Playgroud)
这将打开一个新终端,但不执行该命令.
有没有方法通过单击标题图层来展开/折叠网格?我有一个非常大的表,显示在1680x1050的屏幕上,默认情况下已加载,折叠,每次用户想要展开/折叠表时,他必须单击标题图层的展开/折叠按钮,是"非常"定位.
我正在使用此功能:
def read_example(water_bound, filename, conditions):
f = open(filename,"r")
for i in range(0, conditions):
lines = f.readline()
test = map(int, lines.split(' '))
water_bound.append( test[0]-1 )
Run Code Online (Sandbox Code Playgroud)
我希望能够将它与numba. 据我了解,从这里,我必须将函数声明之前加入这一行:
@jit('void(int32[:], c_string_type, int32 )',nopython=True)
Run Code Online (Sandbox Code Playgroud)
但是,我收到此错误:
NameError: name 'c_string_type' 未定义
任务是找到整数中的奇数个数.
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开始.出了什么问题?
我是编程新手.我为此查看了教程,但我只是变得更加困惑.但我要做的是使用stdin和stdout接收数据,通过参数传递并打印出输出结果.
基本上,在命令行中,用户将输入和参数.
论点是:
i = sys.argv [1]
f = sys.argv [2]
w = sys.argv [3]
Run Code Online (Sandbox Code Playgroud)
然后使用if/else程序将根据上面选择的参数执行一些东西.
即:在命令行上,用户将输入脚本名称和f(for sys.argv[2:2])
$ test.py f
Run Code Online (Sandbox Code Playgroud)
.
if sys.argv == i:
#execute some stuff
elif sys.argv == f:
#execute some stuff
else:
sys.argv == w
#execute some stuff
Run Code Online (Sandbox Code Playgroud)
使用stdin/stdout如何创建此开关,程序根据argv所选代码执行一段代码?任何输入将不胜感激.
python ×9
python-2.7 ×3
python-3.x ×3
dictionary ×1
html ×1
ipython ×1
javascript ×1
jqgrid ×1
jquery ×1
numba ×1
opensuse ×1
php ×1
stdin ×1
stdout ×1