Maison > développement back-end > Tutoriel Python > Tâches du week-end - Liste

Tâches du week-end - Liste

Linda Hamilton
Libérer: 2024-12-24 00:37:13
original
1059 Les gens l'ont consulté

Tâche : 1
s = "a4k3b2"

1) Écrivez un programme pour obtenir le résultat 'abbbbklllbcc'

s = "a4k3b2"
output = ""
i = 0

while i < len(s):
    first = s[i]  
    second =s[i + 1] 
    if second.isdigit():
        alpha=chr(ord(first)+1)
        output=output+ first+ (int(second)*alpha)
        i+=2

print(output)
Copier après la connexion

Sortie :

abbbklllbcc

2) Écrivez un programme pour obtenir le résultat 'aaaaakkkkbbb'

s = "a4k3b2"
output = ""
i = 0

while i < len(s):
    first = s[i]  
    second =s[i + 1] 
    if second.isdigit():
        output=output+ first+ (int(second)*first)
        i+=2

print(output)
Copier après la connexion

Sortie :

aaaaakkkkbbb

Tâche : 2

matrice = [[10,20,30], [40,50,60], [70,80,90]]

Rejoignez la matrice donnée dans une liste unique en utilisant une boucle for complète et normale.
Méthode : 1 (Utilisation de la boucle for normale)

matrix = [[10,20,30], [40,50,60], [70,80,90]]
output=[]

for i in matrix:
    for j in i:
        output.append(j)
print(output)
Copier après la connexion

Méthode : 2 (Utilisation d'une boucle for complète)

matrix = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]

output = [j for i in matrix for j in i]
print(output)
Copier après la connexion

Sortie :

[10, 20, 30, 40, 50, 60, 70, 80, 90]
Copier après la connexion

Tâche : 3
l = ['ABC','DEF', 'GHI', 'JKL']
Obtenez la SORTIE : ['ABC', 'def', 'GHI', 'jkl']

l = ['ABC', 'DEF', 'GHI', 'JKL']

output = [] 
for i, alpha in enumerate(l):
    if i % 2 != 0:
        output.append(alpha.casefold())
    else:
        output.append(alpha)
print(output)
Copier après la connexion

Sortie :

['ABC', 'def', 'GHI', 'jkl']
Copier après la connexion

Transposer la matrice : la transposition d'une matrice est obtenue en changeant les lignes en colonnes et les colonnes en lignes.

Weekend Tasks - List

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal