print sum of duplicate numbers and product of non duplicate numbers from the list - list

I am new to python. I am trying to print sum of all duplicates nos and products of non-duplicates nos from the python list. for examples
list = [2,2,4,4,5,7,8,9,9]. what i want is sum= 2+2+4+4+9+9 and product=5*7*8.

There are pythonic one liners that can do this but here is an explicit way you might find easier to understand.
num_list = [2,2,4,4,5,7,8,9,9]
sum_dup = 0
product = 1
for n in num_list:
if num_list.count(n) == 1:
product *= n
else:
sum_dup += n
Also side note, don't call your list the name "list", it interferes with the builtin name of the list type.

count is useful for this. Sum is built in, but there is no built in "product", so using reduce is the easiest way to do this.
from functools import reduce
import operator
the_sum = sum([x for x in list if list.count(x)>1])
the_product = reduce(operator.mul, [x for x in lst if lst.count(x)==1])

Use a for loop to read a number from the list. create a variable and assign the number to it, read another number and compare them using an if statement. If they are the same sum them like sameNumSum+=sameNumSum else multiply them. Before for loop create these two variables and initialize them. I just gave you the algorithm to it, you can change it into your code. Hope that help though.

Related

How to repeat a function in Python (complete beginner - first lines of code ever)

I have the following code which I have to build upon (i.e. it can't be written a different way). I know there are other better ways of achieving the end result, but I want to use this code and then repeat it to make a list.
from random import choice
number_list = range(1,1001) # Creates a list from 1 to 1000
random_from_list = choice(number_list) # Chooses a random number from the list
I want to now repeat the choice function above 100 times, then print that list of 100 random numbers that I have chosen from my list of 1000 numbers. I have read up on "for" loops but I can't see how to apply it here.
If you don't need to build up your list you could just print them one at a time:
for _ in range(100):
print(choice(number_list))
If you want to build your list first you can use a "list comprehension":
choices = [choice(number_list) for _ in range(100)]
print(choices)
for i in range(100):
print(choice(number_list))

How to get 3 unique values using random.randint() in python?

I am trying to populate a list in Python3 with 3 random items being read from a file using REGEX, however i keep getting duplicate items in the list.
Here is an example.
import re
import random as rn
data = '/root/Desktop/Selenium[FILTERED].log'
with open(data, 'r') as inFile:
index = inFile.read()
URLS = re.findall(r'https://www\.\w{1,10}\.com/view\?i=\w{1,20}', index)
list_0 = []
for i in range(3):
list_0.append(URLS[rn.randint(1, 30)])
inFile.close()
for i in range(len(list_0)):
print(list_0[i])
What would be the cleanest way to prevent duplicate items being appended to the list?
(EDIT)
This is the code that i think has done the job quite well.
def random_sample(data):
r_e = ['https://www\.\w{1,10}\.com/view\?i=\w{1,20}', '..']
with open(data, 'r') as inFile:
urls = re.findall(r'%s' % r_e[0], inFile.read())
x = list(set(urls))
inFile.close()
return x
data = '/root/Desktop/[TEMP].log'
sample = random_sample(data)
for i in range(3):
print(sample[i])
Unordered collection with no duplicate entries.
Use the builtin random.sample.
random.sample(population, k)
Return a k length list of unique elements chosen from the population sequence or set.
Used for random sampling without replacement.
Addendum
After seeing your edit, it looks like you've made things much harder than they have to be. I've wired a list of URLS in the following, but the source doesn't matter. Selecting the (guaranteed unique) subset is essentially a one-liner with random.sample:
import random
# the following two lines are easily replaced
URLS = ['url1', 'url2', 'url3', 'url4', 'url5', 'url6', 'url7', 'url8']
SUBSET_SIZE = 3
# the following one-liner yields the randomized subset as a list
urlList = [URLS[i] for i in random.sample(range(len(URLS)), SUBSET_SIZE)]
print(urlList) # produces, e.g., => ['url7', 'url3', 'url4']
Note that by using len(URLS) and SUBSET_SIZE, the one-liner that does the work is not hardwired to the size of the set nor the desired subset size.
Addendum 2
If the original list of inputs contains duplicate values, the following slight modification will fix things for you:
URLS = list(set(URLS)) # this converts to a set for uniqueness, then back for indexing
urlList = [URLS[i] for i in random.sample(range(len(URLS)), SUBSET_SIZE)]
Or even better, because it doesn't need two conversions:
URLS = set(URLS)
urlList = [u for u in random.sample(URLS, SUBSET_SIZE)]
seen = set(list_0)
randValue = URLS[rn.randint(1, 30)]
# [...]
if randValue not in seen:
seen.add(randValue)
list_0.append(randValue)
Now you just need to check list_0 size is equal to 3 to stop the loop.

Applying regexp and finding the highest number in a list

I have got a list of different names. I have a script that prints out the names from the list.
req=urllib2.Request('http://some.api.com/')
req.add_header('AUTHORIZATION', 'Token token=hash')
response = urllib2.urlopen(req).read()
json_content = json.loads(response)
for name in json_content:
print name['name']
Output:
Thomas001
Thomas002
Alice001
Ben001
Thomas120
I need to find the max number that comes with the name Thomas. Is there a simple way to to apply regexp for all the elements that contain "Thomas" and then apply max(list) to them? The only way that I have came up with is to go through each element in the list, match regexp for Thomas, then strip the letters and put the remaining numbers to a new list, but this seems pretty bulky.
You don't need regular expressions, and you don't need sorting. As you said, max() is fine. To be safe in case the list contains names like "Thomasson123", you can use:
names = ((x['name'][:6], x['name'][6:]) for x in json_content)
max(int(b) for a, b in names if a == 'Thomas' and b.isdigit())
The first assignment creates a generator expression, so there will be only one pass over the sequence to find the maximum.
You don't need to go for regex. Just store the results in a list and then apply sorted function on that.
>>> l = ['Thomas001',
'homas002',
'Alice001',
'Ben001',
'Thomas120']
>>> [i for i in sorted(l) if i.startswith('Thomas')][-1]
'Thomas120'

sorting elements in the list in python

suppose the list is like this
l = [("Texas","city1"), ("Texas","city2"), ("Texas","city3"), ("Texas","city4"), ("Texas","city5"),
("Georgia","city6"), ("Georgia","city9"), ("Georgia","city10"),
("Alabama","city7"), ("Alabama","city8")]
This list will have a unique state names.
Now what i need two things to be done with this list.
1) sorting based on states first and cities second. say after sorting based on states first and cities second the list looks like this
l = [("Georgia","city6"),("Georgia","city9"),("Georgia","city10"),("Texas","city1"), ("Texas","city2"), ("Texas","city3"), ("Texas","city4"), ("Texas","city5"),("Alabama","city7"), ("Alabama","city8")]
2) after this step i need the states to be alphabetized with their corresponding cities. Say i need like below format finally.
l = [("Alabama","city7"), ("Alabama","city8"),("Georgia","city6"),("Georgia","city9"),("Georgia","city10"),("Texas","city1"), ("Texas","city2"), ("Texas","city3"), ("Texas","city4"), ("Texas","city5")]
sorted(lsNearCities, key=operator.itemgetter(0,1)) --> I assume this would sort based on states first and then cities second. And after that how should i accomplish the list which i needed using python. Please help would be appreciated.
How about this:
l = [("Texas","city1"), ("Texas","city2"), ("Texas","city3"), ("Texas","city4"), ("Texas","city5"),("Georgia","city6"), ("Georgia","city9"), ("Georgia","city10"),("Alabama","city7"), ("Alabama","city8")]
def cmp(x,y):
if x[0]<y[0]:
return -1
elif x[0]>y[0]:
return 1
elif x[1]<y[1]:
return -1
elif x[1]>y[1]:
return 1
else:
return 0
l.sort(cmp)
print l
By the way, this is the default sorting so if you don't want to do a different sort then you can just simply do:
l.sort()
You can change the cmp function for different sort modes. I'm not sure what you mean by "alphabetizing"?

XSB prolog: Problems with lists

I'm new to XSB prolog and I'm trying to solve this problem.
I've got prices of products and some orders. It looks like this:
price(cola,3).
price(juice,1).
price(sprite,4).
// product for ex. is cola with a price of 3 (something, it doesn't matter which currency)
order(1, [cola,cola,sprite]).
order(2, [sprite,sprite,juice]).
order(3, [juice,cola]). // the number here is the number of the order
// and the list represents the products that
// belong to that order
Now, my task is to write a new function called bill/2. This function should take the number of the order and then sum up all the prices for the products in the same order(list).
Something like:
|?-bill(1,R).
R= 10 ==> ((cola)3 + (cola)3 + (sprite)4 = 10)
|?-bill(2,R).
R= 9 ==> ((sprite)4 + (sprite4 + (juice)1 = 9)
and so on... I know how to get to the number of the order but I don't know how to get each product from the list inside that order to get to it's price, so I can sum it up.
Thanks in advance.
In plain Prolog, first get all numbers in a list, then sum the list:
bill(Ord, Tot) :-
order(Ord, Items),
findall(Price, (member(I, Items), price(I, Price)), Prices),
sum_list(Prices, Tot).
but since XSB has tabling available, there could be a better way, using some aggregation function.