我有一张表是这样的:
IDs Timestamp Values
124 300.6 1.23
124 350.1 -2.4
309 300.6 10.3
12 123.4 9.00
18 350.1 2.11
309 350.1 8.3
...
Run Code Online (Sandbox Code Playgroud)
我想选择属于一组ID的所有行.我知道我可以做类似的事情
table[table.IDs == 124]
Run Code Online (Sandbox Code Playgroud)
选择一个ID的所有行,我就能做到
table[(table.IDs == 124) | (table.IDs == 309)]
Run Code Online (Sandbox Code Playgroud)
获得两个ID的行.但是想象一下,我有~100,000行,超过1,000个唯一ID(与行索引不同),我想选择匹配一组10个ID的所有行.直觉我想这样做:
# id_list: a list of 10 IDs
table[ table.IDs in id_list ]
Run Code Online (Sandbox Code Playgroud)
但是Python拒绝这种语法.我能想到的唯一方法是做以下事情:
table[ (table.IDs == id_list[0]) |
(table.IDs == id_list[1]) |
(table.IDs == id_list[2]) |
(table.IDs == id_list[3]) |
(table.IDs == id_list[4]) |
(table.IDs == id_list[5]) |
(table.IDs == id_list[6]) |
(table.IDs == …Run Code Online (Sandbox Code Playgroud) 我在"virtualenv"中遇到了PyGTK的一些奇怪问题.gtk不会导入我的virtualenv,而它在我的全局python安装中导入.(上周我没有遇到这个问题,猜测某些软件更新会让人感到不安.)
有没有一种解决此问题的好方法?
如下所示:全局导入gtk,
tom@zeppelin:~$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> gtk
<module 'gtk' from '/usr/lib/pymodules/python2.7/gtk-2.0/gtk/__init__.pyc'>
Run Code Online (Sandbox Code Playgroud)
然后无法导入gtk,
tom@zeppelin:~$ workon py27
(py27)tom@zeppelin:~$ python
Python 2.7.1+ (r271:86832, Sep 27 2012, 21:12:17)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named gtk
Run Code Online (Sandbox Code Playgroud)
不幸的是,这破坏了我的ipython …
我有一个3元素的python元组,我正在尝试使用3元素列表的索引进行排序或重新排列,我想知道最简洁的方法是什么.
到目前为止我有:
my_tuple = (10, 20, 30)
new_positions = [2, 0, 1]
my_shuffled_tuple = my_tuple[new_positions[0]], my_tuple[new_positions[1]], my_tuple[new_positions[2]]
# outputs: (30, 10, 20)
Run Code Online (Sandbox Code Playgroud)
如果我这样做,我也得到相同的结果:
my_shuffled_tuple = tuple([my_tuple[i] for i in new_positions])
Run Code Online (Sandbox Code Playgroud)
是否有更简洁的创作方式my_shuffled_tuple?