简体   繁体   English

打印出通过元组列表切换的字符串...帮助?

[英]Printing out strings toggling through list of tuples…help?

I have a list of tuples which relate a given actor to other actors through the movies and co-stars: 我有一个元组列表,通过电影和联合演员将一个演员与其他演员联系起来:

For a given actor E ActE: 对于一个给定的演员E ActE:

conn_list = [('m6', 'D ActD'), ('m3', 'B ActB'), ('m2', 'Kevin Bacon')]

So this says that: 所以这说:

E ActE was in a movie m6 with D ActD
D ActE was in a movie m3 with B ActB 
B ActB was in a movie m3 with Kevin Bacon

I'm just wondering how to print that out. 我只是想知道如何打印出来。 I know how to toggle through a list and get the elements from the tuples. 我知道如何切换列表并从元组中获取元素。 I am using a for loop to iterate through, but I dont know how to deal with the changing actors when print out the strings. 我正在使用for循环来迭代,但我不知道如何在打印字符串时处理变化的actor。

for connection in conn_list:
    print '%s was in %s with %s'(            , connection[0],            )

That is pretty much where I'm stuck at. 这几乎是我坚持的地方。 I don't want to make multiple print statements since there might be too many movies and actors. 我不想制作多个印刷语句,因为电影和演员可能太多了。 Any ideas? 有任何想法吗?

your input does not match your format string, but this is what you want to do: 您的输入与您的格式字符串不匹配,但这是您想要做的:

actor_name = "ActE"
conn_list = [('m6', 'D ActD'), 
             ('m3', 'B ActB'), 
             ('m2', 'Kevin Bacon')]

for con in conn_list:
     print "%s was in movie %s with %s" % (actor_name, con[0], con[1])

the format string will take the tuple and replace the %s with the element at that location, for example: 格式字符串将使用元组并将%s替换为该位置的元素,例如:

"%s likes %s" % ("bob", "apples")

will replace the first %s with tuple[0] and the second %s with tuple[1] 将用tuple[0]替换第一个%stuple[0] tuple[1]替换第二个%s


EDIT: While that will fix the problem in your question... Reading it again a few times, I think what you may want is something that groups actors in the same movie? 编辑:虽然这将解决你的问题...再次阅读它几次,我认为你可能想要的是在同一部电影中组合演员的东西? but i am unsure. 但我不确定。

I think we're missing some contextual information? 我想我们错过了一些背景信息? If conn_list is the connection list for the actor E ActE then presumably there's a variable which contains the string E ActE . 如果conn_list是actor E ActE的连接列表,那么可能有一个包含字符串E ActE的变量。 Is that correct? 那是对的吗?

Serdalis has pointed out how you can use the % operator to print the message you want, but if you don't want to modify the structure of conn_list then you can use something like this: Serdalis指出了如何使用%运算符来打印所需的消息,但是如果你不想修改conn_list的结构,那么你可以使用这样的东西:

current_actor = 'E ActE'
for connection in conn_list:
    print '%s was in %s with %s' % (current_actor, connection[0], connection[1])
    current_actor = connection[1]

When I run this with your conn_list I get: 当我用你的conn_list运行时,我得到:

E ActE was in m6 with D ActD
D ActD was in m3 with B ActB
B ActB was in m2 with Kevin Bacon

Even better would be to use the format(...) method of strings since the % operator is being phased out: 更好的是使用字符串的format(...)方法,因为%运算符正被逐步淘汰:

current_actor = 'E ActE'
print connection in conn_list:
    print '{0} was in {1} with {2}'.format(current_actor, connection[0], connection[1])
    current_actor = connection[1]

which produces the same output. 它产生相同的输出。

Edit: Serdalis edited their solution while I was writing this. 编辑: Serdalis在我写这篇文章时编辑了他们的解决方案。 The solution now uses the original form of conn_list . 该解决方案现在使用conn_list的原始形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM