The code :
import pandas as pd
import numpy as np
import csv
data = pd.read_csv("/content/NYC_temperature.csv", header=None,names = ['temperatures'])
np.cumsum(data['temperatures'])
printcounter = 0
list_30 = [15.22]#first temperature , i could have also added it by doing : list_30.append(i)[0] since it's every 30 values but doesn't append the first one :)
list_2 = [] #this is for the values of the subtraction (for the second iteration)
for i in data['temperatures']:
if (printcounter == 30):
list_30.append(i)
printcounter = 0
printcounter += 1
**for x in list_30:
substract = list_30[x] - list_30[x+1]**
list_2.append(substraction)
print(max(list_2))
Hey guys ! i'm really having trouble with the black part.
**for x in list_30:
substract = list_30[x] - list_30[x+1]**
I'm trying to iterate over the elements and sub stracting element x with the next element (x+1) but the following error pops out TypeError: 'float' object is not iterable. I have also tried to iterate using x instead of list_30[x] but then when I use next(x) I have another error.
for x in list_30: will iterate on list_30, and affect to x, the value of the item in the list, not the index in the list.
for your case you would prefer to loop on your list with indexes:
index = 0
while index < len(list_30):
substract = list_30[index] - list_30[index + 1]
edit: you will still have a problem when you will reach the last element of list_30 as there will be no element of list_30[laste_index + 1],
so you should probably stop before the end with while index < len(list_30) -1:
in case you want the index and the value, you can do:
for i, v in enumerate(list_30):
substract = v - list_30[i + 1]
but the first one look cleaner i my opinion
if you`re trying to find ifference btw two adjacent elements of an array (like differentiate it), you shoul probably use zip function
inp = [1, 2, 3, 4, 5]
delta = []
for x0,x1 in zip(inp, inp[1:]):
delta.append(x1-x0)
print(delta)
note that list of deltas will be one shorter than the input
Hi I am trying to solve a problem where I have to return the indices in a sublist of the same person. When i say same person , I mean if they have the same username,phone or email(any one of them).
I understand that these identites are mostly unique but for the sake of questions lets assume.
eg.
data = [("username1","phone_number1", "email1"),
("usernameX","phone_number1", "emailX"),
("usernameZ","phone_numberZ", "email1Z"),
("usernameY","phone_numberY", "emailX"),
("username2","phone_number2", "emailX")]
Expected output :
[[0,1,3,4][2]]
Explaination: As 0,1 have the same phone and 3 and 4 have the same email so They all fall under one category. and 2 index falls in the other catoegry.
My approach until now is :
data = [("username1","phone_number1", "email1"),
("usernameX","phone_number1", "emailX"),
("usernameZ","phone_numberZ", "email1Z"),
("usernameY","phone_numberY", "emailX"),
]
def match(t1,t2):
if(t1[0] == t2[0] or t1[1] == t2[1] or t1[2] == t2[2]):
return True
else:
return False
# print(match(data[1],data[3]))
together = []
for i in range(len(data)):
temp = {i}
for j in range(len(data)):
if(match(data[i],data[j])):
temp.add(j)
together.append(temp)
for i in range(len(data)):
ans = together[i]
for j in range(i+1,len(data)):
if(bool(ans.intersection(together[j]))):
ans = ans.union(together[j])
print(ans)
I am not able to reach desired result.
Any help is appreciated. Thank you.
A first solution is similar to yours with some enhancements:
Leveraging any for the match, such that it doesn't require to know the number of items inside the tuples.
Checking if a user is already identified as part of "together" to skip useless comparison
Here it is:
together = set()
for user_idx, user in enumerate(data):
if user_idx in together:
continue # That user is already matched
# No need to check with previous users
for other_idx, other in enumerate(data[user_idx + 1 :], user_idx + 1):
# Match
if any(val_ref == val_other for val_ref, val_other in zip(user, other)):
together.update((user_idx, other_idx))
isolated = set(range(len(data))) ^ together
Another solution use tricks by going through a numpy array to identify isolated users. With numpy it is easy to compare a user to every other user (aka the original array). An isolated user will only match one time to itself on each of its fields, hence summing the boolean values along fields will return, for an isolated user, the length of the tuple of fields.
data = np.array(data)
# For each user, match it with the whole matrice
matches = sum(user == data for user in data)
# Isolated users only match with themselves, hence only have 1 on their line
isolated = set(np.where(np.sum(matches, axis=1) == data.shape[1])[0])
# Together are other users
together = set(range(len(data))) ^ set(isolated)
see the matches array for better understanding:
[[1 2 1]
[1 2 3]
[1 1 1]
[1 1 3]
[1 1 3]]
However, it is not leveraging any of the optimisation mentioned before.
Still, numpy is fast so it should be ok.
I'm building a slot machine simulator in Python. I have setup the reels as follows:
Reels = [[10,9,4,5,7,4,9,2,6,7,3,4,9,3,4,9,6,5,4,11,8,9,11,2,4,1,9,10,4,9,10,6,4,9,1,5,4,9,1,10,3,8,6,4,9,1,8],
[4,3,5,4,3,5,2,8,4,1,8,10,1,2,9,8,11,2,8,5,6,11,3,4,2,8,4,7,6,10,8,7,9,4,1,6,8,4,2,9,8,3,5,4,10,8],
[1,9,4,2,5,1,6,9,2,5,9,2,10,9,4,8,9,11,2,5,8,9,10,4,1,10,9,2,10,5,9,7,5,6,8,9,7,3,10,6,2,9,5,8,3,1,10,3],
[8,10,3,8,7,3,9,8,10,11,3,10,9,6,8,10,11,6,5,3,8,1,4,9,5,8,1,4,3,8,1,5,9,10,8,3,9,4,3,8,9,4,6,11,3,8,9,7,10,11],
[4,11,1,6,3,9,5,10,9,5,8,11,10,3,1,4,10,3,9,4,7,3,9,10,4,3,1,5,10,6,5,8,4,6,9,1,5,10,8,9,5,4,6,8,9,4,8,5,7,9]]
Now I need to iterate through them from 1 through 5 and build a 3X5 matrix. I want to start by producing a random number that determines where on that reel to stop. That value will be the middle value on that reel. Then, I need to add the top and bottom values (but have to account for the middle number potentially being at the beginning or end of the reel strip. I'm getting the error "list index out of range" on the if StopValue == Reels[i][len(Reels[i])]: line:
def spin():
SpinValues = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
for i, object in enumerate(Reels):
length = len(Reels[i])
StopValue = random.randint(0,length)
SpinValues[i][1] = Reels[i][StopValue]
if StopValue == 0:
SpinValues[i][0] = Reels[i][len(Reels[i])]
else:
SpinValues[i][0] = Reels[i][StopValue - 1]
if StopValue == Reels[i][len(Reels[i])]:
SpinValues[i][2] = Reels[i][0]
else:
SpinValues[i][2] = Reels[i][StopValue +1]
print(SpinValues)
spin()
Initially I thought I could do this with just "for i in reels," but I read a post here suggesting to use the "for index, object in enumerate(Reels)" method.
len(Reels[i]) is not a valid index for Reels[i]. The last valid index is len(Reels[i]) - 1
To refer to the last item in a list called my_list, you must use
my_list[-1]
or
my_list[len(my_list)-1]
and not:
my_list[len(my_list)]
The reason is that in Python, all indexing starts from 0, and not from 1
All, I am trying to create a jagged list in Python 3.x. Specifically, I am pulling a number of elements from a list of webpages using Selenium. Each row of my jagged list ("matrix") represents the contents of one of these said webpages. Each of these rows should have as many columns as there are elements pulled from its respective webpage - this number will vary from page to page.
e.g.
webpage1 has 3 elements: a,b,c
webpage2 has 6 elements: d,e,f,g,h,i
webpage3 has 4 elements: j,k,l,m
...
would look like:
[[a,b,c],
[d,e,f,g,h,i],
[j,k,l,m],...]
Here's my code, thus far:
from selenium import webdriver
chromePath = "/Users/me/Documents/2018/chromedriver"
browser = webdriver.Chrome(chromePath)
url = 'https://us.testcompany.com/eng-us/women/handbags/_/N-r4xtxc/to-1'
browser.get(url)
hrefLinkArray = []
hrefElements = browser.find_elements_by_class_name("product-item")
for eachOne in hrefElements:
hrefLinkArray.append(eachOne.get_attribute('href'))
pics = [[]]
for y in range(0, len(hrefLinkArray)): # or type in "range(0, 1)" to debug
browser.get(hrefLinkArray[y])
productViews = browser.find_elements_by_xpath("// *[ # id = 'lightSlider'] / li")
b = -1
for a in productViews:
b = b + 1
# print(y) for debugging
# print(b) for debugging
pics[y][b] = a.get_attribute('src') # <------------ ERROR!
# pics[y][b].append(a.get_attribute('src') GIVES SAME ERROR AS ABOVE
del productViews[:]
browser.quit()
Whenever I run this, I get an error on the first iteration of the a in productViews loop:
line 64, in <module>
pics[y][b] = a.get_attribute('src')
IndexError: list assignment index out of range
From what I can tell, the the integer references are correct (see my debugging lines in the for a in productViews loop), so pics[0][0] is a proper way to reference the jagged list. This being said, I have a feeling pics[0][0] does not yet exist? Or maybe only pics[0] does? I've seen similar posts about this error, but the only solution I've understood seems to be using .append(), and even as such, using this on a 1D list. As you can see in my code, I've used .append() for the hrefLinkArray successfully, whereas it appears unsuccessful on line 64/65. I'm stumped as to why this might be.
Please let me know:
Why my lines .append() and [][]=... are throwing this error.
If there is a more efficient way to accomplish my goal, I'd like to learn!
UPDATE: using #User4343502's answer, in conjunction with #StephenRauch's input, the error resolved and I now and getting the intended-sized jagged list! My amended code is:
listOfLists = []
for y in range(0, len(hrefLinkArray)):
browser.get(hrefLinkArray[y])
productViews = browser.find_elements_by_xpath("// *[ # id = 'lightSlider'] / li")
otherList = []
for other in productViews:
otherList.append(other.get_attribute('src'))
# print(otherList)
listOfLists.append(otherList)
del otherList[:]
del productViews[:]
print(listOfLists)
Note, this code prints a jagged list of totally empty indices e.g. [[][],[][][][],[],[][][],[][],[][][][][]...], but that is a separate issue - I believe related to my productViews object and how it retrieves by xpath... What's important, though, is that my original question was answered. Thanks!
list.append will add an element into a list. This works regardless of what the element is.
a = [1, 2, 3]
b = [float, {}]
c = [[[None]]]
## We will append to this empty list
list_of_lists = []
for x in (a, b, c):
list_of_lists.append(x)
## Prints: [[1, 2, 3], [<type 'float'>, {}], [[[None]]]]
print(list_of_lists)
Try it Online!
You are given information about users of your website. The information includes username, a phone number and/or an email. Write a program that takes in a list of tuples where each tuple represents information for a particular user and returns a list of lists where each sublist contains the indices of tuples containing information about the same person. For example:
Input:
[("MLGuy42", "andrew#example.com", "123-4567"),
("CS229DungeonMaster", "123-4567", "ml#example.net"),
("Doomguy", "john#example.org", "carmack#example.com"),
("andrew26", "andrew#example.com", "mlguy#example.com")]
Output:
[[0, 1, 3], [2]]
Since "MLGuy42", "CS229DungeonMaster" and "andrew26" are all the same person.
Each sublist in the output should be sorted and the outer list should be sorted by the first element in the sublist.
Below is the code snippet that I did for this problem. It seems to work fine, but I'm wondering if there is a better/optimized solution. Any help would be appreciated. Thanks!
def find_duplicates(user_info):
results = list()
seen = dict()
for i, user in enumerate(user_info):
first_seen = True
key_info = None
for info in user:
if info in seen:
first_seen = False
key_info = info
break
if first_seen:
results.append([i])
pos = len(results) - 1
else:
index = seen[key_info]
results[index].append(i)
pos = index
for info in user:
seen[info] = pos
return results
I think I've reached to an optimized working solution using a graph. Basically, I've created a graph with each node contains its user information and its index. Then, use dfs to traverse the graph and find the duplicates.
I think we can simplify this using sets:
from random import shuffle
def find_duplicates(user_info):
reduced = unreduced = {frozenset(info): [i] for i, info in enumerate(user_info)}
while reduced is unreduced or len(unreduced) > len(reduced):
unreduced = dict(reduced) # make a copy
for identifiers_1, positions_1 in unreduced.items():
for identifiers_2, positions_2 in unreduced.items():
if identifiers_1 is identifiers_2:
continue
if identifiers_1 & identifiers_2:
del reduced[identifiers_1], reduced[identifiers_2]
reduced[identifiers_1 | identifiers_2] = positions_1 + positions_2
break
else: # no break
continue
break
return sorted(sorted(value) for value in reduced.values())
my_input = [ \
("CS229DungeonMaster", "123-4567", "ml#example.net"), \
("Doomguy", "john#example.org", "carmack#example.com"), \
("andrew26", "andrew#example.com", "mlguy#example.com"), \
("MLGuy42", "andrew#example.com", "123-4567"), \
]
shuffle(my_input) # shuffle to prove order independence
print(my_input)
print(find_duplicates(my_input))
OUTPUT
> python3 test.py
[('CS229DungeonMaster', '123-4567', 'ml#example.net'), ('MLGuy42', 'andrew#example.com', '123-4567'), ('andrew26', 'andrew#example.com', 'mlguy#example.com'), ('Doomguy', 'john#example.org', 'carmack#example.com')]
[[0, 1, 2], [3]]
>