create an edge list that groups films by genre, i.e. join two films of the same genre

huangapple go评论70阅读模式
英文:

create an edge list that groups films by genre, i.e. join two films of the same genre

问题

这是我的建议:

edges = []
genres = [28, 12, 16]

for genre in genres:
    movies = dictionary_title_withonegenere[genre]
    for i in range(len(movies)):
        for j in range(i + 1, len(movies)):
            edges.append({"source": movies[i], "target": movies[j]})

print(edges)
英文:

I've just been using python and I want to build an edge list that groups together the titles of movies that have a genre in common.
I have this dictionary:

dictionary_title_withonegenere=
{28: ['Avatar: The Way of Water', 'Violent Night', 'Puss in Boots: The Last Wish'],
12: ['Avatar: The Way of Water', 'The Chronicles of Narnia: The Lion, the Witch and the Wardrobe'],
16: ['Puss in Boots: The Last Wish', 'Strange World']}

now 28,12,16 are the genres of movies.I want to create an edge list that groups movies by genre, i.e. I join two movies of the same genre:

source                         target 
Avatar: The Way of Water       Violent Nigh
Avatar: The Way of Water       Puss in Boots: The Last Wish
Violent Nigh                   Puss in Boots: The Last Wish
Avatar: The Way of Water       The Chronicles of Narnia: The Lion, the Witch 
                               and the Wardrobe
Puss in Boots: The Last Wish   Strange World

This is my idea:

edges=[]
genres=[28,12,16]

    for i in range(0,len(genres)):
            for genres[i] in dictionary_title_withonegenere[genres[i]]:
                for genres[i] in dictionary_title_withonegenere[genres[i]][1:]:
                    edges.append({"sorce":dictionary_title_withonegenere[genres[i]][0],"target":dictionary_title_withonegenere[genres[i]][y]})

    print((edges))

My code don't work. How can i do?

答案1

得分: 1

请尝试这个代码:

edges=[]
genres=[28,12,16,35,80,99,18,10751,14,36,27,10402,9648,10749,878,10770,53,10752,37]
dictionary_title_withonegenere = {28: ['Avatar: The Way of Water', 'Violent Night', 'Puss in Boots: The Last Wish'],
12: ['Avatar: The Way of Water', 'The Chronicles of Narnia: The Lion, the Witch and the Wardrobe'],
16: ['Puss in Boots: The Last Wish', 'Strange World']}

for i in range(0,len(genres)):
    if genres[i] in dictionary_title_withonegenere:
        genres_list = dictionary_title_withonegenere[genres[i]]
        genres_list_len = len(genres_list)
        if genres_list_len <= 1:
            continue
        for j in range(genres_list_len):
            for k in range(j+1,genres_list_len):
                edges.append({"name_movies":genres_list[j],"onegres_movies":genres_list[k]})

for edge in edges:
    print(f'{edge["name_movies"]: <40}{edge["onegres_movies"]}')

输出结果:

Avatar: The Way of Water                Violent Night
Avatar: The Way of Water                Puss in Boots: The Last Wish
Violent Night                           Puss in Boots: The Last Wish
Avatar: The Way of Water                The Chronicles of Narnia: The Lion, the Witch and the Wardrobe
Puss in Boots: The Last Wish            Strange World
英文:

try this

