Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何交叉字典键(即文本文件)并打印生成更多交叉点的键的值_Python_Dictionary - Fatal编程技术网

Python 如何交叉字典键(即文本文件)并打印生成更多交叉点的键的值

Python 如何交叉字典键(即文本文件)并打印生成更多交叉点的键的值,python,dictionary,Python,Dictionary,如何与字典键(即文本文件)相交并打印生成最长列表的键的值?这就是我目前得到的。我的问题在代码的末尾 #define intersection between user text input and my file inputs def me_and_the_plant(m, p): return list(set(m) & set(p)) #get user text input words = raw_input("Say anything that comes to you

如何与字典键(即文本文件)相交并打印生成最长列表的键的值?这就是我目前得到的。我的问题在代码的末尾

#define intersection between user text input and my file inputs
def me_and_the_plant(m, p):
    return list(set(m) & set(p))

#get user text input
words = raw_input("Say anything that comes to your mind: ")
print
input_words = words.split()

#define valid user input
if len(input_words) < 3:
    print "I need more than that."
    Mithras()
else:
    me = input_words

#make dictionary with my input files
songs = {"Wicked.txt" : "Wicked.wav",
         "Requiem.txt" : "Requiem.wav"}

#use text files as keys
for lyrics in songs.keys():
    f = open(lyrics)
    r = f.read()
    the_plant = r.split()
    #for the key that gets the most intersections, print its value
    print me_and_the_plant(me, the_plant)
#定义用户文本输入和我的文件输入之间的交叉点
定义me_和__工厂(m,p):
返回列表(集合(m)和集合(p))
#获取用户文本输入
words=原始输入(“说出任何你想到的话:”)
打印
input_words=words.split()
#定义有效的用户输入
如果len(输入_字)<3:
打印“我需要更多。”
密特拉
其他:
me=输入单词
#用我的输入文件制作字典
歌曲={“Wicked.txt”:“Wicked.wav”,
“安魂曲.txt”:“安魂曲.wav”}
#使用文本文件作为键
用于歌曲中的歌词。键():
f=开放(歌词)
r=f.read()
_工厂=r.split()
#对于获得最多交点的关键点,打印其值
打印我和我的工厂(我,我的工厂)

更改末尾的循环,以确定哪一个循环最多:

    ...
#use text files as keys
most_intersection_key = None
most_intersection = None
most_intersection_len = 0
me = me.split()
for lyrics in songs:
    with open(lyrics) as f:
        the_plant = f.read().split()
    intersection = me_and_the_plant(me, the_plant)
    intersection_len = len(intersection)
    if intersection_len > most_intersection_len:  # most seen?
        most_intersection_key = lyrics
        most_intersection_len = intersection_len
        most_intersection = intersection

if most_intersection_key:
    print most_intersection_key, most_intersection_len, most_intersection
else:
    print 'there were no intersections'
您可以使用
collections.Counter
类稍微简化它,并去掉一行
me\u和\u-plant()
函数:

    ...
from collections import Counter

intersection_lengths = Counter()
intersections = {}
me = me.split()
for song_filename in songs:
    with open(song_filename) as f:
        lyrics = f.read().split()
    intersections[song_filename] = set(me) & set(lyrics)
    intersection_lengths[song_filename] = len(intersections[song_filename])

most_intersections = intersection_lengths.most_common(1)
if most_intersections:
    print most_intersections[0], most_intersections[1], \
          list(intersections[most_intersections[0]])
else:
    print 'there were no intersections'
另见。