I have a question about sqlite3 in python, it is about the list problem.
Here is the question:
Write a function getMay(dbName) that takes as a parameter the filename of above database and returns two lists, one with the days and one with the temperatures at noon on those days.
Here is My code:
import sqlite3
def getMay(dbName):
conn = sqlite3.connect(dbName)
cur = conn.cursor()
cur.execute('select Day,Temp from May14 where Time= "12:00" order by Day ASC')
print(cur.fetchall())
cur.close()
conn.close()
Here is my output:
[(1, 13.7), (2, 11.1), (3, 12.2), (4, 13.2), (5, 12.9), (6, 12.5), (7,
9.6), (8, 11.6), (9, 13.2), (10, 19.2), (11, 21.7), (12, 15.2), (13, 11.9), (14, 16.4), (15, 12.2), (16, 10.1), (17, 9.8), (18, 16.2), (19, 21.5), (20, 17.8), (21, 17.0), (22, 18.6), (23, 16.5), (24, 21.2), (25, 25.4), (26, 27.8), (27, 27.3), (28, 13.7), (29, 15.0), (30,
22.5), (31, 21.0)]
But the correct output should look like:
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], [13.7, 11.1, 12.2, 13.2,
12.9, 12.5, 9.6, 11.6, 13.2, 19.2, 21.7, 15.2, 11.9, 16.4, 12.2, 10.1,
9.8, 16.2, 21.5, 17.8, 17.0, 18.6, 16.5, 21.2, 25.4, 27.8, 27.3, 13.7,
15.0, 22.5, 21.0])
Anyone know how to solve this problem?
Please help! Thank!
one easy solution could be this:
dayTemp_list = [(1, 13.7), (2, 11.1), (3, 12.2), (4, 13.2), (5, 12.9), (6, 12.5), (7, 9.6), (8, 11.6), (9, 13.2), (10, 19.2), (11, 21.7), (12, 15.2), (13, 11.9), (14, 16.4), (15, 12.2), (16, 10.1), (17, 9.8), (18, 16.2), (19, 21.5), (20, 17.8), (21, 17.0), (22, 18.6), (23, 16.5), (24, 21.2), (25, 25.4), (26, 27.8), (27, 27.3), (28, 13.7), (29, 15.0), (30, 22.5), (31, 21.0)]
days = []
temp = []
for i in dayTemp_list:
days.append(i[0])
temp.append(i[1])
result = (days,temp)
print result
Related
I have a two-dimension (10,2) coordinate which indicates each points label, like
coord_list = [(19, 17), (19, 17), (5, 26), (19, 17), (5, 26), (5, 26), (15, 17), (19, 5), (18, 6), (5, 26)]
I want to change it to a label list that only have one dimension (10,1),(assign a "label" to every unique item and replace each item by its label),like
label_list = [1,1,0....2,3]
I just want to classified points that have same coordinate in a same label, is there some more simple way can achieve it?
I tried to use this code,
label_list = []
for idx, coord in enumerate(coord_list):
if coord == (19,17):
label = 1
label_list.append(label)
if ...
But the problem is I don't know how many different coordinate in my coord_list, so I cannot write all if sentence in my code
Here's what I think you're after. I convert the list to a set, which eliminates duplicates. Then back to a list, and I sort it. Then I map each element of the original list to its index in that sorted list. There are only 5 unique points here, so the indexes will be from 0 to 4:
coord_list = [(19, 17), (19, 17), (5, 26), (19, 17), (5, 26), (5, 26), (15, 17), (19, 5), (18, 6), (5, 26)]
a = sorted(list(set(coord_list)))
print(a)
b = [a.index(i) for i in coord_list]
print(b)
Output:
[(5, 26), (15, 17), (18, 6), (19, 5), (19, 17)]
[4, 4, 0, 4, 0, 0, 1, 3, 2, 0]
Im wondering how I'm available to print the first 4 words from a list[:4] on the same line and then the next 4 [4:8] on the line under and so on. I have a dictionary with over 1000 words and I don't want 1 word to printed on 1 line because then the vertical output goes on forever. I would like to split them up by 4 on one line.
Any tips?
you can try using pprint module of python
Refer https://docs.python.org/3/library/pprint.html
for the dict:
#let your dict be dict1
val = list(dict1.values())
key = list(dict1.keys())
print(val)
print(key)
for i in range(len(val)):
if i // 4 == (i+1)// 4:
print(dict1[key[i]], end=" ")
else:
print(dict1[key[i]], end="\n")
Given dictionary
d = {i: i * i for i in range(200)}
do
from pprint import pprint
pprint(list(d.items()), compact=True)
which prints
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25), (6, 36), (7, 49), (8, 64),
(9, 81), (10, 100), (11, 121), (12, 144), (13, 169), (14, 196), (15, 225),
(16, 256), (17, 289), (18, 324), (19, 361), (20, 400), (21, 441), (22, 484),
(23, 529), (24, 576), (25, 625), (26, 676), (27, 729), (28, 784), (29, 841),
(30, 900), (31, 961), (32, 1024), (33, 1089), (34, 1156), (35, 1225),
(36, 1296), (37, 1369), (38, 1444), (39, 1521), (40, 1600), (41, 1681),
(42, 1764), (43, 1849), (44, 1936), (45, 2025), (46, 2116), (47, 2209),
(48, 2304), (49, 2401), (50, 2500), (51, 2601), (52, 2704), (53, 2809),
(54, 2916), (55, 3025), (56, 3136), (57, 3249), (58, 3364), (59, 3481),
(60, 3600), (61, 3721), (62, 3844), (63, 3969), (64, 4096), (65, 4225),
(66, 4356), (67, 4489), (68, 4624), (69, 4761), (70, 4900), (71, 5041),
(72, 5184), (73, 5329), (74, 5476), (75, 5625), (76, 5776), (77, 5929),
(78, 6084), (79, 6241), (80, 6400), (81, 6561), (82, 6724), (83, 6889),
(84, 7056), (85, 7225), (86, 7396), (87, 7569), (88, 7744), (89, 7921),
(90, 8100), (91, 8281), (92, 8464), (93, 8649), (94, 8836), (95, 9025),
(96, 9216), (97, 9409), (98, 9604), (99, 9801), (100, 10000), (101, 10201),
(102, 10404), (103, 10609), (104, 10816), (105, 11025), (106, 11236),
(107, 11449), (108, 11664), (109, 11881), (110, 12100), (111, 12321),
(112, 12544), (113, 12769), (114, 12996), (115, 13225), (116, 13456),
(117, 13689), (118, 13924), (119, 14161), (120, 14400), (121, 14641),
(122, 14884), (123, 15129), (124, 15376), (125, 15625), (126, 15876),
(127, 16129), (128, 16384), (129, 16641), (130, 16900), (131, 17161),
(132, 17424), (133, 17689), (134, 17956), (135, 18225), (136, 18496),
(137, 18769), (138, 19044), (139, 19321), (140, 19600), (141, 19881),
(142, 20164), (143, 20449), (144, 20736), (145, 21025), (146, 21316),
(147, 21609), (148, 21904), (149, 22201), (150, 22500), (151, 22801),
(152, 23104), (153, 23409), (154, 23716), (155, 24025), (156, 24336),
(157, 24649), (158, 24964), (159, 25281), (160, 25600), (161, 25921),
(162, 26244), (163, 26569), (164, 26896), (165, 27225), (166, 27556),
(167, 27889), (168, 28224), (169, 28561), (170, 28900), (171, 29241),
(172, 29584), (173, 29929), (174, 30276), (175, 30625), (176, 30976),
(177, 31329), (178, 31684), (179, 32041), (180, 32400), (181, 32761),
(182, 33124), (183, 33489), (184, 33856), (185, 34225), (186, 34596),
(187, 34969), (188, 35344), (189, 35721), (190, 36100), (191, 36481),
(192, 36864), (193, 37249), (194, 37636), (195, 38025), (196, 38416),
(197, 38809), (198, 39204), (199, 39601)]
I have a list of items:
[0,1,10,20,5,6,7]
is there a brief, pythonic way to get all groupings of n variables? In this case, similar groups with a different order are considered duplicates.
3:
(0,1,10)
(0,1,20)
(0,2,5)
...
4:
(0,1,10,20)
(0,1,10,5)
(0,1,10,6)
...
Maybe you are looking for "powerset" from recipes in itertools:
from itertools import chain, combinations
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
l = [0,1,10,20,5,6,7]
list(powerset(l))
Output:
[(),
(0,),
(1,),
(10,),
(20,),
(5,),
(6,),
(7,),
(0, 1),
(0, 10),
(0, 20),
(0, 5),
(0, 6),
(0, 7),
(1, 10),
(1, 20),
(1, 5),
(1, 6),
(1, 7),
(10, 20),
(10, 5),
(10, 6),
(10, 7),
(20, 5),
(20, 6),
(20, 7),
(5, 6),
(5, 7),
(6, 7),
(0, 1, 10),
(0, 1, 20),
(0, 1, 5),
(0, 1, 6),
(0, 1, 7),
(0, 10, 20),
(0, 10, 5),
(0, 10, 6),
(0, 10, 7),
(0, 20, 5),
(0, 20, 6),
(0, 20, 7),
(0, 5, 6),
(0, 5, 7),
(0, 6, 7),
(1, 10, 20),
(1, 10, 5),
(1, 10, 6),
(1, 10, 7),
(1, 20, 5),
(1, 20, 6),
(1, 20, 7),
(1, 5, 6),
(1, 5, 7),
(1, 6, 7),
(10, 20, 5),
(10, 20, 6),
(10, 20, 7),
(10, 5, 6),
(10, 5, 7),
(10, 6, 7),
(20, 5, 6),
(20, 5, 7),
(20, 6, 7),
(5, 6, 7),
(0, 1, 10, 20),
(0, 1, 10, 5),
(0, 1, 10, 6),
(0, 1, 10, 7),
(0, 1, 20, 5),
(0, 1, 20, 6),
(0, 1, 20, 7),
(0, 1, 5, 6),
(0, 1, 5, 7),
(0, 1, 6, 7),
(0, 10, 20, 5),
(0, 10, 20, 6),
(0, 10, 20, 7),
(0, 10, 5, 6),
(0, 10, 5, 7),
(0, 10, 6, 7),
(0, 20, 5, 6),
(0, 20, 5, 7),
(0, 20, 6, 7),
(0, 5, 6, 7),
(1, 10, 20, 5),
(1, 10, 20, 6),
(1, 10, 20, 7),
(1, 10, 5, 6),
(1, 10, 5, 7),
(1, 10, 6, 7),
(1, 20, 5, 6),
(1, 20, 5, 7),
(1, 20, 6, 7),
(1, 5, 6, 7),
(10, 20, 5, 6),
(10, 20, 5, 7),
(10, 20, 6, 7),
(10, 5, 6, 7),
(20, 5, 6, 7),
(0, 1, 10, 20, 5),
(0, 1, 10, 20, 6),
(0, 1, 10, 20, 7),
(0, 1, 10, 5, 6),
(0, 1, 10, 5, 7),
(0, 1, 10, 6, 7),
(0, 1, 20, 5, 6),
(0, 1, 20, 5, 7),
(0, 1, 20, 6, 7),
(0, 1, 5, 6, 7),
(0, 10, 20, 5, 6),
(0, 10, 20, 5, 7),
(0, 10, 20, 6, 7),
(0, 10, 5, 6, 7),
(0, 20, 5, 6, 7),
(1, 10, 20, 5, 6),
(1, 10, 20, 5, 7),
(1, 10, 20, 6, 7),
(1, 10, 5, 6, 7),
(1, 20, 5, 6, 7),
(10, 20, 5, 6, 7),
(0, 1, 10, 20, 5, 6),
(0, 1, 10, 20, 5, 7),
(0, 1, 10, 20, 6, 7),
(0, 1, 10, 5, 6, 7),
(0, 1, 20, 5, 6, 7),
(0, 10, 20, 5, 6, 7),
(1, 10, 20, 5, 6, 7),
(0, 1, 10, 20, 5, 6, 7)]
from itertools import combinations
list(combinations([0,1,10,20,5,6,7], 3))
I've got a list of tuples where I need to return a list of the frequency of elements per every 10-second interval depending on a variable ie
data_list =[(0, 84), (1, 84), (2, 84), (3, 84), (4, 84), (5, 84), (6, 84), (7, 84), (8, 84), (9, 84), (10, 84), (11, 84), (12, 84), (13, 84), (14, 84), (15, 84), (16, 84), (17, 84), (18, 84), (19, 84), (20, 84)]
and size = 3
should return
[[0, 10], [1, 10], [2, 1]]
as there are 10(index 2) elements in range 0-9(index 1), 10 elements in range 10-19 and 1 element in range 20-29
I was thinking about creating a for loop that creates x many lists depending on the variable size but not sure that would work at all. Then I tried using a counter but not sure how I would group them in groups of 10 and by the index
Any ideas would be much appreciated.
from collections import Counter
def get_frequency(tuple_list):
x = Counter(elem[0] for elem in tuple_list)
return x
I was wondering why my loglog plots are creating two lines when it's just the orange one I want and why the labels won't show? Any insight would be greatly appreciated.
import matplotlib.pyplot as plt
data1 = [(3, 5034), (2, 4596), (4, 1469), (5, 1209), (6, 540), (7, 380), (8, 196), (9, 136), (10, 71), (11, 47), (12, 39), (13, 20), (14, 16), (15, 12), (16, 6), (18, 5), (17, 2), (19, 2), (22, 2), (1, 1), (24, 1), (20, 1)]
plt.loglog(data1, basex=2, basey=2, label='N1')
plt.show()