简体   繁体   English

命名列表中的每一项都是字典的值

[英]Naming each item in a list which is a value of a dictionary

I have a dictionary for which key is a normal string and value is a tuple for which example is shown below: 我有一本字典,其键为普通字符串,值为元组,其示例如下所示:

'Europe':(Germany, France, Italy)
'Asia':(India, China, Malaysia)  

I want to display the dictionary items like this: 我想显示这样的字典项目:

'Europe':(RandomStringA:Germany, RandomStringB:France, RandomStringC:Italy)  
'Asia':(RandomStringA:India, RandomStringB:China, RandomStringC:Malaysia)  

I tried the code below: 我尝试了下面的代码:

for k, v in dict.iteritems()  
    print k, "Country1":v[0], "Country2":v[1], "Country3":v[2]  

But this does not seem to work. 但这似乎不起作用。 Is there a way to tag items in a tuple like that? 有没有办法像这样在元组中标记项目? Thanks in advance! 提前致谢!

If you're just trying to print that: 如果您只是尝试打印:

for k, v in dct.iteritems():
    print repr(k)+ ":(" + ", ".join("Country{}:{}".format(i,c) for i,c in enumerate(v, start=1)) + ")"

Output: 输出:

'Europe':(Country1:Germany, Country2:France, Country3:Italy)
'Asia':(Country1:India, Country2:China, Country3:Malaysia)

Note: I'm abusing the function of repr() to get the quotes in there. 注意:我正在滥用repr()函数以获取引号。 You could just as well do "'" + str(k) + "'" . 您也可以执行"'" + str(k) + "'"

The reason why your code doesn't work is your use of : outside of a dictionary initialization or comprehension. 代码不起作用的原因是在字典初始化或理解之外使用了: That is, you can do d = {'a':'b'} but you can't do print 'a':'b' . 也就是说,您可以执行d = {'a':'b'}但不能print 'a':'b' Also, you shouldn't use dict as a variable name, because it is a keyword. 另外,不应将dict用作变量名,因为它是关键字。

My solution will work for tuples which have more (or even less) than 3 elements in them, too. 我的解决方案也适用于其中包含三个(或更少)三个元素的元组。

mainDict = {"Europe": ("Germany", "France", "Italy"),
  "Asia": ("India", "China", "Malaysia")
}

for item in mainDict:
    print "%s:(%s)" % (item, ", ".join(["Country%s:%s" % (r+1, y) for r, y in enumerate(mainDict[item])]))

Print out: 打印:

Europe:(['Country1:Germany', 'Country2:France', 'Country3:Italy'])
Asia:(['Country1:India', 'Country2:China', 'Country3:Malaysia'])

There's nothing built-in that I know of that will do it, but it's pretty simple to do what you want: 我不知道有什么内置函数可以做到,但是要做您想要的事情很简单:

countries = {
    'Europe': ('Germany', 'France', 'Italy'),
    'Asia': ('India', 'China', 'Malaysia'),
}

for k, v in countries.iteritems():
    print k+':', tuple(map(lambda c: 'Country%d:%s' % c, enumerate(v, start=1)))

Output: 输出:

Europe: ('Country1:Germany', 'Country2:France', 'Country3:Italy')
Asia: ('Country1:India', 'Country2:China', 'Country3:Malaysia')

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

相关问题 Python:将项目添加为列表,该列表是字典中的值 - Python: Adding an item a list which is a value in a dictionary 将项添加到字典列表中具有相同值的字典 - Add item to dictionary which has same value in list of dictionary 将列表与字典的每个值(是字符串列表)进行比较 - Comparing a list to each value of dictionary (which are a list of strings) 将每个值都是一个列表并且该列表中的每个项目都是另一个字典的字典转换为元组 - Converting a dictionary where each value is a list and each item in that list is another dictionary into a tuple 如何计算列表中作为字典中的值的项目的重复次数? - How to count the number of repeats for an item in a list which is a value in a dictionary? 向列表或词典中的每个项目添加年份 - Add a Year to each item in list or Dictionary 如何用列表中的每个字典的元组替换字典值 - How to replace dictionary values which is dictionary with tuple for each dictionary in list 将字典值添加到列表,这是字典的值 - Adding dictionary values to a list, which is a value to a dictionary 用每个列表/字典值更新每一行 - Update each row with each list/dictionary value 给定一个字典和一个浮点数列表 L,如何找到与 L 中的每个值最接近的键值? - Given a dictionary and a list of floats L, how to find which keys has the closest value to each value in L?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM