m0_62607163 2021-11-08 11:59 采纳率: 77.8%
浏览 27
已结题

创建一个function,return每个字后面一个字

创建一个function叫follows(words_list),return 一个dictionary每个key的value是一个list包含key后面一个字

例子:

follows(['what', 'is', 'that', 'there'])
{'what': ['is'], 'is': ['that'], 'that': ['there']}

follows(['one', 'fish', 'two', 'fish', 'red', 'fish', 'blue', 'fish', 'blue'])
{'one': ['fish'], 'fish': ['two', 'red', 'blue', 'blue'], 'two': ['fish'], 'red': ['fish'], 'blue': ['fish']}

follows(['a', 'b', 'c', 'b', 'a', 'c', 'a', 'd', 'e', 'b', 'a'])
{'a': ['b', 'c', 'd'], 'b': ['c', 'a', 'a'], 'c': ['b', 'a'], 'd': ['e'], 'e': ['b']}

follows(['That', 'sam-i-am,', 'that', 'sam-i-am,', 'I', 'do', 'not', 'like', 'that', 'sam-i-am.', 'Do', 'you', 'like', 'green', 'eggs', 'and', 'ham?', 'I', 'do', 'not', 'like', 'them', 'sam-i-am.'])
{'That': ['sam-i-am,'], 'sam-i-am,': ['that', 'I'], 'that': ['sam-i-am,', 'sam-i-am.'], 'I': ['do', 'do'], 'do': ['not', 'not'], 'not': ['like', 'like'], 'like': ['that', 'green', 'them'], 'sam-i-am.': ['Do'], 'Do': ['you'], 'you': ['like'], 'green': ['eggs'], 'eggs': ['and'], 'and': ['ham?'], 'ham?': ['I'], 'them': ['sam-i-am.']}

def follows(words_list):
output={}
lis=[]
for i in range(len(words_list)):
lis.append(words_list.index(words_list[i]))
for g in range(len(lis)):
if g!=len(lis)-1:
然后怎么写

  • 写回答

2条回答 默认 最新

  • -Undefined_ 2021-11-08 12:15
    关注
    def follows(words_list):
        output={}
        lis=[]
        index=[]
        for i in range(len(words_list)-1):
            index.append(i)
            lis.append(words_list.index(words_list[i]))
            if words_list[i] != words_list[i+1]:
                if words_list[i] in output:
                    output[words_list[i]].append(words_list[i+1])
                else:
                    output[words_list[i]] = [words_list[i+1]]
        return output
    


     觉得有用的话采纳一下哈

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月16日
  • 已采纳回答 11月8日
  • 创建了问题 11月8日

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写