关于python的列表问题!!求大神!!

Python027

关于python的列表问题!!求大神!!,第1张

#先声明,本人也是初学者,采用了一个比较笨的办法

A = ["aaa","ddd","ccc"]

B = ["aaa","ddd","eee"]

text = input("输入:")

if A[0] in text  and A[1] in text:

    print(A,B)

elif A[2] in text:

    print(A)

elif B[2] in text:

    print(B)

else:

    print('not found')

#第二个也是一样的,都是对数组切片,然后与输入字串比较,符合要求就输出

A = ["123","124","125"]

B = ["123","124","126"]

text = input("输入:")

if A[0] in text  and  A[1] in text:

    print(A,B)

elif A[2] in text:

    print(A)

elif B[2] in text:

    print(B)

else:

    print('not found')

虽然实现的方式不优雅,但确实能够解决你的问题,代码如下:

def plastic(l):

l_sort = sorted(l)

result = list(range(len(l)))

for n,i in enumerate(l_sort,1):

result[l.index(i)] = n

return result

S=[[5,4,3,2,0],[6,5,4,0,1],[0,6,5,1,2],[1,7,6,0,3]]

S=list(map(plastic,S))

print(S)

输出:

[[5, 4, 3, 2, 1], [5, 4, 3, 1, 2], [1, 5, 4, 2, 3], [2, 5, 4, 1, 3]]

matrix = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12]]

# 矩阵转置

# 矩阵的列数

colomn = len(matrix[0])

# 转置矩阵的行数,设置空矩阵[[], [], [], []]

transformMatrix = [[] for i in range(colomn)]

for ele in matrix:

for i in range(colomn):

# transformMatrix[i]标识新矩阵的第i行

# ele[i]标识原有矩阵的第i列

transformMatrix[i].append(ele[i])

print transformMatrix