list - Python: if element1 from list1 is equal to element2 from list2, change element1 -


This is part of the homework for my 'Basics of Programming' course in UNI. It says: < Blockquote>

enter a code (list, cod) function, where lst is a series of strings ccccyyyymmgg ( cccc < / Code> are random characters, yyyymmgg is a date) and cods is a series of 4-letter string. The function should work like this: If a CCCC in the CSCC is equal to the string in the CSS, the element with the CCCC is printed in [[CCCC, DD, MM, YAY] format; Otherwise it is printed in [[ccccyyyymmdd]] [[ccccyyyymmdd]]. For example, if "AcDa20140930" is an element of lst and , ACDA " is turned on cods , Then the list has been changed from "acDa20140930" to lst 4 elements "ACDA", 30, 9, 2014. . < / P>

I tried to solve it like this:

  Def code (list, cod): for code in ccsc: lst1 = [] in element Lst: cccc = lst [0: 3] yyyy = lst [4: 7] mm = lst [8: 9] dd = lst [10:11] If cccc = '' code: lst1 + cccc, dd, mm, yyyy return lst1 first = ['Kara 20130716', 'Tara 20080601', 'Pala19991231', 'Melo1960103'] seconds = [' Printed code (first, second)   

[code modified]: now output is [] any ideas?

Try it, it works.

Resolution # 1:

Demo [ Edit]

code:
  DIF codes (list, cod): toAdd = [] c = 0 for ind, calculation in AMM: if hand [ : 4] No codes: toAdd.append (elem + str (ind + c)) lst.remove (elem) c + = 1 for index, amm enumetre (lst): tmp = [] Tmp.append (elem [ : 4]) tmp.append (elem [10:]) tmp.append (elem [8:10]) tmp.append (elem [4] : 8]) lst.insert (ind, Tmp) lst.remove (elem) [ld] [lst.insert (int (elem [-1]), elem [: len (elem) - 1]) AMAD] return lst first = ['Cara20130716', 'Tara 20080601', 'Pala19991231', 'Melo1960103'] Second = ['Pala', 'Cara', 'Tara'] Print Code (First, Second)  < / pre> 

Output:
 ['Kara 20130716', ['Tara', '01', '06', '2008'], ['Pala', '31 ',' 12 Solution # 2:  

If you want the match to be insensitive.

on the demo

code:
  DIF codes (list, cod): for r ind, cods in cumer (cods): cods [ ind] = cod.lower () to add = [] c = 0 ind, amm enumerate (lst): if elem [: 4]. : toAdd.append (elem + str (ind + c)) lst.remove (elem) c + = 1 for index, amm enumerate (lst): tmp = [] tmp.append (elem [: 4]) tmp Endem (AMM [10:]) TMPAPEND (AMM [8:10]) TMPAPEND (AMM [4: 8]) lst.insert (IND, TMP) lst.remove (elem) [lst.insert (int (Elem [ 1]), elem [: AMAD] in lane (elem) - 1] Return lst first = ['Kara 20130716', 'Tara 20080601', 'Pal 1 99 631', 'Melo1960103'] Seconds = [' Output:  
  [['Kara', '16', 'CARA', 'Tara'] Print code '' 7 ',' 2013 '], [' Tara ',' 01 ',' 06 ',' 2008 '], [' Pala ',' 31 ',' 12 ',' 1996] ',' Melo 19601023 '] < / Code>   

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -