I have a list that should contain all my other lists. Currently I append every list separately but that just looks pretty ugly..
looplist = [] # initiate an empty list that contains all date from the other lists
[...]
looplist.append(internallist1)
[...]
looplist.append(internallist10)
the internallists are all getting initialized and filled in a for loop
You can simply use + to merge them.
You may check for more info.
If you want to have list of lists, check this topic.
listOne.extend(anotherList)
this could help you: https://docs.python.org/3/tutorial/datastructures.html
you can also do listOne+=anotherList and this is less expensive, as it doesn`t involve a function call like extend
To answer what you are asking, Just initialize looplist with your 10 lists.
looplist = [internallist1,
internallist2,
internallist3] #Note: internallist3,] is also valid, python allows trailing comma. Nifty!
However, your 10 lists really shouldn't be separately named lists in the first place if this is your real use case. Just use looplist[0] through looplist[9] instead from the get go.
The zip method could work for you as you stated your output should:
look something like [ [list1], [list2], ... , [list n] ]
in your case the code would be similar to
looplist = list(zip(internallist1,[...], internallist10))
Related
I am writing the following code in Python, I want to accomplish the following using a for condition
if (datetime_obj.hour==7):
hour7.append(a)
if (datetime_obj.hour==8):
hour8.append(a)
if (datetime_obj.hour==9):
hour9.append(a)
if (datetime_obj.hour==10):
hour10.append(a)
if (datetime_obj.hour==11):
hour11.append(a)
if (datetime_obj.hour==12):
hour12.append(a)
if (datetime_obj.hour==13):
hour13.append(a)
if (datetime_obj.hour==14):
hour14.append(a)
if (datetime_obj.hour==15):
hour15.append(a)
hour are empty arrays.
Make hour a list of lists, e.g., hour = [[] for _ in range(24)], such that hour[7] would give you whatever you have for hour7, etc. Then you can simply do
hour[datetime_obj.hour].append(a)
No ifs or loops.
If the values that datetime_obj.hour takes aren't consecutive values from 0 to some number, then use a dictionary of lists instead.
It seems like your code will be greatly simplified by abandoning the specific references to each empty list (e.g. hour1, hour2). Rather, you should create some structure which holds the lists, and allows you to access them in a sensible way. The existing answer uses a nested list, which is a good option. Another way would be with a dict:
hours = {i:[] for i in range(24)}
hours[datetime_obj.hour].append(a)
hours is initialized as a dictionary where each key is an integer (representing an hour of the day) and each value is a separate empty list. You can select those empty lists by directly using datetime_obj.hour, and then append your a.
You can check out here for a relevant discussion; for both convenience and functionality, avoiding these repetitive sequential named variables is recommended!
Importing a JSON document into a pandas dataframe using records = pandas.read_json(path), where path was a pre-defined path to the JSON document, I discovered that the content of certain columns of the resulting dataframe "records" are not simply strings as expected. Instead, each "cell" in such a column is an array, containing one single element -- the string of interest. This makes selecting columns using boolean indexing difficult. For example, records[records['category']=='Python Books'] in Ipython outputs an empty dataframe; had the "cells" contained strings instead of arrays of strings, the output would have been nonempty, containing rows that correspond to python books.
I could modify the JSON document, so that "records" reads the strings in properly. But is there a way to modify "records" directly, to somehow strip the single-element arrays into the elements themselves?
Update: After clarification, I believe this might accomplish what you want while limiting it to a single iteration over the data:
nested_column_1 = records["column_name_1"]
nested_column_2 = records["column_name_2"]
clean_column_1 = []
clean_column_2 = []
for i in range(0, len(records.index):
clean_column_1.append(nested_column_1[i][0])
clean_column_2.append(nested_column_2[i][0])
Then you convert the clean_column lists to Series like you mentioned in your comment. Obviously, you make as many nested_column and clean_column lists as you need, and update them all in the loop.
You could generalize this pretty easily by keeping a record of "problem" columns and using that to create a data structure to manage the nested/clean lists, rather than declaring them explicitly as I did in my example. But I thought this might illustrate the approach more clearly.
Obviously, this assumes that all columns have the same number of elements, which maybe isn't a a valid assertion in your case.
Original Answer:
Sorry if I'm oversimplifying or misunderstanding the problem, but could you just do something like this?
simplified_list = [element[0] for element in my_array_of_arrays]
Or if you don't need the whole thing at once, just a generator instead:
simplifying_generator = (element[0] for element in my_array_of_arrays)
I have a list of n strings. For example, strings = ('path1','path2',path3')
I want to create n variables that are equal to functions on these strings. For example:
s1=pygame.mixer.Sound('path1')
s2=pygame.mixer.Sound('path2')
s3=pygame.mixer.Sound('path3')`
I've looked this up a few times before and answers always seem to refer to dictionaries. I am not too familiar with dictionaries although I know their basic function. I don't know how I would use a dictionary to accomplish this.
The problem with dynamically creating variables is: how do you plan on referring them in your code? You'll need to have some abstracted mechanism for dealing with 0..n objects, so you might as well store them in a data type that can deal with collections. How you store them depends on what you want to do with them. The two most obvious choices are list and dict.
Generally, you'll use a list if you want to deal with them sequentially:
paths = ['path1', 'path2', 'path3']
sounds = [ pygame.mixer.Sound(path) for path in paths ]
# play all sounds sequentially
for sound in sounds:
sound.play()
Whereas dict is used if you have some identifier you want to use to refer to the items:
paths = ['path1', 'path2', 'path3']
sounds = { path: pygame.mixer.Sound(path) for path in paths }
# play a specific sound
sounds[name].play()
You don't need to use a dictionary. Use map.
s = map(pygame.mixer.Sound, strings)
The above statement will call pygame.mixer.Sound with each of the strings in strings as arguments, and return the results as a list. Then you can access the variables like you would access any item from a list.
s1 = s[0] # based on your previous definition
The idea is you use a dictionary (or a list) instead of doing that. The simplest way is with a list:
sounds = [pygame.mixer.Sound(path) for path in strings]
You then access them as sounds[0], sounds[1], sounds[2], etc.
http://diveintopython3.ep.io/native-datatypes.html
I've found that link but it seems to rely on the fact that I first have to hard create the list. How can I create a list in Python if it's empty at first. I'm coming from a C# background so this is kind of weird so far.
For example the .append() method is what I'm looking for, but it relies on the fact that the list first exists. Any suggestions?
You can create an empty list like this
L=list()
or
L=[]
As noted by others, you can create an empty list with the list literal operator [].
Its worth noting that you can initialise the list with values if you always need to start with a few values already in place:
a = 5
L = [42, -12, a]
I have two very large lists and to loop through it once takes at least a second and I need to do it 200,000 times. What's the fastest way to remove duplicates in two lists to form one?
This is the fastest way I can think of:
import itertools
output_list = list(set(itertools.chain(first_list, second_list)))
Slight update: As jcd points out, depending on your application, you probably don't need to convert the result back to a list. Since a set is iterable by itself, you might be able to just use it directly:
output_set = set(itertools.chain(first_list, second_list))
for item in output_set:
# do something
Beware though that any solution involving the use of set() will probably reorder the elements in your list, so there's no guarantee that elements will be in any particular order. That said, since you're combining two lists, it's hard to come up with a good reason why you would need a particular ordering over them anyway, so this is probably not something you need to worry about.
I'd recommend something like this:
def combine_lists(list1, list2):
s = set(list1)
s.update(list2)
return list(s)
This eliminates the problem of creating a monster list of the concatenation of the first two.
Depending on what you're doing with the output, don't bother to convert back to a list. If ordering is important, you might need some sort of decorate/sort/undecorate shenanigans around this.
As Daniel states, a set cannot contain duplicate entries - so concatenate the lists:
list1 + list2
Then convert the new list to a set:
set(list1 + list2)
Then back to a list:
list(set(list1 + list2))
result = list(set(list1).union(set(list2)))
That's how I'd do it. I am not so sure about performance, though, but it is certainly better, than doing it by hand.