列表创建字典_从列表创建字典

本文介绍了如何从列表中高效地构建字典,通过实例解析了列表与字典转换的方法,适合Python初学者和Django开发者参考。
摘要由CSDN通过智能技术生成

列表创建字典

那一件事 (That One Thing)

You know that road-trip game, where you only count cars you see if they are of the same color. On a long, lonely stretch of road, it is a game of anticipation. On a busy highway, it can get a bit neurotic. Sometimes, the game can morph into one of comparing how many different colored cars you can count. Without some sort of tally sheet, keeping count in this way quickly can get out of hand. Even if you make a list comprising each observation, counting them up for each category can be a bit of a hassle.

您知道公路旅行游戏,您只在其中计算汽车的颜色是否相同。 在漫长而孤独的道路上,这是一场期待的游戏。 在繁忙的高速公路上,它可能会变得有些神经质。 有时候,游戏可以变成比较您可以计算多少种不同颜色的汽车之一。 没有某种理赔表,以这种方式快速计数可能会失控。 即使您列出了包含每个观察值的列表,也要为每个类别累加计数可能会有些麻烦。

A digital list can be just trying. You may be able to highlight or sort a spreadsheet list, to make counting a little easier, or perhaps you could aggregate fields in a SQL database. In Python, you also have a few options for managing such a task.

可以尝试使用数字清单。 您可能可以突出显示或对电子表格列表进行排序,以使计数更加容易,或者您可以聚合SQL数据库中的字段。 在Python中,您还有一些管理此类任务的选项。

In this article, will review the following:

在本文中,将回顾以下内容:

  • Understanding the Python list

    了解Python清单
  • What is a dictionary, in Python?

    在Python中,什么是字典?
  • Converting list elements into dictionary keys

    将列表元素转换成字典键
  • How you can count item occurrences in a list, by converting the list to a dictionary

    如何通过将列表转换为字典来计算列表中项目的出现次数
  • Code for implementation in Python

    在Python中实现的代码

Python中的清单 (Lists in Python)

In Python, a list can be any sequence of values. We indicate a list by enclosing the values in square brackets and separating them with commas. Lists may contain numbers, text strings, or any type of object (or a mixture of objects).

在Python中,列表可以是任何值序列。 我们通过将值括在方括号中并用逗号分隔来指示列表。 列表可以包含数字,文本字符串或任何类型的对象(或对象的混合物)。

A Python list of integers from 1–5 looks like this:

1-5的Python整数列表如下所示:

# spaces after the commas are not required
[1, 2, 3, 4, 5]

Strings in a list are surrounded by quotes:

列表中的字符串用引号引起来:

# a four-item combination of strings and numbers
["cat",3,1,"dog"]

Lists can even contain other lists:

列表甚至可以包含其他列表:

# a four-item list of two lists, a number and a string
[[1, 2, 3, 4, 5], 541, ["cat",3,1,"dog"], "Chuck"]# and, of course, we can assign a variable to our list
my_mixed_list = [[1, 2, 3, 4, 5], 541, ["cat",3,1,"dog"], "Chuck"]

Python lists are mutable. This means you can change them. You can perform math on list items, change their order, or append new items to the list. In fact, the actions that may be performed on lists amounts to — well — a very long list. Let’s see what a couple of these operations look like.

Python列表是可变的。 这意味着您可以更改它们。 您可以对列表项执行数学运算,更改其顺序或将新项追加到列表。 实际上,可以对列表执行的操作相当长。 让我们看看其中的几个操作是什么样的。

操作一: (Operation 1:)

# multiplying a list to extend the list
my_list = [1, 2, 3, 4, 5]
result = my_list * 2
print(result)> [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

The above operation results in a list that is twice as long, with each item reoccurring in its original order. We can verify the lengths or number of items in our lists with Python’s len() list method:

上面的操作导致列表的长度是原来的两倍,并且每个项目都以其原始顺序重复出现。 我们可以使用Python的len() list方法来验证列表中项目的长度或数量

# printing the length of `my_list`
print("The length of my_list is ", len(my_list))> The length of my_list is 5# printing the length of `result`
print("The length of result is ", len(result))> The length of result is 10

操作2: (Operation 2:)

# multiplying list items
my_list = [1, 2, 3, 4, 5]# creating a new list by multiplying each `mylist` item (i) by two
result2 = [i*2 for i in my_list]print(result2)> [
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值