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

  • Abe Olandres

    Abe Olandres

    16 EYLÜL 2006
  • LavcoPriceTech

    LavcoPriceTe

    21 AĞUSTOS 2010
  • SamsTech

    SamsTech

    4 NİSAN 2014