首页 \ 问答 \ 在循环字典中分配值(Assigning values in looped dictionary)

在循环字典中分配值(Assigning values in looped dictionary)

我正在努力解决这个循环,我想为嵌套字典赋值。 我有一个名为stp2的列表和一个默认的dict,我创建时没有作为值。 我试图逐个获取列表元素并分配给字典。

stp2=['Anna', 'William', 'Mary', 'Ben', 'Richard','Calvin', 'Rock']

而my_dict是默认的dict(嵌套),如下所示:

defaultdict(<class 'dict'>, {0: {'name': None, 'price': None,}, 1:{'name': None, 'price': None,}, 2: {'name': None, 'price': None,}, 3: {'name': None, 'price': None,}, 4: {'name': None, 'price': None,}, 5: {'name': None, 'price': None,}, 6: {'name': None, 'price': None,}})

我正在使用此循环来更新值:

for z in enumerate(stp2):
 my_dict[z[0]]['name']=z[1]

print(my_dict)

理想情况下我的输出应该是这样的,但我在重复的每一个值中得到“摇滚”。

理想输出:

{0: {'name': 'Anna', 'price': None,}, 1:{'name': 'William', 'price': None,}, 2: {'name': 'Mary', 'price': None,}, 3: {'name': 'Ben', 'price': None,}, 4: {'name': 'Richard', 'price': None,}, 5: {'name': 'Calvin', 'price': None,}, 6: {'name': 'Rock', 'price': None,}}

感谢你的帮助。


I am struggling with this loop where I want to assign values to a nested dictionary. I have a list called stp2 and a default dict that I created with none as values. I am trying to take list elements one by one and assign to the dictionary.

stp2=['Anna', 'William', 'Mary', 'Ben', 'Richard','Calvin', 'Rock']

whereas my_dict is a default dict (nested) which looks like this:

defaultdict(<class 'dict'>, {0: {'name': None, 'price': None,}, 1:{'name': None, 'price': None,}, 2: {'name': None, 'price': None,}, 3: {'name': None, 'price': None,}, 4: {'name': None, 'price': None,}, 5: {'name': None, 'price': None,}, 6: {'name': None, 'price': None,}})

I am using this loop to update values:

for z in enumerate(stp2):
 my_dict[z[0]]['name']=z[1]

print(my_dict)

My output should ideally look like this but I am getting "Rock" in every value repeated.

Ideal output:

{0: {'name': 'Anna', 'price': None,}, 1:{'name': 'William', 'price': None,}, 2: {'name': 'Mary', 'price': None,}, 3: {'name': 'Ben', 'price': None,}, 4: {'name': 'Richard', 'price': None,}, 5: {'name': 'Calvin', 'price': None,}, 6: {'name': 'Rock', 'price': None,}}

Appreciate your help.


原文:https://stackoverflow.com/questions/48524876
更新时间:2022-08-26 21:08

最满意答案

为了评估优缺点,我认为您应该关注微服务架构旨在实现的目标。 在我看来,微服务是一种建筑风格,旨在构建松散耦合的应用程序。 它不是为构建高性能应用程序而设计的,因此当我们决定以微服务方式构建应用程序时,我们已经准备好接受性能和数据冗余的划痕。

我不认为你的服务应该共享数据库。 更紧密的耦合掩盖了微服务架构的主要目标。 我的建议是创建一个统一数据服务,从所有其他服务中获取数据更改事件并更新其后面的数据库。 您可能希望以针对查询优化的方式(如数据仓库)设计统一数据服务后面的数据库,因为这将是所有此服务的用途。 您可能需要考虑使用NoSQL数据库来支持您的整合数据服务。


To evaluate the pros and cons I think you should focus on what microservices architecture is aiming to achieve. In my opinion Microservices is architectural style aiming to build loosely couple applications. It is not designed to build high performance application so scarification of performance and data redundancy are something we are ready accept when we decided to build applications in a microservices way.

I don't think you services should share database. Tighter coupling scarify the main objective of the microservices architecture. My suggestion is to create a consolidated data service which pick up the data changes events from all the other services and update the database behind it. You might want to design the database behind the consolidated data service in a way that is optimised for query (like a data warehouse) because that's all this service will be used for. You might want to consider using a NoSQL database to support your consolidated data service.

相关问答

