Как присвоить элемент из списка переменной - PullRequest
1 голос
/ 18 июня 2020

У меня есть следующий результат:

datos=['Venta Casas CARRETERA NACIONAL, Nuevo León', 'Publicado el 29 de Abr', 'ABEDUL DE LADERAS', '3 Recámaras', '4.5 Baños', '300m² de Construcción', '300m² de Terreno', '2 Plantas', ' 81-1255-3166', ' Preguntale al vendedor', 'http://zuhausebienesraices.nocnok.com/', "INFOSTATS_ADOAnuncios('5', '30559440');"] 

И я хотел бы назначить другую переменную для каждого элемента, если он есть в списке, иначе это будет 0. Например:

recamara= the string from the list that has the word "Recámara"
bano= the string from the list that has the string "Baño"

и так далее. И если слова "Baño" нет в списке, то bano= 0

Ответы [ 2 ]

1 голос
/ 18 июня 2020

Если вы используете Python, вы можете использовать для этого понимание списка.

datos = ['Venta Casas CARRETERA NACIONAL, Nuevo León', 'Publicado el 29 de Abr', 'ABEDUL DE LADERAS', '3 Recámaras', '4.5 Baños', '300m² de Construcción', '300m² de Terreno', '2 Plantas', ' 81-1255-3166', ' Preguntale al vendedor', 'http://zuhausebienesraices.nocnok.com/', "INFOSTATS_ADOAnuncios('5', '30559440');"]

# list of strings which has "Casas" in it
casas_list = [string for string in datos if "Casas" in string]

print(casas_list)

print(len(casas_list))
0 голосов
/ 18 июня 2020
recamara = [s for s in datos if "Recámara" in s]
bano = [s for s in datos if "Baño" in s]

if len(recamara)==0:
    recamara = 0
else:
    print(recamara[0]) #print the entire list if there will be more than 1 string

if len(bano)==0:
    bano = 0
else:
    print(bano[0])
...