Example
Order_ID Name
1 Man
2 Boss
5 Don
7 Lil
9 Dom
10 Bob
Want to get an output as:
3 4 6 8 are the missing Order_ID
Try using a list comprehension with range:
print([i for i in range(1, 10) if i not in df['Order_ID']])
Output:
[3, 4, 6, 8]
Solution for generate missing values from index dynamically by maximum and minimum values:
print (np.setdiff1d(np.arange(df.index.min(), df.index.max() + 1), df.index).tolist())
[3, 4, 6, 8]
Convert the list to set and compute its difference with a set that contains integers ranging from min(lst) and max(lst).
lst=df["Order_ID"].to_list()
sorted(set(range(lst[0], lst[-1])) - set(lst))
> [3, 4, 6, 8]
Try this code;
Code Syntax
missData = list(filter(lambda x: x not in df['Order_ID'], range(1, df['Order_ID].max()+1)))
print(f"{missData} are the missing Order_ID")
Output
[3, 4, 6, 8] are the missing Order_ID
[Program finished]
Related
For example, let's say that I have a range of "10-1," how can I express this to say [1 2 3 4 5 6 7 8 9 10]?
You can use split to read the start and end, use int to convert them into integers, and then use range to return a desired list.
def range_to_list(s):
end, start = map(int, s.split('-'))
return list(range(start, end + 1))
print(range_to_list('10-1')) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Depending on your goal, list might not be necessary.
I am trying to print text with an index and its value. This is the code
test_list = [1, 4, 5, 6, 7]
for index, value in enumerate(test_list):
print("S1_vec('index')<=", value)
Getting output
S1_vec('index')<= 1
S1_vec('index')<= 4
S1_vec('index')<= 5
S1_vec('index')<= 6
S1_vec('index')<= 7
But I want my output to print an index value
S1_vec('0')<= 1
S1_vec('1')<= 4
S1_vec('2')<= 5
S1_vec('3')<= 6
S1_vec('4')<= 7
Can anyone please help me to correct this issue? Thanks
Currently, you are passing index as literal string which is incorrect.
Use f-strings for python3.6+:
test_list = [1, 4, 5, 6, 7]
for index, value in enumerate(test_list):
print(f"S1_vec('{index}')<= {value}")
or format() for lower versions:
test_list = [1, 4, 5, 6, 7]
for index, value in enumerate(test_list):
print("S1_vec('{}')<= {}".format(index, value))
If you're using a sufficiently recent Python, f-strings are probably easiest:
print(f"S1_vec('{index}') <= {value}")
I'm trying to write a code in Python to create a fractional ranking list for a given one.
The fraction ranking is basically the following:
We have a list of numbers x = [4,4,10,4,10,2,4,1,1,2]
First, we need to sort the list in ascending order. I will use insertion sort for it, I already coded this part.
Now we have the sorted list x = [1, 1, 2, 2, 4, 4, 4, 4, 10, 10]. The list has 10 elements and we need to compare it with a list of the first 10 natural numbers n = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
For each element in x we assign a value. Notice the number 1 appears in positions 1 and 2. So, the number 1 receives the rank (1 + 2) / 2 = 1.5.
The number 2 appears in positions 3 and 4, so it receives the rank (3 + 4) / 2 = 3.5.
The number 4 appears in positions 5, 6, 7 and 8, so it receives the rank (5 + 6 + 7 + 8) / 4 = 6.5
The number 10 appears in positions 9 and 10, so it receives the rank (9 + 10) / 2 = 9.5
In the end of this process we need to have a new list of ranks r = [1.5, 1.5, 3.5, 3.5, 6.5, 6.5, 6.5, 6.5, 9.5, 9.5]
I don't want an entire solution, I want some tips to guide me while writing down the code.
I'm trying to use the for function to make a new list using the elements in the original one, but my first attempt failed so bad. I tried to get at least the first elements right, but it didn't work as expected:
# Suppose the list is already sorted.
def ranking(x):
l = len(x)
for ele in range(1, l):
t = x[ele-1]
m = x.count(t)
i = 0
sum = 0
while i < m: # my intention was to get right at least the rank of the first item of the list
sum = sum + 1
i = i + 1
x[ele] = sum/t
return x
Any ideais about how could I solve this problem?
Ok, first, for your for loop there you can more easily loop through each element in the list by just saying for i in x:. At least for me, that would make it a little easier to read. Then, to get the rank, maybe loop through again with a nested for loop and check if it equals whatever element you're currently on. I don't know if that makes sense; I didn't want to provide too many details because you said you didn't want the full solution (definitely reply if you want me to explain better).
Here is an idea:
You can use x.count(1) to see how many number 1s you have in list, x.count(2) for number 2 etc.
Also, never use sum as a variable name since it is an inbuilt function.
Maybe use 2 for loops. First one will go through elements in list x, second one will also go through elements in list x, and if it finds the same element, appends it to new_list.
You can then use something like sum(new_list) and clear list after each iteration.
You don't even need to loop through list n if you use indexing while looping through x
for i, y in enumerate(x) so you could use n[i] to read the value
If you want the code I'll post it in the comment
#VictorPaesPlinio- would you try this sample code for the problem: (it's a partial solution, did the data aggregation work, and leave the last part put the output for your own exercise).
from collections import defaultdict
x = [4, 4, 10, 4, 10, 2, 4, 1, 1, 2]
x.sort()
print(x)
lst = list(range(1, len(x)+1))
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
ranking = defaultdict(list)
for idx, num in enumerate(x, 1):
print(idx, num)
ranking[num].append(idx)
print(ranking)
'''defaultdict(<class 'list'>, {1: [1, 2], 2: [3, 4],
4: [5, 6, 7, 8], 10: [9, 10]})
'''
r = []
# r = [1.5, 1.5, 3.5, 3.5, 6.5, 6.5, 6.5, 6.5, 9.5, 9.5]
# 1 1 2 2 4 4 4 4 10 10
for key, values in ranking.items():
# key is the number, values in the list()
print(key, values, sum(values))
Outputs:
1 [1, 2] 3
2 [3, 4] 7
4 [5, 6, 7, 8] 26
10 [9, 10] 19 # then you can do the final outputs part...
I'm trying to write code to firstly, order numbers from lowest to highest (e.g. 1, 3, 2, 4, 5 to 1, 2, 3, 4, 5). Secondly, I would like to incrementally add the numbers in the list.
eg.
1
3
6
10
15
I've already tried using the sum function, then the sorted function, but I was wondering if I can write them neatly in a code to just get everything worked out.
Addition = [1, 13, 166, 3, 80, 6, 40]
print(sorted(Addition))
I was able to get the numbers sorted horizontally, but I wasn't able to get the numbers added vertically.
Apparently, you need a cumulative addition. You can code a simple one using a simple loop and yield the results on the go
def cumulative_add(array):
total = 0
for item in array:
total += item
yield total
>>> list(cumulative_add([1,2,3,4,5]))
[1, 3, 6, 10, 15]
Depending on your goals, you may also wish to use a library, such as pandas, that has cumulative sum already written for you.
For example,
>>> s = pd.Series([1,2,3,4,5])
>>> s.cumsum()
0 1
1 3
2 6
3 10
4 15
You can use itertools.accumulate with sorted:
import itertools
mylist = [1, 2, 3, 4, 5]
result = list(itertools.accumulate(sorted(mylist)))
# result: [1, 3, 6, 10, 15]
The default action is operator.add, but you can customize it. For example, you can do running product instead of running sum if you needed it:
import itertools
import operator
mylist = [1, 2, 3, 4, 5]
result = list(itertools.accumulate(sorted(mylist), operator.mul))
# result: [1, 2, 6, 24, 120]
This sounds very simple but is it possible to split an integer like lets say 8 to an array like this [0,1,2,3,4,5,6,7,8]
i have already tried the code below
proxies= 'a,b,c,d,e,f,g,h'#list of objects to attach the expression to
objlist = proxies.split(",")#splits every word as an object wherever theres a comma ,
ofset = (len(objlist))
ofset comes out as 8. but i want ofset to be an array of [0,1,2,3,4,5,6,7,8].
>>> list(range(8+1))
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Don't forget the +1.
change :
ofset = (len(objlist))
to :
ofset = range(len(objlist)+1)
Simply use range
>>> range(len(objlist)+1)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
try like this, you will have index and value side by side:
>>> proxies= 'a,b,c,d,e,f,g,h'
>>> for i,x in enumerate(proxies.split(',')):
... print i,x
...
0 a
1 b
2 c
3 d
4 e
5 f
6 g
7 h
if you want only range
>>>range(0,len(proxies.split(','))+1)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Another cool way. You don't have to use the split() function.
proxies= set('abcdefgh')# the set() splits the characters into set proxies
objlist= list(proxies)# proxies is converted into a list
ofset = list(range(len(objlist)+1))