我怎样才能把字典分成两个列表,一个是键,一个是值.例如:
{'name': 'Han Solo', 'firstname': 'Han', 'lastname': 'Solo', 'age': 37, 'score': 100, 'yrclass': 10}
Run Code Online (Sandbox Code Playgroud)
并将其拆分为:
['name', 'firstname', 'lastname', 'age', 'score', 'yrclass']
# and
['Han Solo', 'Han', 'Solo', 36, 100, 10]
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?
我正在创建一个程序,可以访问存储在类中的数据.所以例如我有这个类:
#!/usr/bin/env python
import shelve
cur_dir = '.'
class Person:
def __init__(self, name, score, age=None, yrclass=10):
self.name = name
self.firstname = name.split()[0]
try:
self.lastname = name.split()[1]
except:
self.lastname = None
self.score = score
self.age = age
self.yrclass = yrclass
def yrup(self):
self.age += 1
self.yrclass += 1
if __name__ == "__main__":
db = shelve.open('people.dat')
db['han'] = Person('Han Solo', 100, 37)
db['luke'] = Person('Luke Skywalker', 83, 26)
db['chewbacca'] = Person('Chewbacca', 100, 90901)
Run Code Online (Sandbox Code Playgroud)
所以使用这个我可以调用一个单独的变量,如:
print db['luke'].name
Run Code Online (Sandbox Code Playgroud)
但如果我想打印所有变量,我有点迷失.
如果我跑:
f = db['han']
dir(f) …Run Code Online (Sandbox Code Playgroud) 我正在设置一个程序将我的计算机连接到我们的学校代理,目前有这样的东西:
import subprocess
import sys
username = 'fergus.barker'
password = '*************'
proxy = 'proxy.det.nsw.edu.au:8080'
options = '%s:%s@%s' % (username, password, proxy)
subprocess.Popen('export http_proxy=' + options)
Run Code Online (Sandbox Code Playgroud)
但在跑步时,我得到:
Traceback (most recent call last):
File "school_proxy_settings.py", line 19, in <module>
subprocess.Popen('export http_proxy=' + options)
File "/usr/lib/python2.6/subprocess.py", line 621, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1126, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况请大家好吗?
一个非常简单的问题,我确信我知道但是一定忘记了
运行此代码时:
x = 0
def run_5():
print "5 minutes later"
x += 5
print x, "minutes since start"
run_5()
print x
Run Code Online (Sandbox Code Playgroud)
我得到x没有定义.如何在函数中使用x并在其外部实现?
所以我想知道将列表变成字符串的最"pythonic"方式是什么.
例如:
string_list = ['h', 'e', 'l', 'l', 'o']
# to the string
string = 'hello'
Run Code Online (Sandbox Code Playgroud)
我一直在使用这种''.join(string_list)方法,但它只是觉得接近不可读和迂回的方法来做一些如此简单的事情.
有更好的方法还是我过于复杂?
我正在运行SQL服务器,需要使用SELECT ... INTO命令运行一些命令.目前(作为测试)我正在运行此命令:
SELECT *
INTO `assets_copy`
FROM `assets`
Run Code Online (Sandbox Code Playgroud)
最简单的例子可能,但它仍然不会运行.我收到错误:
MySQL said:
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'assets_copy` FROM `assets` LIMIT 0, 30' at line 1
Run Code Online (Sandbox Code Playgroud)
所以我想知道我使用的SQL版本是否不支持这个?我正在使用3.23.49的MySQL.
关于GitHubs函数的一个非常快速的问题是获取README文件并将其显示在任何存储库中.我知道它使用此存储库来执行此操作,但是想知道是否提供了没有任何扩展(即README不是README.markdown)它是否进行任何形式的格式化.
谢谢
如果我在字典中有一系列列表(例如):
{'Name': ['Thomas', 'Steven', 'Pauly D'], 'Age': [30, 50, 29]}
Run Code Online (Sandbox Code Playgroud)
我想找到字符串的位置,这样我就可以从另一个列表中获得相同的位置.
所以,例如
if x = 'Thomas' #is in position 2:
y = dictionary['Age'][2]
Run Code Online (Sandbox Code Playgroud) python ×6
dictionary ×2
list ×2
class ×1
git ×1
github ×1
mysql ×1
shelve ×1
sql ×1
subprocess ×1