假设我有两个列表:
t1 = ["abc","def","ghi"]
t2 = [1,2,3]
Run Code Online (Sandbox Code Playgroud)
如何使用python合并它,以便输出列表将是:
t = [("abc",1),("def",2),("ghi",3)]
Run Code Online (Sandbox Code Playgroud)
我尝试的程序是:
t1 = ["abc","def"]
t2 = [1,2]
t = [ ]
for a in t1:
for b in t2:
t.append((a,b))
print t
Run Code Online (Sandbox Code Playgroud)
输出是:
[('abc', 1), ('abc', 2), ('def', 1), ('def', 2)]
Run Code Online (Sandbox Code Playgroud)
我不想重复输入.
我正在分析一个功能的控制流图,它基本上将输入数据映射到输出数据.大多数块都像这样:
if (input_variable == SPECIFIC_CONSTANT) {
output_variable = TRUE;
}
else {
output_variable = FALSE;
}
Run Code Online (Sandbox Code Playgroud)
此类代码的典型控制流程图如下图所示
digraph G {
2 -> 3 -> 5;
2 -> 4 -> 5;
}
Run Code Online (Sandbox Code Playgroud)
执行3和4由其价值决定input_variable但是5独立的.
给定有向图和起始节点,如何找到最近的节点,使得起始节点的任何路径都通过该节点?
示例:给定此图如何6从开始2或12从开始查找8?
我可以反转最低共同祖先算法并且它会有效吗?喜欢
for each node in graph:
ancestors = node.get_all_ancestors()
lca = find_lowest_common_ancestor(ancestors)
junction_node[lca] = node
get_junction_point(node):
return junction_node[node]
Run Code Online (Sandbox Code Playgroud)
我的编程语言是Python,我刚刚发现了NetworkX,但任何算法都会受到赞赏.我不习惯图论,我想我会错过基本词汇表来找到我正在寻找的东西.
谢谢你的帮助!
考虑以下代码:
class Foo:
def geta(self):
self.a = 'lie'
return 'this is {self.a}'.format(?)
Run Code Online (Sandbox Code Playgroud)
我应该写什么而不是问号,以便字符串格式正确?
我试图在Mac 10.9.1上安装PIL我收到一条错误消息,我无法在这里找到.我已经安装了Python27,pip,DJango,现在我尝试安装:
sudo pip install pil
Run Code Online (Sandbox Code Playgroud)
我收到的消息:
Downloading/unpacking PIL
Could not find any downloads that satisfy the requirement PIL
Some externally hosted files were ignored (use --allow-external PIL to allow).
Cleaning up...
No distributions at all found for PIL
Storing debug log for failure in /Users/xxx/Library/Logs/pip.log
Run Code Online (Sandbox Code Playgroud)
有人知道如何解决这个错误吗?
在Xcode上,我检查了是否安装了命令行工具,但它没有出现在Xcode-> preferences-> Download下.
非常感谢您的帮助
我正在处理的代码使用一些非常复杂的宏voodoo来生成代码,但最后有一个看起来像这样的构造
#define ARGS 1,2,3
#define MACROFUNC_OUTER(PARAMS) MACROFUNC_INNER(PARAMS)
#define MACROFUNC_INNER(A,B,C) A + B + C
int a = MACROFUNC_OUTER(ARGS);
Run Code Online (Sandbox Code Playgroud)
期待的是获得
int a = 1 + 2 + 3;
Run Code Online (Sandbox Code Playgroud)
这适用于最初为GHS编写的编译器以及GCC编写的编译器,但MSVC(2008)认为PARAMS它不会扩展为单个预处理令牌,然后A将其设置为整体PARAM,B而C不是任何内容.结果就是这样
int a = 1,2,3 + + ;
Run Code Online (Sandbox Code Playgroud)
虽然MSVC警告说not enough actual parameters for macro 'MACROFUNC_INNER'.
假设我有一个在不同键上发生的事件列表.
data = [
{"key": "A", "event": "created"},
{"key": "A", "event": "updated"},
{"key": "A", "event": "updated"},
{"key": "A", "event": "updated"},
{"key": "B", "event": "created"},
{"key": "B", "event": "updated"},
{"key": "B", "event": "updated"},
{"key": "C", "event": "created"},
{"key": "C", "event": "updated"},
{"key": "C", "event": "updated"},
{"key": "C", "event": "updated"},
{"key": "C", "event": "updated"},
{"key": "C", "event": "updated"},
]
df = pandas.DataFrame(data)
Run Code Online (Sandbox Code Playgroud)
我想首先在键上索引我的DataFrame,然后是枚举.它看起来像一个简单的unstack操作,但我无法找到如何正确地执行它.
我能做的最好的是
df.set_index("key", append=True).swaplevel(0, 1)
event
key
A 0 created
1 updated
2 updated
3 updated
B 4 created
5 …Run Code Online (Sandbox Code Playgroud) 我必须在 python 中创建一个具有 2 个以上参数的函数,最后我必须打印函数的第一个和最后一个参数(在列表中)。
我试过这样,但它不起作用。我究竟做错了什么?
import inspect
def func(a, b, c):
frame = inspect.currentframe()
args, _, _, values = inspect.getargvalues(frame)
for i in args:
return [(i, values[i]) for i=0 and i=n]
Run Code Online (Sandbox Code Playgroud)