Store more than 1 value in python array? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I would like to store more than 1 value in a python array(I am open to any other data structure too).
For example :
array[1][2][3] = 1 # (this is what I am able to do now)
But later I also get the value 2, now instead of storing it in another array using the same indices, I want to be able to do this :
array[1][2][3] = 1,2
But I dont want to concatenate the existing result like in a string, and split the string to get the individual values.
Is there a way of doing this without having to introduce another dimension to the array?
edit : I want a neater way to store 2 values in the same cell.
Thanks

I would use defaultdict for this:
from collections import defaultdict
array = defaultdict(list)
array[(1,2,3)].append(1)
array[(1,2,3)].append(2)
Now array at position (1,2,3) is a list containing 1 and 2

If understand correctly, you can store new list with values you need in array[1][2][3] instead of single value:
array[1][2][3] = [1] # instead of = 1
array[1][2][3].append(2) # now it's [1, 2]
array[1][2][3].extend([3,4]) # now it's [1, 2, 3, 4]
all_values = array[1][2][3] # no another dimensions uses to get values

If you know all the contents a priori you may use a tuple. Here I will create pairs of numbers (n, n+1) and store them in a list:
>>> l = [(n,n+1) for n in xrange(0,4)]
>>> l
[(0, 1), (1, 2), (2, 3), (3, 4)]
If you decide you want to append another value, you can. Assume you want to append 3 to the tuple (1,2):
>>> l[1] = l[1] + (3,)
>>> l
[(0, 1), (1, 2, 3), (2, 3), (3, 4)]
Tuples are immutable, so they're faster than lists for construction, but you can't make changes to an instantiated tuple - you must replace it. If you're fairly certain your data won't change, use a tuple. If it will (and often), use a list.
Edit
If you want the values from the tuple, you can do the following:
>>> tup = l[0]
>>> tup
(0, 1)
>>> x, y = tup
>>> x
0
>>> y
1
>>> t, u = l[2] # alternatively
>>> t
2
>>> u
3

array[1][2][3] = 1,2
Seems like you want to have a list in array[1][2][3], which you could actually do with
array[1][2][3] = [1,2]
or alternatively, as already mentionned by others with variations of
array[1][2][3] = []
array[1][2][3].append(1)
array[1][2][3].extend([2])
Mind the fact that use of t-uples is also possible
array[1][2][3] = (1,2)
But in this case, you won't be able to extend your "list" nor to modify its content (t-uples are indeed constant arrays, in size as well as in content.).
Is there a way of doing this without having to introduce another
dimension to the array?
Well... this requirement seems rather antagonistic with your previous one.
array[1][2][3] = 1,2
suggests that your are able to differentiate between "array[1][2][3][0]" (=1) and "array[1][2][3][1]" (=2). So, unless I don't understand your question, the short answer is "no" :)

Related

Subtracting a tuples of the list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
I have a list of tuples
lst = [(1,2,3),(1,2,3),(7,8,9)]
And
I want to find the difference between previous tuple from the next like this:
ans = [(0,0,0), (6,6,6)]
How can I do this in Python?
You can iterate over the data and use the sub method from the operator package to subtract the tuples:
from operator import sub
data = [(1,2,3),(1,2,3),(7,8,9)]
diff = [tuple(map(sub, data[i+1], data[i])) for i in range(0, len(data)-1)]
You could also use a for loop instead of the list comprehension if you prefer. The tuple subtraction is from here: Elegant way to perform tuple arithmetic
from functools import reduce, partial
from operator import sub
my_list = [(1,2,3),(1,2,3),(7,8,9)]
diff_list = list(map(partial(reduce, lambda a, b: tuple(map(sub, a, b))), zip(my_list[1:], my_list)))
To break it down:
zip(my_list[1:], my_list)
This creates a new iterator which yields iterables consisting of every next element of the list followed by the previous.
partial(reduce, lambda a, b: tuple(map(sub, a, b)))
This creates a function which when given an iterable whose elements consist of tuples, will return a new tuple containing the difference between the first and the second tuples. It could be more tuples, but in our case, we only ever pass to this function an iterable consisting of two tuples, it will always do what we want.
Try:
lst = [(1, 2, 3), (1, 2, 3), (7, 8, 9)]
out = [tuple(b - a for a, b in zip(t1, t2)) for t1, t2 in zip(lst, lst[1:])]
print(out)
Prints:
[(0, 0, 0), (6, 6, 6)]

