Related
I'm doing some beginner python exercises and one of them is to remove duplicates from a list. I've successfully done it, but the strange thing is that it is returning a dictionary instead of a list.
This is my code.
import random
a = []
b = []
for i in range(0,20):
n = random.randint(0,10)
a.append(n)
for i in range(0,20):
n = random.randint(0,10)
b.append(n)
print(sorted(a))
print(sorted(b))
c = set(list(a+b))
print(c)
and this is what it's spitting out
[0, 0, 1, 1, 1, 1, 2, 3, 4, 4, 6, 6, 7, 7, 7, 8, 9, 9, 10, 10]
[0, 1, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 7, 8, 9, 9, 10, 10, 10]
{0, 1, 2, 3, 4, 6, 7, 8, 9, 10}
thanks in advance!
{0, 1, 2, 3, 4, 6, 7, 8, 9, 10} is a set, not a dictionary, a dictionary would be printed as {key:value, key:value, ...}
Try print(type(c)) and you'll see it prints <class 'set'> rather than <class 'dict'>
Also try the following
s = {1,2,3}
print(type(s))
d = {'a':1,'b':2,'c':3}
print(type(d))
You'll see the type is different
I mainly use console.log to print a bunch of numbers in 2D array, but after i updated my node to the current LTS version (v12.13.0), it now logs the array with line breaks.
For comparison:
// LTS v10
[ 1, 2, 3, 4, 5, 6, 7 ]
[
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7],
[1, 2, 3, 4, 5, 6, 7]
]
// LTS v12
[
1, 2, 3, 4,
5, 6, 7
]
[
[
1, 2, 3, 4,
5, 6, 7
],
[
1, 2, 3, 4,
5, 6, 7
],
[
1, 2, 3, 4,
5, 6, 7
],
[
1, 2, 3, 4,
5, 6, 7
]
]
How do I 'fix' this?
PS: I don't know if this is important or not but I'm on git bash, windows
console.log uses util.inspect to format and present data values. The formatting performed by util.inspect is controlled by a collection of options and in Node v12 the default value of some of those options was changed.
Specifically, the default value of the compact option was changed from true to 3 (compare that option in the link above to the v10 default options). That's why your arrays are now broken over multiple lines.
I don't have a v12 install handy right now to test, but I believe that you can restore the v10 behaviour by changing the compact option back to true in the util.inspect.defaultOptions object. Just do:
util.inspect.defaultOptions.compact = true;
somewhere in your program before you console.log your arrays. I'll try to find the time to install v12 later to check if that works.
If that approach doesn't work then you might also have to adjust some other options. The breaklength option would be a good candidate. In the worst case you might have to create a separate instance of Console with exactly the inspect options that you want, and call the log method on that new instance (instead of on the global console instance) to show your arrays.
Trying to create a simple Statistical Process Control program. I need absolute values for my moving range. The absolute values are the difference of the successive data points.
I've tried adding 'abs()' throughout the code but no luck.
values = [19,27,20,16,18,25,22,24,17,25,15,17,20,22,19,16,22,19,25,22,18,20,16,17]
move_range = [x-y for x, y in zip(values, values[1:])]
print (move_range)
Current output is:
[-8, 7, 4, -2, -7, 3, -2, 7, -8, 10, -2, -3, -2, 3, 3, -6, 3, -6, 3, 4, -2, 4, -1]
I want these in absolute values.
It would seem that using abs when taking differences should work. That is,
[abs(x - y) for x, y in zip(values, values[1:])]
gives
[8, 7, 4, 2, 7, 3, 2, 7, 8, 10, 2, 3, 2, 3, 3, 6, 3, 6, 3, 4, 2, 4, 1]
The program below will create a list of 100 numbers chosen randomly between 1-10. I need help to then sum the list, then average the list created.
I have no idea how to begin and since I'm watching videos online I have no person to turn to. I'm very fresh in this world so I may just be missing entire ideas. I would doubt that I don't actually know enough though because the videos I paid for are step by step know nothing to know something.
Edit: I was informed that what the program does is overwrite a variable, not make a list. So how do I sum my output like this example?
This is all I have to go on:
Code:
import random
x=0
while x < 100:
mylist = (random.randrange(1,10))
print(mylist)
x = x+1
I think the shortest and pythonic way to do this is:
import random
x = [random.randrange(1,10) for i in range(100)] #list comprehension
summed = sum(x) #Sum of all integers from x
avg = summed / len(x) #Average of the numbers from x
In this case this shouldn't have a big impact, but you should never use while and code manual counter when you know how many times you want to go; in other words, always use for when it's possible. It's more efficient and clearer to see what the code does.
def sum(list):
sm = 0
for i in list:
sm+=i
return sm
Just run sum(list) to get sum of all elements
Or you can use
import random
x=0
mylist = []
sm = 0
while x < 100:
mylist.append(random.randrange(1,10))
sm += mylist[x]
x += 1
Then sm will be sum of list
The code is not correct. It will not create a list but generate a number everytime. Use the below code to get your desired result.
import random
mylist = []
for x in range(100):
mylist.append(random.randrange(1,10))
print(mylist)
print(sum(mylist))
OR
import random
mylist = [random.randrange(1,10) for value in range(100)]
print(mylist)
print(sum(mylist))
Output:
[3, 9, 3, 1, 3, 5, 8, 8, 3, 3, 1, 2, 5, 1, 2, 1, 4, 8, 9, 1, 2, 2, 4,
6, 9, 7, 9, 5, 4, 5, 7, 7, 9, 2, 5, 8, 2, 4, 3, 8, 2, 1, 3, 4, 2, 2,
2, 1, 6, 8, 3, 2, 1, 9, 6, 5, 8, 7, 7, 9, 9, 9, 8, 5, 7, 9, 4, 9, 8,
7, 5, 9, 2, 6, 8, 8, 3, 4, 8, 4, 7, 9, 9, 4, 2, 9, 9, 6, 3, 4, 9, 5,
3, 8, 4, 1, 1, 3, 2, 6]
512
I cannot find how to update a column MultiIndex with dead values after columns have been dropped. I have read documentation, but I do not find a method that fit my needs.
MWE:
MultiIndex(levels=[['41B001', '41B004', '41B011', '41MEU1', '41N043', '41R001', '41R002', '41R012', '41WOL1'], ['CO', 'NO', 'NO2', 'O3', 'PM-10.0', 'PM-2.5', 'SO2']],
labels=[[1, 1, 2, 2, 4, 4, 5, 5, 7, 7, 8, 8], [2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3]],
names=['sitekey', 'measurandkey'])
Keys 41B001, 41MEU1, 41R001 and CO, NO, PM-10.0, PM-2.5, SO2 have been removed but there are still referenced in MultiIndex. I would like a new index without those labels.
Following command does the trick, but it do not find it clean:
data3.T.reset_index().set_index(['sitekey', 'measurandkey']).index
Returns what I expect:
MultiIndex(levels=[['41B004', '41B011', '41N043', '41R001', '41R012', '41WOL1'], ['NO2', 'O3']],
labels=[[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5], [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1]],
names=['sitekey', 'measurandkey'])
Is there a better way (efficient, cleaner, pythonic, pandas friendly) to achieve this working only on MultiIndex instead of transposing the DataFrame?