Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
-
Suwamoto
- Regular
- Posts: 66
- Joined: Wed Sep 05, 2012 7:36 am
-
Contact:
#1
Post
by Suwamoto » Tue Jun 25, 2013 7:34 pm
Hi >w< Let's say I got two different lists which look like this:
Code: Select all
list1 = [
("bird", 5),
("dog", 2)
]
list2 = [
("bird", 3, 5),
("lurch", 4, 4)
]
And I want to compare them and figure out if there are fields that got the same name. In this example, it would be "bird".
I have been doing that with a double for loop until now, which looks like this.
Code: Select all
for list1name, list1number in list1:
for list2name, list2number, list2amount in list2:
if list1name == list2name:
# Do something
So I was wondering if there are any more efficient ways to do this? >w<
Last edited by
Suwamoto on Wed Jun 26, 2013 3:46 pm, edited 1 time in total.
-
pwisaguacate
- Veteran
- Posts: 356
- Joined: Mon Mar 11, 2013 11:03 pm
-
Contact:
#2
Post
by pwisaguacate » Wed Jun 26, 2013 12:37 am
This is a way to check to see if
list1 and
list2 shares one or more same first values of each tuple:
Code: Select all
$ similar = [ a[0] for a, b in zip(list1, list2) if a[0] == b[0] ]
$ result = similar != []
The variable
similar becomes a list of shared first value. It is then checked to see if it's blank or not.
To be honest, I got help in a chat because I'm bad at lists. If anyone can write a function, that would be neat. (Apparently I can't into real functions either.)
Heck, you can turn the whole thing into a one-liner if you don't need the new list itself:
Code: Select all
$ similar = [ a[0] for a, b in zip(list1, list2) if a[0] == b[0] ] != []