Professional IronPython



Download 20,35 Mb.
Pdf ko'rish
bet87/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   83   84   85   86   87   88   89   90   ...   453
68

CHAPTER 4
Using ArrAys And ColleCtions
Now that you know about using 
range()
and 
len()
together, let’s look at an example of ragged 
array processing. The following code shows how to process a ragged array.
# Create a ragged array.
MyList = [[1, 2], 3, 4, [5, 6]]
# Display the array content. In this case, you must place the ragged
# array portion of the example in a try block so that the code 
# displays just the first dimension when only one dimension is 
# available. 
print(‘\nDisplaying the ragged array.’) 
print(‘X Y Value’) 
for x in range(len(MyList)):
try:
for y in range(len(MyList[x])):
print x, y, ‘ ‘, MyList[x][y]
except TypeError:
print x, ‘N/A’, MyList[x]
In this case, you begin with a ragged array that contains the numbers 1 through 6. In order to process 
this kind of array, you begin with the first array dimension, which will always have either a sub-array 
or a value. In this case, there are four array elements — two sub-arrays and two values (3 and 4).
Of course, you want the values in those sub-arrays. Consequently, the next step is to place the sub-
array processing in a 
try...except
block because the processing will fail when the code encounters 
a value. The moment the code tries to get the length of the sub-array using 
len()
, it will fail with a 
TypeError
. When the error does occur, the example prints just the first dimension. You can use this 
pattern for any ragged array you need to process. Figure 4-8 shows the output from this example.
FIguRE 4-8: 
Ragged arrays are relatively easy to process using range() and len().
When working with IronPython, there are typically multiple ways to perform the same task. 
Many developers (with good reason) won’t process data by exception. Fortunately, there’s another 
way to process a ragged array that doesn’t involve the 
try...except
block shown earlier. Here’s 
the second method.
# Create a ragged array.
MyList = [[1, 2], 3, 4, [5, 6]]
# Display the array content.
548592c04.indd 68
2/24/10 12:47:36 PM
www.finebook.ir



Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   83   84   85   86   87   88   89   90   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish