Un résumé des opérations typiques de Ruby Array et de la liste Python.
Ceci est un article pour Rubyist (Pythonista) qui vient de commencer à apprendre Python (Ruby).
Référence: Cliquez ici pour la version C ++ Résumé de la prise en charge des opérations sur les tableaux Ruby-C ++
size
[1,2,3].size #=> 3
len( [1,2,3] ) #=> 3
each
Boucle autour de l'élément
[1,2,3,4,5].each do |x|
puts 2*x #=> 2,4,6,8,10
end
for x in [1,2,3,4,5]:
print(2*x)
each_with_index
Tournez la boucle avec index
[5,4,3,2,1].each_with_index do |x,idx|
print "#{idx}:#{x}, "
end
# => 0:5, 1:4, 2:3, 3:2, 4:1,
for idx,x in enumerate( [5,4,3,2,1] ):
print("{0}:{1}, ".format(idx,x), end="")
Joindre des séquences
concat
est une opération destructive, `+ ʻest une opération non destructive[1,2,3] + [4,5] # [1,2,3,4,5]
est une opération destructive. Fonctionnement non destructif avec
+`[1,2,3] + [4,5]
push (<<)
Ajouter un élément à la fin
a = [1,2]
a << 3
# a => [1,2,3]
a = [1,2]
a.append(3)
# a => [1,2,3]
map
[1,2,3].map {|x| 2*x } #=> [2,4,6]
map
, mais la notation d'inclusion de liste semble être moderne.[ 2*x for x in [1,2,3] ] # => [2,4,6]
list( map( lambda x: 2*x, [1,2,3] ) ) # => [2,4,6]
find
[7,8,9,10,11].find {|x| x%5 == 0 } #=> 10
[ x for x in [7,8,9,10,11] if x % 5 == 0 ][0] #=> 10
--Ajout: http://stackoverflow.com/questions/9868653/find-first-list-item-that-matches-criteria
next((x for x in [7,8,9,10,11] if x % 5 == 0), None)
find_all
[1,2,3,4,5].find_all {|x| x>3 } #=> [4,5]
[ x for x in [1,2,3,4,5] if x > 3] #=> [4,5]
list( filter( lambda x: x>3, [1,2,3,4,5] ) ) #=> [4,5]
sort
[4,1,3,2,5].sort #=> [1,2,3,4,5]
sorted( [4,1,3,2,5] ) #=> [1,2,3,4,5]
sort_by
[4,1,2,3,5].sort_by {|x| (3-x).abs } #=> [3,4,2,1,5]
sorted( [4,1,3,2,5], key= lambda x: abs(3-x) )
index
[5,1,4,2,3].index(2) #=> 3
[5,1,4,2,3].index(2) #=> 3
all?
[2,4,6,8].all? {|x| x % 2 == 0 } #=> true
[2,4,6,9].all? {|x| x % 2 == 0 } #=> false
all( x%2==0 for x in [2,4,6,8] ) #=> True
all( x%2==0 for x in [2,4,6,9] ) #=> False
sum
[1,3,5].inject(:+) #=> 9
sum( [1,3,5] )
inject (reduce)
[1,2,3,4,5].inject(1) {|acc,x| acc*x } #=> 120
[1,2,3,4,5].inject("") {|acc,x| acc + (x%2==0?'e':'o') } #=> oeoeo
functools
depuis Python3.import functools
functools.reduce( lambda acc,x: acc*x, [1,2,3,4,5], 1 )
functools.reduce( lambda acc,x: acc+("e" if x%2==0 else "o") , [1,2,3,4,5], "" )
uniq
[5,1,1,2,3,5,5].uniq #=> [5,1,2,3]
list( set( [5,1,1,2,3,5,5] ) )
li_uniq = []
for x in [5,1,1,2,3,5,5]:
if x not in li_uniq:
li_uniq.append(x)
li_uniq #=> [5,1,2,3]
uniq {block}
[4,5,1,2,3,4,5].uniq {|x| x % 3 } #=> [4,5,3]
li_uniq = []
set_uniq = set()
for x in [4,5,1,2,3,4,5]:
if x % 3 not in set_uniq:
li_uniq.append(x)
set_uniq.add(x%3)
li_uniq #=> [4,5,3]
join
[1,5,3,2].join(":") #=> "1:5:3:2"
join
. Le tableau doit être explicitement converti en type chaîne.":".join(map(str, [1,5,3,2]))
reverse
[1,2,3].reverse #=> [3,2,1]
a = [1,2,3]
a.reverse!
a #=> [3,2,1]
list( reversed([1,2,3]) )
a = [1,2,3]
a.reverse()
a #=> [3,2,1]
group_by
["cat","bat","bear","camel","alpaca"].group_by {|x| x[0]}
# {"c"=>["cat", "camel"], "b"=>["bat", "bear"], "a"=>["alpaca"]}
d = {}
for x in ["cat","bat","bear","camel","alpaca"]:
key = x[0]
if key not in d:
d[key] = []
d[key].append(x)
d # => {'c': ['cat', 'camel'], 'a': ['alpaca'], 'b': ['bat', 'bear']}
shuffle
[1,2,3,4,5,6,7].shuffle #=> [2, 6, 4, 7, 5, 3, 1]
import random
a = [1,2,3,4,5,6,7]
random.shuffle(a)
a #=> [4, 5, 6, 2, 7, 1, 3]
Recommended Posts