python中怎样删除二维数组同一行相同的数?

neoconstantinus7 2013-07-03 06:01:02
both previous grids with the modification that if a letter would appear more than once horizontally, then the second and subsequent occurrences are removed
...全文
385 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
libralibra 2013-07-04
  • 打赏
  • 举报
回复
引用 4 楼 u011286446 的回复:
等我写完了 我才看到你们发的不过真心赞!我写的不好,才学python几天,要走的路还很长啊! 我把整个题都发上来了,是求4个lists。原矩阵,转置的,和删除同一行相同出现的
删除相同的上面写过了,原矩阵你写的方式也可以,转置就很简单了,测试代码
# transpose
def transpose(mat):
    return [[mat[row][col] for row in range(len(mat))] for col in range(len(mat[0]))]

# print matrix
def printmat(mat):
    for row in mat:
        for col in row:
            print '{0:2d}'.format(col),
        print

# test
row, col = 4, 4
a = [[x for x in range(i*row,i*row+col)] for i in range(row)]

# original
print 'Original'
printmat(a)

# transpose
print '\nTransposed'
printmat(transpose(a))
测试
>>> ================================ RESTART ================================
>>> 
Original
 0  1  2  3
 4  5  6  7
 8  9 10 11
12 13 14 15

Transposed
 0  4  8 12
 1  5  9 13
 2  6 10 14
 3  7 11 15
>>> 
neoconstantinus7 2013-07-04
  • 打赏
  • 举报
回复
引用 1 楼 libralibra 的回复:
这个按照意思写就行了,如果已经有了,直接跳过,否则加入
>>> a=[	['a','b','c','a'],
	['e','b','d','f'],
	['h','g','h','h']]
>>> a
[['a', 'b', 'c', 'a'], ['e', 'b', 'd', 'f'], ['h', 'g', 'h', 'h']]
>>> b = []
>>> c = []
>>> for m in a:
	c = []
	for n in m:
		if n not in c:
			c.append(n)
	b.append(c)

	
>>> b
[['a', 'b', 'c'], ['e', 'b', 'd', 'f'], ['h', 'g']]
等我写完了 我才看到你们发的不过真心赞!我写的不好,才学python几天,要走的路还很长啊! 我把整个题都发上来了,是求4个lists。原矩阵,转置的,和删除同一行相同出现的
print "input the amount of rows"
n=input()
print "input the amount of columns"
m=input()
lists_1 = [[] for i in range(n)]
lists_2 = [[] for i in range(m)]
lists_3 = [[] for i in range(n)]
lists_4 = [[] for i in range(m)]

for i in range(n):
  for j in range(m):
    x=raw_input()
    lists_1[i].append(x)
lists_2= map(list, zip(*lists_1))

for i in range(n):
 lists_3[i].append(lists_1[i][0])
 for j in range(m):
  for k in range(j+1,m):
   if(lists_1[i][k]!=lists_1[i][j]):
    cnt=0
    for d in range(len(lists_3[i])):
     if(lists_1[i][k]!=lists_3[i][d]):
      cnt=cnt+1
     if(cnt==len(lists_3[i])):
      lists_3[i].append(lists_1[i][k])
for i in range(m):
 lists_4[i].append(lists_2[i][0])
 for j in range(n):
  for k in range(j+1,n):
   if(lists_2[i][k]!=lists_2[i][j]):
    cnt=0
    for d in range(len(lists_4[i])):
     if(lists_2[i][k]!=lists_4[i][d]):
      cnt=cnt+1
     if(cnt==len(lists_4[i])):
      lists_4[i].append(lists_2[i][k])      
print lists_1
print lists_2
print lists_3
print lists_4
libralibra 2013-07-03
  • 打赏
  • 举报
回复
引用 2 楼 wang8118 的回复:

a=[['a','b','c','a'],
    ['e','b','d','f'],
    ['h','g','h','h']]

b = [list(set(x)) for x in a]
print(b)
开始也是这么考虑的,但是set后不能保证原来元素的次序不变,
梅小西Echo 2013-07-03
  • 打赏
  • 举报
回复

a=[['a','b','c','a'],
    ['e','b','d','f'],
    ['h','g','h','h']]

b = [list(set(x)) for x in a]
print(b)
libralibra 2013-07-03
  • 打赏
  • 举报
回复
这个按照意思写就行了,如果已经有了,直接跳过,否则加入
>>> a=[	['a','b','c','a'],
	['e','b','d','f'],
	['h','g','h','h']]
>>> a
[['a', 'b', 'c', 'a'], ['e', 'b', 'd', 'f'], ['h', 'g', 'h', 'h']]
>>> b = []
>>> c = []
>>> for m in a:
	c = []
	for n in m:
		if n not in c:
			c.append(n)
	b.append(c)

	
>>> b
[['a', 'b', 'c'], ['e', 'b', 'd', 'f'], ['h', 'g']]

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