微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我还需要使用有序字典吗?

如何解决我还需要使用有序字典吗?

所以,我想知道我是否还需要为此使用有序字典。

import requests
import time
import collections
import threading
 
 
class LiveChecker:
    """Editable YT streamer checker. Meant to be used with a discord bot. Edit the module directly to change which streamers to track."""
    def __init__(self):
 
        self.headers = {'Accept-Encoding' : 'identity'}
 
        self.initial = {  'Gura':'https://www.youtube.com/channel/UCoSrY_IQQVpmIRZ9Xf-y93g',#name as key,link as value
                          'Calli':'https://www.youtube.com/channel/UCL_qhgtOy0dy1Agp8vkySQg','Kiara':'https://www.youtube.com/channel/UCHsx4Hqa-1ORjQTh9TYDhww','Ina':'https://www.youtube.com/channel/UCMwGHR0BTZuLsmjY_NT5Pwg','Ame':'https://www.youtube.com/channel/UCyl1z3jo3XHR1riLFKG5UAg','Noel':'https://www.youtube.com/channel/UCdyqAaZDKHXg4Ahi7VENThQ','Pekora':'https://www.youtube.com/channel/UC1DCedRgGHBdm81E1llLhOQ','Suisei':'https://www.youtube.com/channel/UC5CwaMl1eIgY8h02uZw7u8A','Okayu':'https://www.youtube.com/channel/UCvaTdHTWBGv3MKj3KVqJVCw','Korone':'https://www.youtube.com/channel/UChAnqc_AY5_I3Px5dig3X1Q','Flare':'https://www.youtube.com/channel/UCvInZx9h3jC2JzsIzoOebWg','Ollie':'https://www.youtube.com/channel/UCYz_5n-uDuChHtLo7My1HnQ','Moona':'https://www.youtube.com/channel/UCP0BspO_AMEe3aQqqpo89Dg','rushia':'https://www.youtube.com/channel/UCl_gCybOjrigOXw6Qb4qJzQ','Coco':'https://www.youtube.com/channel/UCS9uQI-jC3DE0L4IpXyvr6w','Aqua':'https://www.youtube.com/channel/UC1opHUrw8rvnsadT-iGp7Cg','Subaru':'https://www.youtube.com/channel/UCvzGlP9oQwU--Y0r9id_jnA'}
 
        self.OD = collections.OrderedDict(sorted(self.initial.items())) # used to keep the dict consistent (and sorted through
                                                                        # alphabetical order)
        
 
    def checker(self,name,link):
        live = requests.get(link,headers = self.headers)
        if '{"accessibilityData":{"label":"LIVE"}}},"style":"LIVE","icon":{"iconType":"LIVE"}' in live.text:
            states = "**LIVE**"
        else:
            states = "*Offline*"
        self.toAnswer += [f"{name}: {states}"]
 
        
    def gettp(self):
        self.toAnswer = []
        threads = []
        for i in self.OD.keys():
            process = threading.Thread(target = self.checker,args = [i,self.OD[i]])
            process.start()
            threads.append(process)
 
        for process in threads:
            process.join()
        print(f"STREAMER STATES: Updated ({time.strftime('%H:%M:%s',time.localtime())})")
 
 
    def getRes(self):
        return ('\n'.join(sorted(self.toAnswer)))
 
 
if __name__ == '__main__': # Debugging
    from os import system
    system("title yts Debug")
    wl = LiveChecker()
    while True:
        wl.gettp()
        print(wl.getRes())
        time.sleep(3)
# forgive my weeb!

我在某处读到字典没有排序,所以我认为排序至少可以使其保持一致。然而,我最近读到一篇文章,说字典现在本质上是按条目排序的,就像有序字典一样。

所以我想知道,在上面的例子中,是否仍然需要继续使用有序字典。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。