字典交集后去掉括号
我正在研究一个问题,并得到了一个非常接近的答案......基本上,问题是你有两个字典,你必须找到与两个字典相交的元素,然后创建这些元素(两个字典中的一个相同的键)以及来自两个 dic 的两个值)在新字典中。
a = {'A':17,'B':31,'C':42,'D':7,'E':46,'F':39,'G':9}
b = {'D':8,'E':3,'F':2,'g':5}
def intersect(a,b):
c = set(a).intersection(set(b))
d = {}
for i in c:
if i in a:
d[i] = int(a[i]),int(b[i])
return d
OUTPUT: {'E': (46, 3), 'D': (7, 8), 'F': (39, 2)}
我想要得到类似 {'E': 46, 3, 'D': 7, 8, 'F': 39, 2} 的输出
如何去掉值周围的括号?
There is one question I am working on and got a very close answer... basically, the question is that you get two dictionaries and you have to find elements that intersect from both dictionaries and then create those elements (one same key from both dicts and two values from both dics) in a new dictionary.
a = {'A':17,'B':31,'C':42,'D':7,'E':46,'F':39,'G':9}
b = {'D':8,'E':3,'F':2,'g':5}
def intersect(a,b):
c = set(a).intersection(set(b))
d = {}
for i in c:
if i in a:
d[i] = int(a[i]),int(b[i])
return d
OUTPUT: {'E': (46, 3), 'D': (7, 8), 'F': (39, 2)}
I want to get the output like {'E': 46, 3, 'D': 7, 8, 'F': 39, 2}
How do I get rid of the parentheses around the values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您编写的代码根本不会输出任何内容。但是,如果您想删除括号,则可以使用它。
或者等效于此,这更简洁和高效
The code as you have written won't output anything at all. However, if you want to remove parentheses, then you can use this.
or equivalently this, which is a bit more concise and efficient
您看到的输出是字典的 Python 表示形式。您所构建的(据我所知,您已经正确构建了它 - 这就是您想要的)是将键映射到项目对的字典。这些对是元组,并且在它们周围打印有括号。
听起来您想要的是一种获取字典并打印它并以特定方式格式化的方法。
像这样的东西会按照你想要的方式打印字典:
The output you are seeing is the python representation of your dictionary. What you have built (and, as far as I can tell, you have built it correctly -- it's what you want) is a dictionary mapping keys to pairs of items. The pairs are tuples, and those get printed with parentheses around them.
It sounds like what you want is an method which takes your dictionary and prints it, formatted in a specific way.
Something like this would print the dictionary the way you want it to: