在Python中将列表转换为字典的10种方法

本文详细介绍了在Python中将列表转换为字典的十种方法,包括将元组列表、相同长度或不同长度的两个列表、备用键值对列表、字典列表,以及使用enumerate()、字典理解、dict.fromkeys()、Counter()等方法进行转换。
摘要由CSDN通过智能技术生成

Python数据结构 (Python Data Structures)

Python lists and dictionaries are two data structures in Python used to store data. A Python list is an ordered sequence of objects, whereas dictionaries are unordered. The items in the list can be accessed by an index (based on their position) whereas items in the dictionary can be accessed by keys and not by their position.

Python列表和字典是Python中用于存储数据的两个数据结构。 Python列表是对象的有序序列,而字典是无序的。 列表中的项目可以通过索引(基于它们的位置)来访问,而字典中的项目可以通过键而不是它们的位置来访问。

Let's see how to convert a Python list to a dictionary.

让我们看看如何将Python列表转换成字典。

将Python列表转换为字典的十种不同方法 (Ten different ways to convert a Python list to a dictionary)

  1. Converting a list of tuples to a dictionary

    将元组列表转换为字典
  2. Converting two lists of the same length to a dictionary

    将两个相同长度的列表转换为字典
  3. Converting two lists of different length to a dictionary

    将两个不同长度的列表转换为字典
  4. Converting a list of alternative key, value items to a dictionary

    将备用键,值项列表转换为字典
  5. Converting a list of dictionaries to a single dictionary

    将词典列表转换为单个词典
  6. Converting a list into a dictionary using enumerate()

    使用enumerate()将列表转换成字典
  7. Converting a list into a dictionary using dictionary comprehension

    使用字典理解将列表转换为字典
  8. Converting a list to a dictionary using dict.fromkeys()

    使用dict.fromkeys()将列表转换成字典
  9. Converting a nested list to a dictionary using dictionary comprehension

    使用字典理解将嵌套列表转换为字典
  10. Converting a list to a dictionary using Counter()

    使用Counter()将列表转换为字典

1.将元组列表转换为字典 (1. Converting a List of Tuples to a Dictionary)

The dict() constructor builds dictionaries directly from sequences of key-value pairs.

dict()构造函数直接从键值对序列中构建字典。

l1=[(1,'a'),(2,'b'),
    (3,'c'),(4,'d')]
d1=dict(l1)
print (d1)
#Output:{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

2.将两个相同长度的列表转换成字典 (2. Converting Two Lists of the Same Length to a Dictionary)

We can convert two lists of the same length to the dictionary using zip().

我们可以使用以下命令将两个相同长度的列表转换为字典 zip()

zip() will return an iterator of tuples. We can convert that zip object to a dictionary using the dict() constructor.

zip()将返回一个元组的迭代器。 我们可以使用以下方式将zip对象转换为字典 dict()构造函数。

压缩() (zip())

Make an iterator that aggregates elements from each of the iterables.

创建一个迭代器,以聚合每个可迭代对象中的元素。

zip(*iterables): Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.” — Python documentation

zip ( *iterables ) 返回一个元组的迭代器,其中第i个元组包含每个参数序列或iterables中的第i个元素。 当最短的可迭代输入耗尽时,迭代器停止。 使用单个可迭代参数,它返回1元组的迭代器。 没有参数,它将返回一个空的迭代器。” — Python 文档

Example:

例:

l1=[1,2,3,4]
l2=['a','b','c','d']
d1=zip(l1,l2)
print (d1)#Output:<zip object at 0x01149528>
#Converting zip object to dict using dict() contructor.
print (dict(d1))
#Output:{1: 'a', 2: 'b', 3: 'c', 4: 'd'}
Image for post
Image source: Author
图片来源:作者

3,将两个不同长度的列表转换成字典 (3.Converting Two Lists of Different Length to a Diction

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值