Python 从列表列表创建置换数组

Python 从列表列表创建置换数组,python,permutation,itertools,pydot,Python,Permutation,Itertools,Pydot,我有变量'actorslist'和它的100行输出(每部电影一行): 那么我有: pairslist = list(itertools.permutations(actorslist, 2)) 这给了我一对演员,但只在一个特定的电影,然后在一个新的台词后,它进入下一部电影。我如何让它在一个大阵列中输出所有电影中的所有演员?他们的想法是,两个演员在一部电影中在一起应该得到一个pydot优势 我输入了这个,它成功地输出到一个点文件,但是没有输出正确的数据 graph = pydot.Dot(gra

我有变量'actorslist'和它的100行输出(每部电影一行):

那么我有:

pairslist = list(itertools.permutations(actorslist, 2))
这给了我一对演员,但只在一个特定的电影,然后在一个新的台词后,它进入下一部电影。我如何让它在一个大阵列中输出所有电影中的所有演员?他们的想法是,两个演员在一部电影中在一起应该得到一个pydot优势

我输入了这个,它成功地输出到一个点文件,但是没有输出正确的数据

graph = pydot.Dot(graph_type='graph', charset="utf8")
for i in pairslist:
  edge = pydot.Edge(i[0], i[1])
  graph.add_edge(edge)
  graph.write('dotfile.dot')
我的预期输出在点文件中应如下所示(A,B)与(B,A)相同,因此不存在于输出中:

"Tim Robbins" -- "Morgan Freeman";
"Tim Robbins" -- "Bob Gunton";
"Tim Robbins" -- "William Sadler";
"Morgan Freeman" -- "Bob Gunton";
"Morgan Freeman" -- "William Sadler";
"Bob Gunton" -- "William Sadler";
"Christian Bale" -- "Heath Ledger";
"Christian Bale" -- "Aaron Eckhart";
"Christian Bale" -- "Michael Caine";
"Heath Ledger" -- "Aaron Eckhart";
"Heath Ledger" -- "Michael Caine";
"Aaron Eckhart" -- "Michael Caine";
其他信息:

有些人对变量
actorslist
的创建方式感兴趣:

file = open('input.txt','rU') ###input is JSON data on each line{"Title":"Shawshank...
nfile = codecs.open('output.txt','w','utf-8')
movie_actors = []
for line in file:
  line = line.rstrip()
  movie = json.loads(line)
  l = []
  title = movie['Title']
  actors = movie['Actors']
  tempactorslist = actors.split(',')
  actorslist = []
  for actor in tempactorslist:
    actor = actor.strip()
    actorslist.append(actor)
  l.append(title)
  l.append(actorslist)
  row = l[0] + '\t' + json.dumps(l[1]) + '\n'
  nfile.writelines(row)

你会想要这样的东西:

import itertools

actorslist = [
    [u'Tim Robbins', u'Morgan Freeman', u'Bob Gunton', u'William Sadler'],
    [u'Christian Bale', u'Heath Ledger', u'Aaron Eckhart', u'Michael Caine']
    ]

for movie in actorslist:
    for actor1, actor2 in itertools.permutations(movie, 2):
        print(actor1, actor2)
        # make edge, etc.
输出:

Tim Robbins Morgan Freeman
Tim Robbins Bob Gunton
Tim Robbins William Sadler
Morgan Freeman Tim Robbins
Morgan Freeman Bob Gunton
Morgan Freeman William Sadler
Bob Gunton Tim Robbins
Bob Gunton Morgan Freeman
Bob Gunton William Sadler
William Sadler Tim Robbins
William Sadler Morgan Freeman
William Sadler Bob Gunton
Christian Bale Heath Ledger
Christian Bale Aaron Eckhart
Christian Bale Michael Caine
Heath Ledger Christian Bale
Heath Ledger Aaron Eckhart
Heath Ledger Michael Caine
Aaron Eckhart Christian Bale
Aaron Eckhart Heath Ledger
Aaron Eckhart Michael Caine
Michael Caine Christian Bale
Michael Caine Heath Ledger
Michael Caine Aaron Eckhart
你现在拥有的是排列电影列表,而不是每部电影中的演员列表

from collections import Counter
from itertools import combinations
import pydot

actorslists = [
    [u'Tim Robbins', u'Morgan Freeman', u'Bob Gunton', u'William Sadler'],
    [u'Christian Bale', u'Heath Ledger', u'Aaron Eckhart', u'Michael Caine'],
    [u'Tim Robbins', u'Heath Ledger', u'Michael Caine']
]

# Counter tracks how often each pair of actors has occurred (-> link weight)
actorpairs = Counter(pair for actorslist in actorslists for pair in combinations(sorted(actorslist), 2))

graph = pydot.Dot(graph_type='graph', charset="utf8")
for actors,weight in actorpairs.iteritems():   # or .items() for Python 3.x
    a,b = list(actors)
    edge = pydot.Edge(a, b, weight=str(weight))
    graph.add_edge(edge)
graph.write('dotfile.dot')
导致


我不确定它需要有多复杂,但这似乎可以生成您的输出。我只是换了你的线。。。(我冒昧地把蒂姆·罗宾斯(Tim Robbins)放进了《蝙蝠侠》(Batman),只是为了让它有更真实的重叠)

输出文件(记得我更改了Tim Robbins的输入)


你能不能也显示一下预期的产量?现在还不太清楚您想要实现什么。谢谢,似乎我缺少组合模块,可能是因为我正在运行python 2.7。@kegewe:
itertools。组合是标准python 2.7库的一部分。嗯,我似乎收到了以下错误消息:
a,b=list(actor)
ValueError:太多的值无法解包
@kegewe:我的输入错误(应该是actors,复数);固定的。我目前正在安装pydot进行测试(知道dot_解析器应该来自哪里吗?)是的,dot_解析器有点棘手,但这似乎对我有所帮助:我想我的问题是我的
actorslist
变量没有逗号分隔的actor集,而是在每一行上都有一组新的参与者…您知道我如何转换该变量,使其看起来像您在topSame注释中的一样,如上所述:我的问题是我的
actorslist
变量没有用逗号分隔的参与者集,而是在每一行上都有一组新的演员…你知道我如何转换这个变量,使它看起来像你的吗top@kegewe什么?
actorslist
的类型是什么?当我说
print type(actorslist)
时,我得到了很多行
@kegewe您是如何创建
actorslist
的?我不明白
print type(actorslist)
怎么能给你“很多行”。我编辑了原始问题来展示我是如何创建
actorslist的
from collections import Counter
from itertools import combinations
import pydot

actorslists = [
    [u'Tim Robbins', u'Morgan Freeman', u'Bob Gunton', u'William Sadler'],
    [u'Christian Bale', u'Heath Ledger', u'Aaron Eckhart', u'Michael Caine'],
    [u'Tim Robbins', u'Heath Ledger', u'Michael Caine']
]

# Counter tracks how often each pair of actors has occurred (-> link weight)
actorpairs = Counter(pair for actorslist in actorslists for pair in combinations(sorted(actorslist), 2))

graph = pydot.Dot(graph_type='graph', charset="utf8")
for actors,weight in actorpairs.iteritems():   # or .items() for Python 3.x
    a,b = list(actors)
    edge = pydot.Edge(a, b, weight=str(weight))
    graph.add_edge(edge)
graph.write('dotfile.dot')
actorslist = [[u'Tim Robbins', u'Morgan Freeman', u'Bob Gunton', u'William Sadler'],
  [u'Christian Bale', u'Heath Ledger', u'Tim Robbins', u'Michael Caine']]

import itertools
import pydot
graph = pydot.Dot(graph_type='graph', charset="utf8")

# generate a list of all unique actors, if you want that
# allactors = list(set([j for j in [i for i in actorslist]]))

# this is the key line -- you have to iterate through the list 
# and not try to permute the whole thing
pairs = [list(itertools.permutations(k, 2)) for k in actorslist]


for pair in pairs:
    for a,b in pair:
        edge = pydot.Edge(a,b)
        graph.add_edge(edge)
        graph.write('dotfile.dot')
graph G {
charset=utf8;
"Tim Robbins" -- "Morgan Freeman";
"Tim Robbins" -- "Bob Gunton";
"Tim Robbins" -- "William Sadler";
"Morgan Freeman" -- "Tim Robbins";
"Morgan Freeman" -- "Bob Gunton";
"Morgan Freeman" -- "William Sadler";
"Bob Gunton" -- "Tim Robbins";
"Bob Gunton" -- "Morgan Freeman";
"Bob Gunton" -- "William Sadler";
"William Sadler" -- "Tim Robbins";
"William Sadler" -- "Morgan Freeman";
"William Sadler" -- "Bob Gunton";
"Christian Bale" -- "Heath Ledger";
"Christian Bale" -- "Tim Robbins";
"Christian Bale" -- "Michael Caine";
"Heath Ledger" -- "Christian Bale";
"Heath Ledger" -- "Tim Robbins";
"Heath Ledger" -- "Michael Caine";
"Tim Robbins" -- "Christian Bale";
"Tim Robbins" -- "Heath Ledger";
"Tim Robbins" -- "Michael Caine";
"Michael Caine" -- "Christian Bale";
"Michael Caine" -- "Heath Ledger";
"Michael Caine" -- "Tim Robbins";
}