python字典排序奇怪(想要非字母顺序)

发布于 2024-11-30 04:28:26 字数 895 浏览 0 评论 0原文

所以我试图发送一个包含一些我已放入字典中的密钥的数据包。这些需要以特定的顺序打印,所以我尝试创建一个函数来重新排序它们。我认为 python 正在按照我不想要的顺序重新排列,但我不确定为什么。

我目前正在使用这个功能来尝试实现这一目标。

def array_sort(array, sort): #yeah, it's array cuz i php like that.
    ordered = {}
    for i in range(0,len(sort)):
        if sort[i] in array:
            ordered[sort[i]] = array[sort[i]]
    keys = array.keys()
    return ordered 


order = "l5,l4,l3,l2,q,y,k,k3,d1,z,p,c,b,d3,dt,N,n,a,h,v".split(',')
keys = array_sort(infos, order)

由于某种原因,这不起作用, infos 是按字母顺序排列的键列表,我只是不确定为什么该函数以奇数顺序输出键。 (第一个,当 15 应该是第一个时:S)

如果你知道更好的方法来做到这一点,请随时告诉我,我最近刚刚开始使用 python,所以我不太熟悉。

编辑: 在制作订单字典后,我能够立即使用它以正确的顺序打印密钥。即使它只是作为字符串输出,顺序也会被保留(暂时),并且您可以再次 .split() 以获得正确顺序的字典(我认为)。

for i in range(0, len(order)):
    if order[i] in infos:
        packet += order[i] + '="' + infos[order[i]] + '" '

so I am trying to send a packet with some keys that i have put into a dictionary. these need to be printed in a specific order, so I've tried to make a function to reorder them. I assume python is doing something to rearrange in the order I don't want, but i'm not sure why.

I'm currently using this function to try and achieve this.

def array_sort(array, sort): #yeah, it's array cuz i php like that.
    ordered = {}
    for i in range(0,len(sort)):
        if sort[i] in array:
            ordered[sort[i]] = array[sort[i]]
    keys = array.keys()
    return ordered 


order = "l5,l4,l3,l2,q,y,k,k3,d1,z,p,c,b,d3,dt,N,n,a,h,v".split(',')
keys = array_sort(infos, order)

for some reason this isn't working, infos is the list of keys in alphabetical order, i'm just not sure why the function is outputting the keys in an odd order. (a first, when 15 should be first :S)

If you know a better way to do this feel free to tell me, I just recently started on python, so I'm not that familiar.

EDIT:
I was able to print the keys in the correct order using this immediately after making the order dictionary. even if it was just output as a string the order would be preserved (for the time being) and you could .split() it again to get the dictionary in the correct order (i think).

for i in range(0, len(order)):
    if order[i] in infos:
        packet += order[i] + '="' + infos[order[i]] + '" '

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南七夏 2024-12-07 04:28:26

由于Python中的字典是无序的,你所需要的只是输出按指定规则排序的结果,你可以这样做:

order = "l5,l4,l3,l2,q,y,k,k3,d1,z,p,c,b,d3,dt,N,n,a,h,v".split(',')

for key in order:
    print key, infos.get(key)#if infos is your dictionary

或者你可以通过以下方式获取/传递列表而不是字典:

print [(key, infos.get(key)) for key in order]

Since dictionaries in Python are unordered and all that you need is to output results ordered by the specified rule you can do something like this:

order = "l5,l4,l3,l2,q,y,k,k3,d1,z,p,c,b,d3,dt,N,n,a,h,v".split(',')

for key in order:
    print key, infos.get(key)#if infos is your dictionary

Or you can get/pass list instead of dict the following way:

print [(key, infos.get(key)) for key in order]
享受孤独 2024-12-07 04:28:26

Python 字典不是有序的容器。看看 collections.OrderedDict

Python dictionaries are not ordered containers. Take a look at the collections.OrderedDict instead

っ左 2024-12-07 04:28:26

Python 字典根据哈希值对键进行排序,这使得它看起来是随机的。我建议使用 OrderedDict 来按顺序获取密钥。

Python dictionaries order their keys based on its hash, which will make it seem random. I recommend using OrderedDict to get your keys in order.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文