SORU
30 Temmuz 2010, Cuma


İnt() çağrısı her listede Python eleman fonksiyon

Sayısal bir dize içeren bir liste, çok beğendim

numbers = ['1', '5', '10', '8'];

Bu nedenle de her liste öğesi tamsayı dönüştürmek istiyorum:

numbers = [1, 5, 10, 8];

Bu bir döngü kullanarak, şöyle yapabilirim:

new_numbers = [];
for n in numbers:
    new_numbers.append(int(n));
numbers = new_numbers;

Bu kadar çirkin olmak zorunda mı? Kod tek bir satır içinde bunu yapmak için daha pythonic bir yolu yoktur eminim. Lütfen bana yardım edin.

CEVAP
30 Temmuz 2010, Cuma


Bu kapsam için:

numbers = [ int(x) for x in numbers ]

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Call Me Howard

    Call Me Howa

    18 AĞUSTOS 2012
  • RayperEnglishKnight

    RayperEnglis

    24 Kasım 2008
  • SunsetTrance

    SunsetTrance

    20 EYLÜL 2008