更多
  • 你拥有的逻辑并不是错误的,但更好的是在向另一个服务发出请求之前构建一个抽象层,例如。 另一个微服务的API网关。 让我们为这个实例调用微服务B(API网关向B发出请求)。 在这种情况下,B应该提供自己的客户端,了解另一个服务应该如何与之交互,无论是通过HTTP还是WebSockets,协议都取决于B,因为B了解应该如何与之通信。 客户和正在一起实施的服务的论点是,这两个组件应具有更高的凝聚力,因为从技术上讲,它们受合同约束,例如。 如果需要对服务进行请求,则需要遵守服务所需的合同。 在使用Express的简 ...
  • 我不知道整个系统的设计,但是我会让用户微服务在用户创建,更新或删除时发送一个事件。 所有对此事件感兴趣的其他微服务都可以提取他们需要的必要用户数据。 通过使用微服务,无法避免冗余数据,但每个微服务对数据都有不同的看法。 您的用户microservice可能会将用户视为完整用户数据(例如,ID,密码,名字,姓氏,电子邮件,地址,组织等)的身份。另一方面,您的文档生成微服务可能会将用户视为接收者并且不需要所有的用户数据。 I don't know the design of the whole system, ...
  • 在你的情况下,使用直接REST调用应该没问题。 选项1使用Rest API: 当你需要同步通讯时。 例如,你的情况。 这个选项是合适的。 选项2使用AMQP: 当你需要异步通信。 例如,当您的订单服务创建订单时,您可能希望通知产品服务以减少产品数量。 或者您可能希望为用户订购成功的用户服务。 我强烈建议看看http://microservices.io/patterns/index.html In your case using direct REST calls should be fine. Optio ...
  • 如果你想保持微服务架构的稳固,我一定会选择1号; 如果您将“用户/用户状态”视为可以单独运行的完全独立的产品。 我认为防止冗余实现/数据的额外调用是可行的方法,如果Redis支持它,并且前面的API正在执行那么你应该没有额外调用的问题。 并且通过尽可能地将系统分离来获得胜利。 如果不是这样,那么2将是一个很好的解决方案。 您将面临有关数据完整性和复制问题的挑战,但它是其中一种方法。 I will definitely go with number 1 if you want to keep the micr ...
  • 这实际上取决于您的目标和要求。 例如 - 是否有特定要求在VM级别隔离微服务? 如果没有,那么你肯定会通过在运行多个容器的单个VM上运行,或者在没有完整的操作系统开销的情况下甚至更低级别运行来实现效率 - 只需运行容器(例如CoreOS,Rancher等) 请注意,Kubernetes将帮助您组织和协调您的容器,使您能够通过容器进行分段,并提供其他方便的连接,如容器之间的网络连接(这是一个非常常见的要求),但Kubernetes将不具备任何特定应用级别的知识。申请是整体的(也不应该)。 如果您可以指定要优化 ...
  • 为了评估优缺点,我认为您应该关注微服务架构旨在实现的目标。 在我看来,微服务是一种建筑风格,旨在构建松散耦合的应用程序。 它不是为构建高性能应用程序而设计的,因此当我们决定以微服务方式构建应用程序时,我们已经准备好接受性能和数据冗余的划痕。 我不认为你的服务应该共享数据库。 更紧密的耦合掩盖了微服务架构的主要目标。 我的建议是创建一个统一数据服务,从所有其他服务中获取数据更改事件并更新其后面的数据库。 您可能希望以针对查询优化的方式(如数据仓库)设计统一数据服务后面的数据库,因为这将是所有此服务的用途。 您 ...
  • 你不能假设url的结构,但你可以返回一个实体ID。 我应该在图片微服务中使用“/ users / 1”(“self”链接)作为用户ID吗? 如果你这样做,你假设第二个微服务使用相同的url方案,这是不好的。 你将两个微服务结合在一起。 此外,请求响应中包含的网址仅对该(微)服务有意义,您不能仅仅假设另一个系统中的相应resource具有相同的id You cannot assume the structure of the url but you can return an entity id. Shoul ...
  • 另外,我一直在阅读Apache Thrift和RPC如何帮助解决这个问题。 任何人都可以详细说明吗? 像Apache Thrift这样的RPC框架的目标是 显着减少手动编程开销 提供有效的序列化和传输机制 跨越各种编程语言和平台 换句话说,这允许您通过线路将数据作为非常紧凑的写入和压缩数据包发送,而实现此目的所需的大部分工作由框架提供。 Apache Thrift为您提供可插拔的传输/协议栈,可以通过插入不同的方式快速进行调整 传输(套接字,HTTP,管道,流,......) 协议(二进制,紧凑,JSON, ...
  • 一般性前言 - 如果您不需要,请不要这样做。 如果您处理大量事件和事件备份问题等,引入队列系统可能是一个很大的改进。但如果您没有遇到任何问题,您可能会更好地利用直接服务通信的低复杂性。 回到你的问题 - 听起来你想要抽象与队列的通信,因为你担心用不同的系统替换队列的努力 - 这是正确的吗? 在这种情况下,你可以做你提出的建议 - 在中间开发一项新服务。 这附带了物理服务的所有包袱(包括部署,扩展等)。 或者第二种方法是编写一个客户端库,以您希望的方式抽象队列,并允许您在需要参与队列的所有服务中重用它。 这样 ...
  • 您无需将帐户数据复制到视频/图像服务中。 存储视频/图片时,只需提供帐户ID即可。 例如,当您需要获取用户及其图像时,您可以使用不同的选项来获取数据: 如果您使用API网关,则可以从服务器进行2次异步并行调用,并将这两种数据合并到DTO中并将其返回。 如果由于您首先需要来自用户的特定数据而无法做到这一点,则只需拨打帐户服务,然后轮流将调用图像服务以检索其数据,然后将整个包发回。 您可以自行决定是否可以。 简而言之,只需存储每个视频/图像实体的帐户ID。 You don't need to copy the ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)