簡體   English   中英

如何將列表中的項目分配給變量

[英]How to assign item from list to variable

我有以下 output:

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

如果您使用的是 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))
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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM