2017-07-16 15 views
1

辞書を作成した後にキーに値を追加しようとしています。異なる長さに基づいてキーに値を追加する

これは私がこれまで持っているものです。

movie_list = "movies.txt" # using a file that contains this order on first line: Title, year, genre, director, actor 
in_file = open(movie_list, 'r') 
in_file.readline() 

def list_maker(in_file): 
    movie1 = str(input("Enter in a movie: ")) 
    movie2 = str(input("Enter in another movie: ")) 

    d = {} 
    for line in in_file: 
     l = line.split(",") 
     title_year = (l[0], l[1]) # only then making the tuple ('Title', 'year') 
     for i in range(4, len(l)): 
      d = {title_year: l[i]} 

     if movie1 or movie2 == l[0]: 
      print(d.values()) 

私はそれを得る出力:私はこれら二つの映画で入力したい場合

Enter in a movie: 13 B 
Enter in another movie: 1920 
{('13 B', '(2009)'): 'R. Madhavan'} 
{('13 B', '(2009)'): 'Neetu Chandra'} 
{('13 B', '(2009)'): 'Poonam Dhillon\n'} 
{('1920', '(2008)'): 'Rajneesh Duggal'} 
{('1920', '(2008)'): 'Adah Sharma'} 
{('1920', '(2008)'): 'Anjori Alagh\n'} 
{('1942 A Love Story', '(1994)'): 'Anil Kapoor'} 
{('1942 A Love Story', '(1994)'): 'Manisha Koirala'} 
{('1942 A Love Story', '(1994)'): 'Jackie Shroff\n'} 
.... so on and so forth. I get the whole list of movies. 

にはどうすれば任意の(そうやって行くだろう2つのムービーをキーの値の和集合(movie1、movie2))として使用しますか?

例:

{('13 B', '(2009)'): 'R. Madhavan', 'Neetu Chandra', 'Poonam Dhillon'} 
{('1920', '(2008)'): 'Rajneesh Duggal', 'Adah Sharma', 'Anjori Alagh'} 

答えて

0

申し訳ありませんが、出力が何をしたい、完全ではありませんが、ここでは、あなたがそれを行う必要があります方法です場合:

d = {} 
for line in in_file: 
    l = line.split(",") 
    title_year = (l[0], l[1]) 
    people = [] 
    for i in range(4, len(l)): 
     people.append(l[i]) # we append items to the list... 
    d = {title_year: people} # ...and then make the dict so that the list is in it. 

    if movie1 or movie2 == l[0]: 
     print(d.values()) 

基本的には、私たちがここでやっていることは、我々でありますリストを作成していて、そのリストをdictの内部のキーに設定しています。

関連する問題