简体   繁体   English

如何基于定界符区分dict键和值

[英]How to differentiate the dict key and values based on delimiters

I have a dict of type x={'a':'1','b':'2','c;0':'a1;b1;b2;b3;b4','c;1':'a2;b2;b3;b4;b5;b6'} 我有一个类型为x={'a':'1','b':'2','c;0':'a1;b1;b2;b3;b4','c;1':'a2;b2;b3;b4;b5;b6'}的字典x={'a':'1','b':'2','c;0':'a1;b1;b2;b3;b4','c;1':'a2;b2;b3;b4;b5;b6'}

i am writing program like this 我正在写这样的程序

for key,values in x.iteritems():
   if key.split(";") is True:
      print key,value
   else:
      print key,value

its not working properly. 它无法正常工作。

in output what i want on execution of 在输出中我想要执行什么

if key.split(";") is False
   print key,values

output should be 输出应该是

a 1
b 2

in output what i want on execution of 在输出中我想要执行什么

if key.split(";") is True
   print key,values

output should be 输出应该是

c;0  a1;b1;b2;b3;b4,
c;1  a2;b2;b3;b4;b5;b6

I think you don't really need any if / else condition since you're printing key/value pairs for both cases: 我认为您真的不需要任何if / else条件,因为在两种情况下您都将打印键/值对:

>>> for k, v in x.iteritems():
...     print k, v
a 1
b 2
c;1 a2;b2;b3;b4;b5;b6
c;0 a1;b1;b2;b3;b4

Looking at the output you provided, the only difference I see is that your example is ordered. 查看您提供的输出,我看到的唯一区别是示例是有序的。 If that's what you're looking for then, you can use sorted like this: 如果这正是您要寻找的,则可以使用如下所示的sorted

>>> for k, v in sorted(x.iteritems()):
...     print k, v
a 1
b 2
c;0 a1;b1;b2;b3;b4
c;1 a2;b2;b3;b4;b5;b6

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM