Thank you for looking at my issue.
I'm trying to compare cells from three csv files to make sure they are exactly the same info. the cells in the csv can contain names, dates or ID numbers. All have to match.
compile = []
for a in Treader,Vreader,Dreader:
for b in a:
compile.append(b[0])
However, the number of variables will fluctuate and I don't want to keep adding index splicing every time. see "complie.append(b[0])" . The question now what way can I construct this to give me a random amount of variables and random number of indexes based on the length "len" of the original list. can i use the range function for that? not sure how i can create something like this.
The current question I have is
List = [[sally,john,jim], [sally,john,jim], [sally,john,jim]]
If I have the list above how could I get it to show
List =[sally,sally,sally]
List1 = [john,john,john]
List2 = [jim,jim,jim]
Also I want to be able to come up with unlimited number of list based on the length of this list that is inside the list. In this case its 3 for three names.
Some of my list has 30 some has 5 so its important I can assign it without having to type list1 to list 30 and manually assign each one.
you may use:
compile = list(zip(Treader,Vreader,Dreader))
this will create a list of tuples, a tuple will have like (sally,john,jim)
after your edit
you may use:
list(zip(*List))
output:
[('sally', 'sally', 'sally'), ('john', 'john', 'john'), ('jim', 'jim', 'jim')]
Related
I am looking for a solution to change the last two lists in a list of lists. The number of lists inside the list is variable. The change of the values depends on every time the last two lists.
list_of_colors=[['red','red','red','red'],['red','red','red','red'],
['red','red','red','red'], ['red','red','red','red']]
I expect the second to the last list changes completely from 'red' to 'lightgrey' and in the last list, only the last two from 'red' into 'lightgrey' - like this:
list_of_colors=[['red','red','red','red'],['red','red','red','red'],
['lightgrey','lightgrey','lightgrey','lightgrey'], ['red','red','lightgrey','lightgrey']]
This list of lists is to color a plotly table - Thanks for the help
For a more dynamic solution, i.e. in case the lists don't have a fixed length, you can try this:
list_of_colors[-2] = ['lightgrey'] * len(list_of_colors[-2])
last_n = 2
list_of_colors[-1][-last_n:] = ['lightgrey'] * last_n
In the last_n variable, I have specified the number of elements you wish to change, of the last list.
If I understood your question correctly, the parent list which contains the lists can have variable number of lists inside it, and you specifically know what values you require inside the last two lists, Then, this should work:
list_of_colors[-1] = ['lightgrey','lightgrey','lightgrey','lightgrey']
list_of_colors[-2] = ['red','red','lightgrey','lightgrey']
I was learning about Deep Learning in Kaggle through an exercise and this confused me. In order to write a code for checking whether something was a hot dog or not, there was a list of predictions, with each element being the most likely prediction for what a different image was. So the overall list was :
[
[('n07697537', 'hotdog', 0.8770528)],
[('n07697537', 'hotdog', 0.9659182)],
[('n07579787', 'plate', 0.7972369)],
[('n07583066', 'guacamole', 0.9996675)]
]
And one element is:
[('n07697537', 'hotdog', 0.9659182)]
So in order to check whether an image is most likely a hotdog, I'd have to get that second field, the label. But I ran into some syntax issues trying to access the field inside the nested list.
So I tried accessing the first element's label as an example (decoded is the name of the outer list) with print(decoded[0][1]). This didn't work. So I checked the sample solution after failing to figure out how to access the element cleanly without having to do something convoluted.
The sample code used
labels = [d[0][1] for d in decoded]
And that successfully makes a list of the labels. I tried to do something similar before checking the solution but I was slightly off, I tried the singular version of this by setting d = decoded[0] , and I got a list of length 1 with the three elements, like the element example earlier. What I found confusing is that d[0][1] works to give the me label, but decoded[0][1] does not. Why?
You need to work on tuples :
decoded = [[('n07697537', 'hotdog', 0.8770528)], [('n07697537', 'hotdog', 0.9659182)], [('n07579787', 'plate', 0.7972369)], [('n07583066', 'guacamole', 0.9996675)]]
d = [x for y in decoded for x in y]
labels = [d[0][1] for d in decoded]
This script gives:
['hotdog', 'hotdog', 'plate', 'guacamole']
If you would like to access the first element's label aka 'hotdog' in the first tuple, you need to print(decoded[0][0][1]) where the [1] in decoded[0][0][1] is the 2nd element in the tuple (0-indexed), the right hand [0] is the tuple itself, and the left hand [0] is the inner list.
Some background: there is actually a list enclosing other lists of tuples in your example shown as [[()],[()],[()]] where () is a tuple and [] is a list. You could in theory have multiple tuples in each inner list like [[(),(),()],[(),()],[()]] etc. However, you access values within tuples the same way as you do with lists, using indices, hence the confusion.
The code [d[0][1] for d in decoded] works because d is actually just a list of tuples (though only one tuple is in the list in this case).
How to create a nested list ?
For example there is a list lines[None]*int(nl) where nl is number of lines, input taken from user, and in lines[] I want to create multiple lists which would hold numbers for different lines.
the below code shows you how to add a list to a list. i am assuming this is what you wanted
list_example = []
# this will be your base list
print (list_example)
#now lets add a sub list
list_example.append([])
print (list_example)
#now lets add a list to that list
list_example[0].append([])
Basically, I need to order a 2D array. Genes is an array of 8 lists, all containing 8 items, all of which are floats. This is for an evolution simulator of sorts, hence 'genes'. My current solution is this:
scores = []
[scores.append(score(x)) for x in genes]
unsorted = genes
genes = [unsorted[0]]
for y in range(7):
for x in range(len(genes)):
if score(unsorted[y+1]) >= score(genes[x]):
genes.insert(x, unsorted[y+1])
break
I have a list of all the scores, I save a copy of 'genes' called 'unsorted', and set genes as the first item it once contained. The nested loop underneath should run through unsorted, taking each item through the 'x' loop, and inserting it into 'genes' once it finds the first item of score equal or smaller than its own. I thought this would work, but for some reason, it returns lists of random sizes, like 3, 2 and 5 or even 16. If you have a more efficient or pythonic way to do this, or just one that works, please help!
That is what sorted is for.
genes = sorted(genes, key=score)
Alright, so I have a question. I am working on creating a script that grabs a random name from a list of provided names, and generates them in a list of 5. I know that you can use the command
items = ['names','go','here']
rand_item = items[random.randrange(len(items))]
This, if I am not mistaken, should grab one random item from the list. Though if I am wrong correct me, but my question is how would I get it to generate, say a list of 5 names, going down like below;
random
names
generated
using
code
Also is there a way to make it where if I run this 5 days in a row, it doesn't repeat the names in the same order?
I appreciate any help you can give, or any errors in my existing code.
Edit:
The general use for my script will be to generate task assignments for a group of users every day, 5 days a week. What I am looking for is a way to generate these names in 5 different rotations.
I apologize for any confusion. Though some of the returned answers will be helpful.
Edit2:
Alright so I think I have mostly what I want, thank you Markus Meskanen & mescalinum, I used some of the code from both of you to resolve most of this issue. I appreciate it greatly. Below is the code I am using now.
import random
items = ['items', 'go', 'in', 'this', 'string']
rand_item = random.sample(items, 5)
for item in random.sample(items, 5):
print item
random.choice() is good for selecting on element at random.
However if you want to select multiple elements at random without repetition, you could use random.sample():
for item in random.sample(items, 5):
print item
For the last question, you should trust the (pseudo-) random generator to not give the same sequence on two consecutive days. The random seed is initialized with current time by default, so it's unlikely to observe the same sequence on two consecutive days, altough not impossible, especially if the number of items is small.
If you absolutely need to avoid this, save the last sequence to a file, and load it before shuffling, and keep shuffling until it gives you a different order.
You could use random.choice() to get one item only:
items = ['names','go','here']
rand_item = random.choice(items)
Now just repeat this 5 times (a for loop!)
If you want the names just in a random order, use random.shuffle() to get a different result every time.
It is not clear in your question if you simply want to shuffle the items or make choose a subset. From what I've made sense you want the second case.
You can use random.sample, to get a given number of random items from a list in python. If I wanted to get 3 randomly items from a list of five letters, I would do:
>>> import random
>>> random.sample(['a', 'b', 'c', 'd', 'e'], 3)
['b', 'a', 'e']
Note that the letters are not necessarily returned in the same order - 'b' is returned before 'a', although that wasn't the case in the original list.
Regarding the second part of your question, preventing it from generating
the same letters in the same order, you can append every new generated sublists in a file, retrieving this file during your script execution and generating a new sublist until it is different from every past generated sublist.
random.shuffle(items) will handle the random order generation
In [15]: print items
['names', 'go', 'here']
In [16]: for item in items: print item
names
go
here
In [17]: random.shuffle(items)
In [18]: for item in items: print item
here
names
go
For completeness, I agree with the above poster on random.choice().