C# 将Python代码翻译为C等效代码

C# 将Python代码翻译为C等效代码,c#,python,facebook-graph-api,facebook-fql,C#,Python,Facebook Graph Api,Facebook Fql,我想将书中的python代码示例重写为C等效代码,以测试其功能 代码如下: q = "select target_id from connection where source_id=me() and target_type='user'" my_friends = [str(t['target_id']) for t in fql.query(q)] q = "select uid1, uid2 from friends where uid1 in (%s) and iud2 in (%s)

我想将书中的python代码示例重写为C等效代码,以测试其功能

代码如下:

q = "select target_id from connection where source_id=me() and target_type='user'"
my_friends = [str(t['target_id']) for t in fql.query(q)]

q = "select uid1, uid2 from friends where uid1 in (%s) and iud2 in (%s)" %
    (",".join(my_friends), ",".join(my_friends),)
mutual_friendships = fql(q)
我不知道符号%s和%s在代码中的含义。如果有人能用C编写等效代码,我将不胜感激。

在.NET中调用string.Format或类似函数时,%s是字符串格式的替换占位符,这将等效于{0}…{1}等等。

签出:

对于任何字符串值,%s将转换为{0}…{N}d、 等等,都将用{0}…{N}语法表示,但是在MSDN上定义了几个不同格式的字符串。例如,Python中有and.

字符串格式化操作 %s被替换为在%运算符之后传递的元组中的相应值

它在Python中的工作原理 例如:

my_friends = [0, 2, 666, 123132]
print "select uid1 from friends where uid1 in (%s)" % (",".join(my_friends))
将打印以下内容:

从uid1位于0,2666123132中的朋友中选择uid1

如何用C替换它 您需要使用String.Format,如前所述,例如:


它的工作方式与Python 2.6之后提供的字符串格式方法非常相似。

什么是fql?%s被替换为适当的字符串,该字符串由%operator之后传递到相应位置的值生成,该运算符将字符串从元组中分离出来。在python代码中,我建议您使用.format而不是%,因为您创建了两次联接方法。”{0} ... {0}.format、.joinmy_friends是waaay bettersorry@jamesTheProgrammer,但你完全错了。检查答案以查看%在python中的作用。
my_friends = [0, 2, 666, 123132]
print "select uid1 from friends where uid1 in (%s)" % (",".join(my_friends))
string formatString = "select uid1, uid2 from friends where uid1 in ({0}) and iud2 in ({1})"
string q = String.Format(formatString, yourReplacement1, yourReplacement2)