python元组转换为字典,将python中的元组列表转换为字典

社交网络喜欢关系解析
本文介绍了一种使用Python实现的方法,该方法通过处理元组列表来解析社交网络中的人际喜欢关系,并将其转换为易于理解的字典格式。具体而言,文章提供了一个名为likes_relation的函数,它接受一个包含姓名及其喜欢对象的元组列表作为输入,然后返回一个字典,该字典以姓名为键,值为他们喜欢的人的列表。

Imagine a social network website which allows people to specify which other people they like.

We can store information about who likes who in a list of tuples, such as the one assigned to

friendface below:

friendface = [

(’Zeus’,’Apollo’),

(’Zeus’,’Aphrodite’),

(’Apollo’,’Aphrodite’),

(’Athena’,’Hera’),

(’Hera’,’Aphrodite’),

(’Aphrodite’,’Apollo’),

(’Aphrodite’,’Zeus’),

(’Athena’,’Aphrodite’),

(’Aphrodite’,’Athena’),

(’Zeus’,’Athena’),

(’Zeus’,’Hera’),

Write a Python function likes_relation(network) which takes a list of tuples as

its argument (in the format described above) and returns a dictionary as its result. The outputdictionary has strings for keys (representing names of people) and lists of strings for values (representing lists of names of people).

Each person in the dictionary is associated with the list of all and only the people that they like. For example, the function should behave like so when applied to the friendface list:

likes_relation(friendface)

{ 'Aphrodite': ['Apollo', 'Zeus', 'Athena'],

'Hera': ['Aphrodite'],

'Zeus': ['Apollo', 'Aphrodite', 'Athena', 'Hera'],

'Apollo': ['Aphrodite'],

'Athena': ['Hera', 'Aphrodite'] }

Sorry should add it's from a list of example exam questions but no answers are given.

I got as far as:

def likes_relations(network):

likes = {}

for k, v in network:

after than i am a bit lost as its not like any of the examples we did in class

解决方案

Use either defaultdict(list) or dict.setdefault(..., []) - there's not much difference in performance or readability, so it's really a matter of taste. I prefer using setdefault:

likes = {}

for k, v in friendface:

likes.setdefault(k, []).append(v)

要将Python中的元组转换字典,可以使用以下方法: ``` def tuple_to_dict(tuples): dictionary = dict(tuples) return dictionary ``` 这个方法将一个元组列表作为参数,并使用`dict()`函数将其转换字典。返回的字典中,元组中的第一个元素作为键,第二个元素作为值。 例如,如果我们有一个元组列表如下: ``` tuples = [('a', 1), ('b', 2), ('c', 3)] ``` 使用`tuple_to_dict(tuples)`将返回以下字典: ``` {'a': 1, 'b': 2, 'c': 3} ``` 这样,我们就成功将元组转换字典。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python元组转换字典_将python中的元组列表转换字典](https://blog.csdn.net/weixin_39620334/article/details/109927729)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [头歌Python入门之元组字典](https://download.csdn.net/download/qq_56893454/86721975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值