Mathematica: How to get element of nested list - list

I have a little problem with getting element from nested list. I have list like this:
list{{4, 1}, {3, 2}, {3, 3}, {4, 4}}
I want to get single number from this list, e.g. 4. How can I get it? I've tried something like
list[[1]]
and other things. I've tried many times and failed. Would be great if anyone could show me this. Thanks!

That's not valid Mathematica syntax. Do you mean this?
list = {{4, 1}, {3, 2}, {3, 3}, {4, 4}}
If so, then notice that the numbers in that list are "two levels down" so you have to use the [[_]] operator twice. Either like list[[1]][[2]] or I think list[[1,2]] works too.

Related

What ...[] means in Flutter Cloud Firestore | Firebase?

I am still learning and I have a question. What excatly in flutter means? -
`'...[]'`
Just wondering.. When im using 'for in' to fetch data from my database I have to use this '...[]' but why? What does it doing?
I couldn't find answer in the google, probably im pretty bad in googling stuff but ofcourse I got you guys!
Thanks for reply!
This is the spread operator, is used when you want to separate the items, imagine a situation that you want to join two lists:
List<int> a = [1, 2, 3];
List<int> b = [4, 5, 6];
If you just put them inside a list, you will get a List of lists.
List<int> combinedLists = [a, b];
// [[1, 2, 3], [4, 5, 6]]
But when you use spread operator, you will get the items out of list, like this:
List<int> combinedLists = [...a, ...b];
// [1, 2, 3, 4, 5, 6]

(wx)Maxima: list factors of an integer

How can one return the factors of an integer in a list? e.g. list_factors(6); > [1,2,3,6]? Is something like this possible? I looked through the documentation but didn't find anything like this tied to "factor" or "prime".
I think this is what you want:
listify(divisors(6)); > [1, 2, 3, 6]

Python 3 list with range and other individual numbers

I need to make a list of numbers. These numbers represent binary masks. The first 100 or so masks are all included in this range. In the next group of masks only certain masks are included. I need a list similar to the following.
[1,2,3,5,6,7,8,9,10,30,34,48,53,62]
Can I do something like [range(1,10),30,34,48,53,62]
or do I need to create my list using range(1,10) and then append the next list to it?
Thanks
Python 3 actually allow you to build a list literal prepending an * to any iterable objects - which are in turn expanded in place:
>>> [1,2, *range(10), *range(2)]
[1, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1]
If you need this n older Pythons, or if you'd prefer to keep readability for people not too proeficient in Python who might have to walk through your code, an option is just to concatenate your different list fragments using the + operator:
a = list(range(1,10)) + [ 30,34,48,53,62]
Looks like I had to use the list(range(1,10)+[47,34,57]
solution

Creation of unexpected tuple value

I am dealing with somewhat of a mystery and hoped for some clarity. I wrote a script for finding dice roll combinations adding to 24 that looks like the following:
start=[3,3,3,3,3,3,3,3]
outcomes=set(tuple(start)) #Use a set to ensure uniqueness
index_list=np.random.randint(0,8,1000)
#This little snippet adds one and subtracts one randomly, keeping total at 24
for i in xrange(0,500):
upper=index_list[i]
downer=index_list[i+20]
if start[upper]!=6 and start[downer]!=1:
start[upper]=start[upper]+1
start[downer]=start[downer]-1
outcomes.add(tuple(start))
print outcomes
What I am running into, is that When I look at outcomes, there is a single 3 of type 'int' in there.
set([(4, 4, 4, 3, 2, 2, 2, 3), 3, (2, 5, 4, 3, 1, 4, 2, 3), (4, 4, 4, 2, 3, 1, 3, 3),(4, 2, 5, 2, 3, 4, 1, 3)])
While I could certainly remove it, I am just curious how it is getting in there to begin with? My initial guess was the index list might be producing an index outside of [0-7], but it is not. I've looked for a similar question other places, but have yet to find a similar issue. Thanks!
set expects an iterable. You're passing a tuple which is an iterable.
set iterates through it, leaving just 1 value: 3 (because your tuple only contains the same 3 value).
You have to put your element in a list or tuple so it is seen as a single element (exactly the same problem when you pass a string and it is unexpectedly iterated upon)
The rest of your code is OK and has nothing to do with the problem.
Do this instead:
outcomes=set([tuple(start),])
now set iterates through a list of 1 tuple, effectively creating tuple elements.
You could do that also, maybe simpler:
outcomes=set()
outcomes.add(tuple(start))
there's no ambiguity since you're adding 1 element. It's not iterated through.

Generate Random Number from fix Set of numbers in iphone

Suppose I have One set of numbers i.e
{1, 6, 3, 5, 7, 9}
I want to Generate Random number from this set of number only i.e. a Generated number should be random and should be from these number({1, 6, 3, 5, 7, 9}) only.
standard C/C++ function will also do...
arc4random%(set count) = a random index.
What they are telling you is this. Generate a random number from 0-5. Then use that as an index into the array. Eg if the random # is 2, look at element #2 (the third one since you start at 0) of your list of numbers, which is 3. If the random # is 5, you get 9.
MSalters' comment shows you how to do it in a single expression.