[英]what does x[1] mean?
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use("ggplot")
plt.figure(figsize=(10,5))
N = len(sortedAverageList)
x = np.arange(1,N+1)
y = [x[1] for x in sortedAverageList]
width = 1
labels = [x[0] for x in sortedAverageList]
What does [x[1] for x in sortedAverageList]
in the coding above?在上面的编码中
[x[1] for x in sortedAverageList]
是什么? What does x[1]
mean? x[1]
是什么意思?
If sortedAverageList
is a sequence containing another sequence with at least 2 elements, [x[1] for x in sortedAverageList]
will give you a list of the second element in each of those sequences.如果
sortedAverageList
是一个包含另一个至少具有 2 个元素的序列的序列,则[x[1] for x in sortedAverageList]
将为您提供每个序列中第二个元素的列表。
Example:例子:
sortedAverageList = [[1,2],[5,6],[7,8]]
print ([x[1] for x in sortedAverageList] )
#prints [2,6,8]
For each of the lists [1,2]
, [5,6]
, [7,8]
, x[1]
chooses the respective second element.对于列表
[1,2]
、 [5,6]
、 [7,8]
, x[1]
选择相应的第二个元素。 x[0]
would choose the first and x[2]
would not work because the lists only have two elements. x[0]
将选择第一个,而x[2]
将不起作用,因为列表只有两个元素。
Note that the x
in [x[1] for x in sortedAverageList]
has nothing to do with the x
you define in the line above.请注意,
x
在[x[1] for x in sortedAverageList]
无关与x
你在上面的线定义。 Instead it is the variable inside the for loop to which the elements of sortedAverageList
are repeatedly assigned.相反,它是 for 循环内的变量,
sortedAverageList
的元素被重复分配给它。
I would recomment to study some basic python tutorial before continuiung with more advanced tasks like plotting.在继续进行更高级的任务(如绘图)之前,我建议先学习一些基本的 Python 教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.