首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Perl 求教数组的排列,多谢

2013-08-01 
Perl 求教数组的排列,急,谢谢@ax(6,9100,98,70,51,68)#这个数组代表的数目@bx(300,400

Perl 求教数组的排列,急,谢谢
@ax=("6",9""100","98","70","51","68")   #这个数组代表的数目

@bx=("300","400","250","3175","750","2050","1950") #这个数组代表的大小

@cx=("hole","hole","slot","hole","slot","hole","slot",) #表示形状 hole表示孔,slot表示槽

#上面的数据是意义对应的

# 要求:  如果@cx里面都是hole的话: 把@bx中的3175排在前面,然后就是由小到大的顺序排列

          如果@cx里面有hole和slot两中形状的话,首先把@bx中的3175排在前面,然后就是由小到大的顺序排列hole形状, 最后就是由小到大排列slot形状

数组的排列 Perl
[解决办法]
如果不是非要perl的话:

In [9]: ax=("6","9","100","98","70","51","68")   #这个数组代表的数目
   ...: 
   ...: bx=("300","400","250","3175","750","2050","1950") #这个数组代表的大小
   ...: 
   ...: cx=("hole","hole","slot","hole","slot","hole","slot",) #表示形状 hole表示孔,slot表示槽

In [10]: combined = zip(ax, bx, cx)

In [11]: combined.sort(key=lambda x: (x[1] != '3175', x[2], int(x[1])))

In [12]: combined
Out[12]: 
[('98', '3175', 'hole'),
 ('6', '300', 'hole'),
 ('9', '400', 'hole'),
 ('51', '2050', 'hole'),
 ('100', '250', 'slot'),
 ('70', '750', 'slot'),
 ('68', '1950', 'slot')]

In [13]: zip(combined)
Out[13]: 
[(('98', '3175', 'hole'),),
 (('6', '300', 'hole'),),
 (('9', '400', 'hole'),),
 (('51', '2050', 'hole'),),
 (('100', '250', 'slot'),),
 (('70', '750', 'slot'),),
 (('68', '1950', 'slot'),)]

In [14]: zip(*combined)
Out[14]: 
[('98', '6', '9', '51', '100', '70', '68'),
 ('3175', '300', '400', '2050', '250', '750', '1950'),
 ('hole', 'hole', 'hole', 'hole', 'slot', 'slot', 'slot')]

In [15]: 


[解决办法]

楼主, 刚写的这个应该符合你的需求。


#1/usr/bin/perl -w

use strict;
use Data::Dumper;

my @ax = qw( 6 9 100 98 70 51 68 );
my @bx = qw( 300 400 250 3175 750 2050 1950 );
my @cx = qw( hole hole slot hole slot hole slot );  #  test 1
#my @cx = qw( hole hole hole hole hole hole hole ); #  test 2

my @pre_sort;
my @sorted;
my $special;  #  it is used for storing anonymous array which include size [3175]

# Data processing
my %state; $state{$_}++ for @cx;  # flags which are used to judge whether 'hole' and 'slot' are co-existence or not
my @data = map{ [ $_, shift @ax, shift @bx ] } @cx; #  build basic data structure [shape, numb, size]

for my $shape ( @data ){
  if ( $shape->[2] == 3175 ){
       $special = $shape;  # store reference of anonymous array which include size [3175]
       next;
  }
  push @pre_sort, $shape;  # array for sort whose one element(include size [3175]) has been filterd
}

if (defined $state{'hole'} and not defined $state{'slot'} ){ 
  @sorted = 
    sort { 
     $a->[2]  <=>  $b->[2]; # sort 1
   } @pre_sort;

} elsif ( defined $state{'hole'} and  $state{'slot'} ){
  @sorted = 
    sort { 
      $a->[0] cmp $b->[0] or  # sort 2
      $a->[2] <=> $b->[2]     #  
    } @pre_sort;
}

unshift @sorted, $special; # package array with $special and @sorted( without element which include size[3175]


$Data::Dumper::Terse = 1;
print Dumper(@sorted);





# my @cx = qw( hole hole slot hole slot hole slot );     #test 1 
# test 1 输出:
[
  'hole',
  '98',
  3175
]
[
  'hole',
  '6',
  300
]
[
  'hole',
  '9',
  400
]
[
  'hole',
  '51',
  2050
]
[
  'slot',
  '100',
  250
]
[
  'slot',
  '70',
  750
]
[
  'slot',
  '68',
  1950
]




# my @cx = qw( hole hole hole hole hole hole hole ); #  test 2
# test 2 输出:
[
  'hole',
  '98',
  3175
]
[
  'hole',
  '100',
  250
]
[
  'hole',
  '6',
  300
]
[
  'hole',
  '9',
  400
]
[
  'hole',
  '70',
  750
]
[
  'hole',
  '68',
  1950
]
[
  'hole',
  '51',
  2050
]


btw, 这个程序给的的最大教训就是以后遇到要排序的东东万不可建复杂数据结构, 尤其是referenceof hash, ~ of array混在一起,复杂排序用数组最方便了。


[解决办法]
引用:
Quote: 引用:

如果不是非要perl的话:

In [9]: ax=("6","9","100","98","70","51","68")   #这个数组代表的数目
   ...: 
   ...: bx=("300","400","250","3175","750","2050","1950") #这个数组代表的大小
   ...: 
   ...: cx=("hole","hole","slot","hole","slot","hole","slot",) #表示形状 hole表示孔,slot表示槽

In [10]: combined = zip(ax, bx, cx)

In [11]: combined.sort(key=lambda x: (x[1] != '3175', x[2], int(x[1])))

In [12]: combined
Out[12]: 
[('98', '3175', 'hole'),
 ('6', '300', 'hole'),
 ('9', '400', 'hole'),
 ('51', '2050', 'hole'),
 ('100', '250', 'slot'),


 ('70', '750', 'slot'),
 ('68', '1950', 'slot')]

In [13]: zip(combined)
Out[13]: 
[(('98', '3175', 'hole'),),
 (('6', '300', 'hole'),),
 (('9', '400', 'hole'),),
 (('51', '2050', 'hole'),),
 (('100', '250', 'slot'),),
 (('70', '750', 'slot'),),
 (('68', '1950', 'slot'),)]

In [14]: zip(*combined)
Out[14]: 
[('98', '6', '9', '51', '100', '70', '68'),
 ('3175', '300', '400', '2050', '250', '750', '1950'),
 ('hole', 'hole', 'hole', 'hole', 'slot', 'slot', 'slot')]

In [15]: 



兄弟,以我对python浅薄的认知, 你这个是不是用了一些模块? 能说说么怎么实现的吗? 感觉好精炼的样子。 求分享啊!

歪楼了...

代码中用到的都是python内置的函数。

zip: 把ax,bx,cx相同位置的项放到一起。
sort:key参数可以支持非常灵活的排序。这儿用了一些不太健壮(要求稍微一变,代码就不能用了)的技巧:False<True,“hole”恰好小于“slot”。
zip(*combined): 把ax,bx,cx分开
[解决办法]
引用:
Quote: 引用:

Quote: 引用:

如果不是非要perl的话:

In [9]: ax=("6","9","100","98","70","51","68")   #这个数组代表的数目
   ...: 
   ...: bx=("300","400","250","3175","750","2050","1950") #这个数组代表的大小
   ...: 
   ...: cx=("hole","hole","slot","hole","slot","hole","slot",) #表示形状 hole表示孔,slot表示槽

In [10]: combined = zip(ax, bx, cx)

In [11]: combined.sort(key=lambda x: (x[1] != '3175', x[2], int(x[1])))

In [12]: combined
Out[12]: 
[('98', '3175', 'hole'),
 ('6', '300', 'hole'),
 ('9', '400', 'hole'),
 ('51', '2050', 'hole'),
 ('100', '250', 'slot'),
 ('70', '750', 'slot'),
 ('68', '1950', 'slot')]

In [13]: zip(combined)
Out[13]: 
[(('98', '3175', 'hole'),),
 (('6', '300', 'hole'),),
 (('9', '400', 'hole'),),
 (('51', '2050', 'hole'),),
 (('100', '250', 'slot'),),
 (('70', '750', 'slot'),),
 (('68', '1950', 'slot'),)]

In [14]: zip(*combined)
Out[14]: 
[('98', '6', '9', '51', '100', '70', '68'),
 ('3175', '300', '400', '2050', '250', '750', '1950'),


 ('hole', 'hole', 'hole', 'hole', 'slot', 'slot', 'slot')]

In [15]: 



兄弟,以我对python浅薄的认知, 你这个是不是用了一些模块? 能说说么怎么实现的吗? 感觉好精炼的样子。 求分享啊!

歪楼了...

代码中用到的都是python内置的函数。

zip: 把ax,bx,cx相同位置的项放到一起。
sort:key参数可以支持非常灵活的排序。这儿用了一些不太健壮(要求稍微一变,代码就不能用了)的技巧:False<True,“hole”恰好小于“slot”。
zip(*combined): 把ax,bx,cx分开
 不歪, 我在5楼已经给了楼主完整的code。

热点排行