Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F163443
test_python
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Authored By
aarwine
Jun 5 2014, 12:00 AM
2014-06-05 00:00:58 (UTC+0)
Size
835 B
Referenced Files
None
Subscribers
None
test_python
View Options
#!/usr/bin/python2
def commonInLists(listOfLists):
# Input: List of lists
# Output: Single list of common elements
# Must take unknown number of lists
if len(listOfLists) == 0:
raise Exception('No.')
elif len(listOfLists) == 1:
return listOfLists[0]
elif len(listOfLists) == 2:
return list(set(listOfLists[0]).intersection(listOfLists[1]))
elif len(listOfLists) > 2:
result = set(listOfLists[0]).intersection(listOfLists[1])
for i in range(2, len(listOfLists)):
result = result.intersection(listOfLists[i])
return result
else:
raise Exception('How did I get here?')
a = ['1', '2', '3', '4', '5']
b = ['2', '3', '4', '5', '6']
c = ['3', '4', '5', '6', '7']
d = ['4', '5', '6', '7', '8']
foo = [a, b, c, d]
print commonInLists(foo)
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
amazon-s3
Storage Format
Raw Data
Storage Handle
phabricator/og/ty/epbqpewspop4wlku
Default Alt Text
test_python (835 B)
Attached To
Mode
P1171 test python
Attached
Detach File
Event Timeline
Log In to Comment