Python test if list items are far apart [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a list in python that might look something like this:
list_example = [1,2,20,21,22]
As you can see, all the numbers are close to their adjacent list items, except for the jump between 2 and 20.
How would I get Python to notice this larger jump and store the list item's index?
You can use zip() to iterate through every i and i+1 element, and then compute the difference between the two adjacent elements.
for a, b in zip(list_example, list_example[1:]):
diff = b - a
if diff > 10:
print("Jump between %d and %d" % (a, b))
But then, it is up to you to define what is exactly a "jump".
If you need the index, you can make use of enumerate():
for i, (a, b) in enumerate(zip(list_example, list_example[1:])):
...
You could get the average distance between elements and then output those indices where the distance to the last element is larger than that average.
>>> lst = [1,2,20,21,22]
>>> avg_dst = sum(abs(a-b) for a, b in zip(lst, lst[1:])) / (len(lst)-1)
>>> [i for i in range(1, len(lst)) if abs(lst[i] - lst[i-1]) > avg_dst]
[2]
You need a set number that is the number that will set a jump off.
This means looping over comparing the current number with its neighbour and alerting whenever the numbers have a difference of your set jump number.
This is assuming your list is already sorted.
Assuming that the values are arranged in ascending order (like your example), you could created a sorted list of (DIFF, INDEX) pairs.
xs = [1, 2, 20, 21, 22]
pairs = sorted((xs[i] - xs[i-1], i) for i in range(1, len(xs)))
print(pairs) # [(1, 1), (1, 3), (1, 4), (18, 2)]

Python, Make variable equal to the second column of an array

I realise that there's a fair chance this has been asked somewhere else, but to be honest I'm not sure exactly what terminology I should be using to search for it.
But basically I've got a list with a varying number of elements. Each element contains 3 values: A string, another list, and an integer eg:
First element = ('A', [], 0)
so
ListofElements[0] = [('A', [], 0)]
And what I am trying to do is make a new list that consists of all of the integers(3rd thing in the elements) that are given in ListofElements.
I can do this already by stepping through each element of ListofElements and then appending the integer onto the new list shown here:
NewList=[]
for element in ListofElements:
NewList.append(element[2])
But using a for loop seems like the most basic way of doing it, is there a way that uses less code? Maybe a list comprehension or something such as that. It seems like something that should be able to be done on a single line.
That is just a step in my ultimate goal, which is to find out the index of the element in ListofElements that has the minimum integer value. So my process so far is to make a new list, and then find the integer index of that new list using:
index=NewList.index(min(NewList))
Is there a way that I can just avoid making the new list entirely and generate the index straight away from the original ListofElements? I got stuck with what I would need to fill in to here, or how I would iterate through :
min(ListofElements[?][2])
You can use a list coprehension:
[x[2] for x in ListOfElements]
This is generally considered a "Pythonic" approach.
You can also find the minimum in a rather stylish manner using:
minimum = min(ListOfElements, key=lambda x: x[2])
index = ListOfElements.index(minimum)
Some general notes:
In python using underscores is the standard rather than CamelCase.
In python you almost never have to use an explicit for loop. Instead prefer
coprehensions or a functional pattern (map, apply etc.)
You can map your list with itemgetter:
>>> from operator import itemgetter
>>> l = [(1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3), (1, 2, 3)]
>>> map(itemgetter(2), l)
[3, 3, 3, 3, 3]
Then you can go with your approach to find the position of minimum value.

Why doesn't addition broadcast over tuples? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
E.g., why can't I do:
(0,1) + (2,2)
and get:
(2,3)
as a result?
Because the + operator is used to make a new tuple that is the combination of two other tuples. That is just how Python was designed.
To do what you want, you can use zip and a generator expression:
>>> t1 = (0,1)
>>> t2 = (2,2)
>>> tuple(x + y for x, y in zip(t1, t2))
(2, 3)
>>>
'+' operator concatenate tuples . if you want to sum tuples item you can use:
tuple(sum(x) for x in zip((0,1),(2,2)))
or
tuple(map(sum,zip((0,1),(2,2))))
Its because of you add tow tuple and + operation for tuples concatenate them ! you can use map and zip functions for that :
>>> map(sum,zip((0,1),(2,2)))
[2, 3]
or use a generator :
>>> tuple(i+j for i,j in zip((0,1),(2,2)))
(2, 3)
and a better way with operator.add:
>>> from operator import add
>>> map(add,(0,1),(2,2))
[2, 3]
Since Python thinks of tuples as lists that are immutable, adding two tuples is just like adding two lists. So, just as adding two lists will concatenate them:
>>> [1, 2] + [3, 4]
[1, 2, 3, 4]
adding two tuples will also concatenate them:
>>> (1, 2) + (3, 4)
(1, 2, 3, 4)
You can create a new tuple that consists of the sum of each pair with a few of Python's built-in functions:
>>> tuple(map(sum, zip((0, 1), (2, 2))))
(2, 3)
This works by using zip() on the two tuples to make a list of pairs:
>>> zip((0, 1), (2, 2))
[(0, 2), (1, 2)]
And using map() to apply sum() on each element of that list, to get a list of sums:
>>> map(sum, zip((0, 1), (2, 2)))
[2, 3]
And, finally, using tuple() to turn that from a list into a tuple.
Element-wise addition is a more specialized operation than concatenation. Fewer tuples could be added together: what would ('a','b') + (1,2,) equal?
Or ('a', 'b') + (1,2,3) for that matter?
Since concatenation is arguably the more commonly desired operation, and importantly, well defined for all tuples, it makes sense that addition of tuples performs concatenation.

Getting one value from a tuple

Is there a way to get one value from a tuple in Python using expressions?
def tup():
return (3, "hello")
i = 5 + tup() # I want to add just the three
I know I can do this:
(j, _) = tup()
i = 5 + j
But that would add a few dozen lines to my function, doubling its length.
You can write
i = 5 + tup()[0]
Tuples can be indexed just like lists.
The main difference between tuples and lists is that tuples are immutable - you can't set the elements of a tuple to different values, or add or remove elements like you can from a list. But other than that, in most situations, they work pretty much the same.
For anyone in the future looking for an answer, I would like to give a much clearer answer to the question.
# for making a tuple
my_tuple = (89, 32)
my_tuple_with_more_values = (1, 2, 3, 4, 5, 6)
# to concatenate tuples
another_tuple = my_tuple + my_tuple_with_more_values
print(another_tuple)
# (89, 32, 1, 2, 3, 4, 5, 6)
# getting a value from a tuple is similar to a list
first_val = my_tuple[0]
second_val = my_tuple[1]
# if you have a function called my_tuple_fun that returns a tuple,
# you might want to do this
my_tuple_fun()[0]
my_tuple_fun()[1]
# or this
v1, v2 = my_tuple_fun()
Hope this clears things up further for those that need it.
General
Single elements of a tuple a can be accessed -in an indexed array-like fashion-
via a[0], a[1], ... depending on the number of elements in the tuple.
Example
If your tuple is a=(3,"a")
a[0] yields 3,
a[1] yields "a"
Concrete answer to question
def tup():
return (3, "hello")
tup() returns a 2-tuple.
In order to "solve"
i = 5 + tup() # I want to add just the three
you select the 3 by:
tup()[0] # first element
so all together:
i = 5 + tup()[0]
Alternatives
Go with namedtuple that allows you to access tuple elements by name (and by index). Details are at https://docs.python.org/3/library/collections.html#collections.namedtuple
>>> import collections
>>> MyTuple=collections.namedtuple("MyTuple", "mynumber, mystring")
>>> m = MyTuple(3, "hello")
>>> m[0]
3
>>> m.mynumber
3
>>> m[1]
'hello'
>>> m.mystring
'hello'

Categories

Resources