Initial commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
def common_data(list1, list2):
|
||||
result = False
|
||||
for x in list1:
|
||||
for y in list2:
|
||||
if x == y:
|
||||
result = True
|
||||
return result
|
||||
print(common_data([1,2,3,4,5], [5,6,7,8,9]))
|
||||
print(common_data([1,2,3,4,5], [6,7,8,9]))
|
||||
3
python/List/Python program access the index of a list.py
Normal file
3
python/List/Python program access the index of a list.py
Normal file
@@ -0,0 +1,3 @@
|
||||
nums = [5, 15, 35, 8, 98]
|
||||
for num_index, num_val in enumerate(nums):
|
||||
print(num_index, num_val)
|
||||
@@ -0,0 +1,4 @@
|
||||
list1 = [1, 2, 3, 0]
|
||||
list2 = ['Red', 'Green', 'Black']
|
||||
final_list = list1 + list2
|
||||
print(final_list)
|
||||
@@ -0,0 +1,26 @@
|
||||
def is_Sublist(l, s):
|
||||
sub_set = False
|
||||
if s == []:
|
||||
sub_set = True
|
||||
elif s == l:
|
||||
sub_set = True
|
||||
elif len(s) > len(l):
|
||||
sub_set = False
|
||||
|
||||
else:
|
||||
for i in range(len(l)):
|
||||
if l[i] == s[0]:
|
||||
n = 1
|
||||
while (n < len(s)) and (l[i+n] == s[n]):
|
||||
n += 1
|
||||
|
||||
if n == len(s):
|
||||
sub_set = True
|
||||
|
||||
return sub_set
|
||||
|
||||
a = [2,4,3,5,7]
|
||||
b = [4,3]
|
||||
c = [3,7]
|
||||
print(is_Sublist(a, b))
|
||||
print(is_Sublist(a, c))
|
||||
@@ -0,0 +1,8 @@
|
||||
list1 = [10, 10, 0, 0, 10]
|
||||
list2 = [10, 10, 10, 0, 0]
|
||||
list3 = [1, 10, 10, 0, 0]
|
||||
|
||||
print('Compare list1 and list2')
|
||||
print(' '.join(map(str, list2)) in ' '.join(map(str, list1 * 2)))
|
||||
print('Compare list1 and list3')
|
||||
print(' '.join(map(str, list3)) in ' '.join(map(str, list1 * 2)))
|
||||
@@ -0,0 +1,12 @@
|
||||
def count_range_in_list(li, min, max):
|
||||
ctr = 0
|
||||
for x in li:
|
||||
if min <= x <= max:
|
||||
ctr += 1
|
||||
return ctr
|
||||
|
||||
list1 = [10,20,30,40,40,40,70,80,99]
|
||||
print(count_range_in_list(list1, 40, 100))
|
||||
|
||||
list2 = ['a','b','c','d','e','f']
|
||||
print(count_range_in_list(list2, 'a', 'e'))
|
||||
@@ -0,0 +1,9 @@
|
||||
def match_words(words):
|
||||
ctr = 0
|
||||
|
||||
for word in words:
|
||||
if len(word) > 1 and word[0] == word[-1]:
|
||||
ctr += 1
|
||||
return ctr
|
||||
|
||||
print(match_words(['abc', 'xyz', 'aba', '1221']))
|
||||
@@ -0,0 +1,8 @@
|
||||
def long_words(n, str):
|
||||
word_len = []
|
||||
txt = str.split(" ")
|
||||
for x in txt:
|
||||
if len(x) > n:
|
||||
word_len.append(x)
|
||||
return word_len
|
||||
print(long_words(3, "The quick brown fox jumps over the lazy dog"))
|
||||
@@ -0,0 +1,13 @@
|
||||
def second_largest(numbers):
|
||||
count = 0
|
||||
n1 = n2 = float('-inf')
|
||||
for x in numbers:
|
||||
count += 1
|
||||
if x > n2:
|
||||
if x >= n1:
|
||||
n1, n2 = x, n1
|
||||
else:
|
||||
n2 = x
|
||||
return n2 if count >= 2 else None
|
||||
|
||||
print(second_largest([1, 2, -8, -2, 0]))
|
||||
@@ -0,0 +1,9 @@
|
||||
def second_smallest(numbers):
|
||||
a1, a2 = float('inf'), float('inf')
|
||||
for x in numbers:
|
||||
if x <= a1:
|
||||
a1, a2 = x, a1
|
||||
elif x < a2:
|
||||
a2 = x
|
||||
return a2
|
||||
print(second_smallest([1, 2, -8, -2, 0]))
|
||||
4
python/List/Python program to flatten a shallow list.py
Normal file
4
python/List/Python program to flatten a shallow list.py
Normal file
@@ -0,0 +1,4 @@
|
||||
import itertools
|
||||
original_list = [[2,4,3],[1,5,6], [9], [7,9,0]]
|
||||
new_merged_list = list(itertools.chain(*original_list))
|
||||
print(new_merged_list)
|
||||
@@ -0,0 +1,15 @@
|
||||
def sub_lists(my_list):
|
||||
subs = [[]]
|
||||
for i in range(len(my_list)):
|
||||
n = i+1
|
||||
while n <= len(my_list):
|
||||
sub = my_list[i:n]
|
||||
subs.append(sub)
|
||||
n += 1
|
||||
|
||||
return subs
|
||||
|
||||
l1 = [10, 20, 30, 40]
|
||||
l2 = ['X', 'Y', 'Z']
|
||||
print(sub_lists(l1))
|
||||
print(sub_lists(l2))
|
||||
@@ -0,0 +1,7 @@
|
||||
def printValues():
|
||||
l = list()
|
||||
for i in range(1,21):
|
||||
l.append(i**2)
|
||||
print(l[5:])
|
||||
|
||||
printValues()
|
||||
@@ -0,0 +1,8 @@
|
||||
def printValues():
|
||||
l = list()
|
||||
for i in range(1,21):
|
||||
l.append(i**2)
|
||||
print(l[:5])
|
||||
print(l[-5:])
|
||||
|
||||
printValues()
|
||||
@@ -0,0 +1,6 @@
|
||||
def last(n): return n[-1]
|
||||
|
||||
def sort_list_last(tuples):
|
||||
return sorted(tuples, key=last)
|
||||
|
||||
print(sort_list_last([(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]))
|
||||
@@ -0,0 +1,5 @@
|
||||
import collections
|
||||
my_list = [10,10,10,10,20,20,20,20,40,40,50,50,30]
|
||||
print("Original List : ",my_list)
|
||||
ctr = collections.Counter(my_list)
|
||||
print("Frequency of the elements in the List : ",ctr)
|
||||
@@ -0,0 +1,7 @@
|
||||
def max_num_in_list( list ):
|
||||
max = list[ 0 ]
|
||||
for a in list:
|
||||
if a > max:
|
||||
max = a
|
||||
return max
|
||||
print(max_num_in_list([1, 2, -8, 0]))
|
||||
@@ -0,0 +1,7 @@
|
||||
def smallest_num_in_list( list ):
|
||||
min = list[ 0 ]
|
||||
for a in list:
|
||||
if a < min:
|
||||
min = a
|
||||
return min
|
||||
print(smallest_num_in_list([1, 2, -8, 0]))
|
||||
@@ -0,0 +1,5 @@
|
||||
my_list = [10, 20, 30, 40, 20, 50, 60, 40]
|
||||
print("Original List : ",my_list)
|
||||
my_set = set(my_list)
|
||||
my_new_list = list(my_set)
|
||||
print("List of unique numbers : ",my_new_list)
|
||||
@@ -0,0 +1,6 @@
|
||||
def multiply_list(items):
|
||||
tot = 1
|
||||
for x in items:
|
||||
tot *= x
|
||||
return tot
|
||||
print(multiply_list([1,2,-8]))
|
||||
@@ -0,0 +1,3 @@
|
||||
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
|
||||
color = [x for (i,x) in enumerate(color) if i not in (0,4,5)]
|
||||
print(color)
|
||||
@@ -0,0 +1,10 @@
|
||||
a = [10,20,30,20,10,50,60,40,80,50,40]
|
||||
|
||||
dup_items = set()
|
||||
uniq_items = []
|
||||
for x in a:
|
||||
if x not in dup_items:
|
||||
uniq_items.append(x)
|
||||
dup_items.add(x)
|
||||
|
||||
print(dup_items)
|
||||
@@ -0,0 +1,4 @@
|
||||
from random import shuffle
|
||||
color = ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
|
||||
shuffle(color)
|
||||
print(color)
|
||||
@@ -0,0 +1,6 @@
|
||||
def sum_list(items):
|
||||
sum_numbers = 0
|
||||
for x in items:
|
||||
sum_numbers += x
|
||||
return sum_numbers
|
||||
print(sum_list([1,2,-8]))
|
||||
@@ -0,0 +1,10 @@
|
||||
def prime_eratosthenes(n):
|
||||
prime_list = []
|
||||
for i in range(2, n+1):
|
||||
if i not in prime_list:
|
||||
print (i)
|
||||
for j in range(i*i, n+1, i):
|
||||
prime_list.append(j)
|
||||
|
||||
print(prime_eratosthenes(100))';
|
||||
include("../../new_editor/editor_python.php")
|
||||
Reference in New Issue
Block a user