Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python列表。。两个功能。。。我有第一个(我想),但没有第二个_Python_Python 2.7_Python 3.x - Fatal编程技术网

Python列表。。两个功能。。。我有第一个(我想),但没有第二个

Python列表。。两个功能。。。我有第一个(我想),但没有第二个,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,以数字x作为输入参数的函数。然后它生成一个x列表,所有x元素都是随机数,并返回它。另一个函数,用于打印从第一个值生成的列表的第二个值。我不太确定如何编写第二个函数。如果我理解正确,您只想打印每个列表的第二个值,那么: def f(x): L1=[] for y in range(x): L2=[] for z in range(x): m=randrange(0,100) L2.append(m)

以数字x作为输入参数的函数。然后它生成一个x列表,所有x元素都是随机数,并返回它。另一个函数,用于打印从第一个值生成的列表的第二个值。我不太确定如何编写第二个函数。

如果我理解正确,您只想打印每个列表的第二个值,那么:

def f(x):
    L1=[]
    for y in range(x):
        L2=[]
        for z in range(x):
            m=randrange(0,100)
            L2.append(m)
        L1.append(L2)
    return(L1)

print(f(5))
def first(x):
  return [[random.randint(0,100) for _ in range(x)] for _ in range(x)]

def second(L):
  for item in map(operator.itemgetter(1), L):
    print(item)

为什么不发布第二部分的尝试解决方案?我不希望有太多的人来解决你的家庭作业,因为你能解释一下答案吗。
map(f,L)
对列表中的每个元素调用函数
f
,并返回一个列表,其中包含对每个元素调用
f
的结果
operator.itemgetter(1)
生成一个函数,当对列表调用时,该函数返回该列表中索引
1
处的元素。
def print_second(L):
    for c in range(len(L)): #go through all of the lists
        if len(L[c]) >= 2:  #don't look for 2nd item of list of length 1
            print(L[c][1])  #print the second item (starting from 0, index 1)