edges=[]
genres=[28,12,16,35,80,99,18,10751,14,36,27,10402,9648,10749,878,10770,53,10752,37]
dictionary_title_withonegenere = {28: [&#39;Avatar: The Way of Water&#39;, &#39;Violent Night&#39;, &#39;Puss in Boots: The Last Wish&#39;],
12: [&#39;Avatar: The Way of Water&#39;, &#39;The Chronicles of Narnia: The Lion, the Witch and the Wardrobe&#39;],
16: [&#39;Puss in Boots: The Last Wish&#39;, &#39;Strange World&#39;]}

for i in range(0,len(genres)):
    if genres[i] in dictionary_title_withonegenere:
        genres_list = dictionary_title_withonegenere[genres[i]]
        genres_list_len = len(genres_list)
        if genres_list_len &lt;= 1:
            continue
        for j in range(genres_list_len):
            for k in range(j+1,genres_list_len):
                edges.append({&quot;name_movies&quot;:genres_list[j],&quot;onegres_movies&quot;:genres_list[k]})

for edge in edges:
    print(f&#39;{edge[&quot;name_movies&quot;]: &lt;40}{edge[&quot;onegres_movies&quot;]}&#39;)

output

Avatar: The Way of Water                Violent Night
Avatar: The Way of Water                Puss in Boots: The Last Wish
Violent Night                           Puss in Boots: The Last Wish
Avatar: The Way of Water                The Chronicles of Narnia: The Lion, the Witch and the Wardrobe
Puss in Boots: The Last Wish            Strange World

答案2

得分: 1

你可以通过创建一个中间数据结构来检查两部电影是否有共同的流派,即创建一个包含电影->流派映射的数据结构,然后你可以遍历所有电影,查看是否有共同的流派,并在它们之间创建一条边。

from pprint import pprint
dictionary_title_withonegenere= {28: ['Avatar: The Way of Water', 'Violent Night', 'Puss in Boots: The Last Wish'],
12: ['Avatar: The Way of Water', 'The Chronicles of Narnia: The Lion, the Witch and the Wardrobe'],
16: ['Puss in Boots: The Last Wish', 'Strange World']}

movies_with_genre = {}
movies_set = set()
for genre, movies in dictionary_title_withonegenere.items():
    for movie in movies:
        movies_with_genre.setdefault(movie, set()).add(genre)
        movies_set.add(movie)

pprint(movies_with_genre)
movie_list = list(movies_set)
edges = []
for i in range(len(movie_list)):
    source_movie= movie_list[i]
    for j in range(i + 1, len(movie_list)):
        target_movie = movie_list[j]
        common_genre = False
        for source_genre in movies_with_genre[source_movie]:
            if source_genre in movies_with_genre[target_movie]:
                common_genre = True
                break
        if common_genre:
            edges.append({"source":source_movie, "target":target_movie})
pprint(edges)

输出:

{'Avatar: The Way of Water': {28, 12},
 'Puss in Boots: The Last Wish': {16, 28},
 'Strange World': {16},
 'The Chronicles of Narnia: The Lion, the Witch and the Wardrobe': {12},
 'Violent Night': {28}}
[{'source': 'Strange World', 'target': 'Puss in Boots: The Last Wish'},
 {'source': 'Avatar: The Way of Water', 'target': 'Puss in Boots: The Last Wish'},
 {'source': 'Avatar: The Way of Water', 'target': 'Violent Night'},
 {'source': 'Avatar: The Way of Water',
  'target': 'The Chronicles of Narnia: The Lion, the Witch and the Wardrobe'},
 {'source': 'Puss in Boots: The Last Wish', 'target': 'Violent Night'}]
英文:

You can check if 2 movies have common genre by creating an intermediate datastructure, that is to have a mapping with movie->genres and with that datastructure, you can iterate over all movies and see if there is any common genre and create an edge between them.

from pprint import pprint
dictionary_title_withonegenere= {28: [&#39;Avatar: The Way of Water&#39;, &#39;Violent Night&#39;, &#39;Puss in Boots: The Last Wish&#39;],
12: [&#39;Avatar: The Way of Water&#39;, &#39;The Chronicles of Narnia: The Lion, the Witch and the Wardrobe&#39;],
16: [&#39;Puss in Boots: The Last Wish&#39;, &#39;Strange World&#39;]}

movies_with_genre = {}
movies_set = set()
for genre, movies in dictionary_title_withonegenere.items():
    for movie in movies:
        movies_with_genre.setdefault(movie, set()).add(genre)
        movies_set.add(movie)
    
pprint(movies_with_genre)
movie_list = list(movies_set)
edges = []
for i in range(len(movie_list)):
    source_movie= movie_list[i]
    for j in range(i + 1, len(movie_list)):
        target_movie = movie_list[j]
        common_genre = False
        for source_genre in movies_with_genre[source_movie]:
            if source_genre in movies_with_genre[target_movie]:
                common_genre = True
                break
        if common_genre:
            edges.append({&quot;sorce&quot;:source_movie, &quot;target&quot;:target_movie})
pprint(edges)

OUTPUT

{&#39;Avatar: The Way of Water&#39;: {28, 12},
 &#39;Puss in Boots: The Last Wish&#39;: {16, 28},
 &#39;Strange World&#39;: {16},
 &#39;The Chronicles of Narnia: The Lion, the Witch and the Wardrobe&#39;: {12},
 &#39;Violent Night&#39;: {28}}
[{&#39;sorce&#39;: &#39;Strange World&#39;, &#39;target&#39;: &#39;Puss in Boots: The Last Wish&#39;},
 {&#39;sorce&#39;: &#39;Avatar: The Way of Water&#39;,
  &#39;target&#39;: &#39;Puss in Boots: The Last Wish&#39;},
 {&#39;sorce&#39;: &#39;Avatar: The Way of Water&#39;, &#39;target&#39;: &#39;Violent Night&#39;},
 {&#39;sorce&#39;: &#39;Avatar: The Way of Water&#39;,
  &#39;target&#39;: &#39;The Chronicles of Narnia: The Lion, the Witch and the Wardrobe&#39;},
 {&#39;sorce&#39;: &#39;Puss in Boots: The Last Wish&#39;, &#39;target&#39;: &#39;Violent Night&#39;}]

huangapple
  • 本文由 发表于 2023年1月9日 17:13:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定