• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Problems with lists  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello. This is my code. It is kind of hard to explain, because this is just a segment of the program. The basic principle is that i have this list cijela_lista_bez_duplikata that has 2003 elements in it. I am using regex to sort the elements.  The problem is that once i put "cijela_lista_bez_duplikata.remove(element)" on the end of each sorting the result changes. Which is very strange because regexis shouldn't overlap (There is a tiny overlap, but nothing signifacnt). I am "losing" 200 elements in 5a.
I am not sure if you guys understand what I am trying to do.
For reference, the strings in cijela_lista_bez_duplikata look like this: ('mislim', 'misliti'), ('znam', 'znati'), ('imam', 'imati'), ('vidim', 'vidjeti'), ('moram', 'morati'), ('mogu', 'moći'), ('želim', 'željeti')...

I didn't translate the code, because I would probably mess up everything.
 
Sheriff
Posts: 13456
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code practically unreadable. Why use a name like mtch instead of matches or even just match? Is it really so hard to type those extra letters? The difference in the readability of code when you use actual words versus unreadable contractions is HUGE. Please do yourself and others a favor and spend a few minutes to spell that out at least.

And while you're at it, spell out "vrsta" as well. Better yet, translate it to an English word for the folks here.
 
Junilu Lacar
Sheriff
Posts: 13456
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This formulation seems suspicious to me:

The result of calling match() on a regular expression is a match object, which is always true, or None. You use the and
Boolean operator though. It seems to me that if both calls to match() succeed, then the mtch variable will be a plain Boolean value of true rather than a match object. I could be wrong though. If it is indeed just a Boolean true value, what happens when you get to mtch.group()?

Edit: Ok, I'll answer my own question. Apparently, the expression r_inf_5a.match(element[1]) and r_pz_5a.match(element[0]) evaluates to the match object returned by the second term, r_pz_5a.match(element[0]), if the first term matches.

So as I understand it now, the intent of that code is to replace the pair with just the element[0] of the tuple/pair when both elements match their respective regexs.
 
Junilu Lacar
Sheriff
Posts: 13456
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your problem is that you're removing elements from the list while you are still iterating over it.

See this thread in SO about using a list comprehension instead: https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating
 
M. Gumblert
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was in a rush and completely forgot to answer you. I managed to correct my program using list comprehension. Is´s much more easier and is working perfectly fine now. I am not sure why you said my code is unreanable. As I said I didn´t want to rename the variables because I was afraid I might break the code. Anyway, thank you.
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!