I want to iterate randomly over some given items in my list
for example:
items = ['rock', 'paper', 'scissors']
for item in items:
print(random.shuffle(items))
I want in each iteration it splits out a random item from my list but everytime it gives 'None'
random.shuffle() shuffles the list in-place and it returns None.
You need to:
import random
items = ['rock', 'paper', 'scissors']
for item in random.sample(items,3): # get list of n=3 random shuffled values no dupes
print(item)
# or
random.shuffle(items) # shuffle in place (might get the same order as well)
for item in items:
print(item)
See differences between:
https://docs.python.org/3.8/library/random.html#random.shuffle (in place)
https://docs.python.org/3.8/library/random.html#random.sample (no dupes)
https://docs.python.org/3.8/library/random.html#random.choices (maybe dupes)
Related
Have got a item = set(), need to append values to this 'item' to the end. But set() append it to first position pushing already present values to last as shown below.
item = {'Mango'}
item.add('Apple')
#Returns
{'Apple','Mango'}
#Expected output
{'Mango','Apple'}
Even tried item.update(['Apple']) doesn't work.
It looks like you want a data structure that has an ordered sequence. You can do this with lists instead of a set, and use .append to add things to the end of the list:
item = ['Mango', 'Apple']
item.append('Pear')
#Output
['Mango', 'Apple', 'Pear']
I'm using sklearn TfIdfVectorizer. I'm trying to get the column names in a list in the order of thier tf-idf values in decreasing order for each document? So basically, If a document has all the stop words then we don't need any column names.
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
msg = ["My name is Venkatesh",
"Trying to get the significant words for each vector",
"I want to get the list of words name in the decresasing order of their tf-idf values for each vector",
"is to my"]
stopwords=['is','to','my','the','for','in','of','i','their']
tfidf_vect = TfidfVectorizer(stop_words=stopwords)
tfidf_matrix=tfidf_vect.fit_transform(msg)
pd.DataFrame(tfidf_matrix.toarray(),
columns=tfidf_vect.get_feature_names_out())
I want to generate a column with the list word names in the decreasing order of their tf-idf values
So the column would be like this
['venkatesh','name']
['significant','trying','vector','words','each','get']
['decreasing','idf','list','order','tf','values','want','each','get','name','vector','words']
[] # empty list Since the document consists only stopwords
Above is the primary result I'm looking for, it would be great if we get the sorted dict with tdfidf values as keys and the list of words as values asociated with that tfidf value for each document
So,the result would be like the below
{'0.785288':['venkatesh'],'0.619130':['name']}
{'0.47212':['significant','trying'],'0.372225':['vector','words','each','get']}
{'0.314534':['decreasing','idf','list','order','tf','values','want'],'0.247983':['each','get','name','vector','words']}
{} # empty dict Since the document consists only stopwords
I think this code does what you want and avoids using pandas:
from itertools import groupby
sort_func = lambda v: v[0] # sort by first value in tuple
all_dicts = []
for row in tfidf_matrix.toarray():
sorted_vals = sorted(zip(row, tfidf_vect.get_feature_names()), key=sort_func, reverse=True)
all_dicts.append({val:[g[1] for g in group] for val, group in groupby(sorted_vals, key=sort_func) if val != 0})
You could make it even less readable and put it all in a single comprehension! :-)
The combination of the following function and to_dict() method on dataframe can give you the desired output.
def ret_dict(_dict):
# Get a list of unique values
list_keys = list(set(_dict.values()))
processed_dict = {key:[] for key in list_keys}
# Prepare dictionary
for key, value in _dict.items():
processed_dict[value].append(str(key))
# Sort the keys (as you want)
sorted_keys = sorted(processed_dict, key=lambda x: x, reverse=True)
sorted_keys = [ keys for keys in sorted_keys if keys > 0]
# Return the dictionary with sorted keys
sorted_dict = {k:processed_dict[k] for k in sorted_keys}
return sorted_dict
Then:
res = pd.DataFrame(tfidf_matrix.toarray(), columns=tfidf_vect.get_feature_names_out())
list_dict = res.to_dict('records')
processed_list = []
for _dict in list_dict:
processed_list.append(ret_dict(_dict))
processed_list contains the output you desire. For instance: processed_list[1] would output:
{0.47212002654617047: ['significant', 'trying'], 0.3722248517590162: ['each', 'get', 'vector', 'words']}
In the func function, I performed some processing on the oldlist[] and then saved the result in a buffer[] list. Now I want to save this buffer[]list to be the first one in a list of tuples. After that, a new values in buffer[] must be saved as a second list in tuples and so on.
The output something like this
#list of tuple:
Ltuple=[(1,2,3),(66,53,6),(3,1,5,8,3)]
I do not know how to use list of tuple any advice please?
def func(oldlist):
buffer = []
buffer.append(oldlist[0])
# some processing on the items of oldlist[]
buffer.append(oldlist[index])
print(buffer)
# Now here I need to append the buffer[] in list of tuple to be the
# first list in tuple and in next time the new list insert to be
# the second list in list of tuple
buffer.clear()
oldlist=[]
#Some code to deal with oldlist[] and in each time send some items
# from oldlist[] to func()
func(oldlist)
oldlist.clear()
Can you pass Ltuple to the function?
If not it has to be made global or you need to return the buffer tuple
I have modified the function below to take the list of tuples also.
def func(oldlist, list_of_tuples):
buffer = []
buffer.append(oldlist[0])
# some processing on the items of oldlist[]
buffer.append(oldlist[index])
print(buffer)
# Now here I need to append the buffer[] in list of tuple to be the
# first list in tuple and in next time the new list insert to be
# the second list in list of tuple
list_of_tuples.append(tuple(buffer))
buffer.clear()
oldlist=[]
#Some code to deal with oldlist[] and in each time send some items
# from oldlist[] to func()
list_of_tuples = []
func(oldlist, list_of_tuples)
oldlist.clear()
any help would be appreciated! I'm scraping multiple URLs and iterating over the URLs with a for loop. I'm putting relevant data into individual lists. however, I'm trying to organize my data in a list of lists to compare with other data... that I have't scraped yet. How do I iterate through the list of lists and put data into each element of the list? this doesn't seem that hard... don't know what I'm missing?
def get_info(item_urls)#, count): #count is being passed in, leaving this here for context
for item in item_urls:
#get data and stuff from current URL
data = ["beer", "is", "awesome!", "...", "for", "helping", "with", "my", "depression"]
count = len(data) # counting data for a number, that I should have just made up :)
table = [[] for i in range(0, count)]
for truth in data:
for i in range(0, count):
list('table[{}]'.format(i)).append(truth)
print(truth)
for thing in table[0]:
print(thing)
return "borked"
my fake logic:
for each element in data, append the element to table.
Once I iterate through all the URLs, I would like to return the entire built out table.
myList[i] iterates through a list. myList[i][j] iterates through elements in list of lists. j is the index for element in the inner list.
I have a dictionary where it includes few sub-dictionaries in it. Each sub-dictionary has many keys. After running a for loop with an if condition too, the results are generated. I want to add ALL the results to under the desired key; but all what my code actually does is adding the result of the last iteration of the loop thereby replacing the value of the previous iteration.
But, actually, i want to print all the results.
for item in list1: #item is a tuple & list1 has tuples in it
if item == node_pair: #node pair is another tuple
high_p[i]["links"] = link_name #"links" is the key
desired output:
"links": [link_name1, link_name2, link_name3]
what i get:
"links" : link_name3
Please guide me..
So each sub-dictionary needs to have lists as values. You could pre-populate each sub-dictionary with lists ahead of time, but it's easier to create them on demand using setdefault.
for item in list1:
if item == node_pair:
high_p[i].setdefault("links", []).append(link_name)