I am having a problem with the Union of sets not giving me the correct Length.
ConnectingLists =
Do[AConnectingList = {}; For[i = 1, i <= Length[PlayerACountries], i++,
AConnect = PlayerACountries[[i]][[4]];
AConnectingList = Union[AConnectingList, AConnect]];, {1}]
This code should return a list of unique elements from the 4th part of each element in PlayerACountries. There is a total of 14 elements in PlayerACountries.
It however returns the following:
In[81]:= AConnectingList
Out[81]= {"Brazil, WesternEurope, SouthernEurope, Egypt, EastAfrica, \
CentralAfrica", "CentralAfrica, EastAfrica, Madagascar", \
"CentralAmerica, Brazil, Peru", "China, Siberia, Japan, Irkutsk, \
Kamchatka", "Indonesia, EasternAustralia", "Mongolia, Siberia, \
Yakutsk, Kamchatka", "NorthAfrica, EastAfrica, SouthAfrica", \
"NorthwestTerritory, Alberta, Kamchatka", "Ontario, \
EasternUnitedStates, Greenland", "Peru, Brazil", "Russia, \
Afghanistan, China, Siberia", "Russia, MiddleEast, India, China, \
Ural", "SouthAfrica, EastAfrica", "WesternEurope, NorthernEurope, \
Iceland, Scandinavia"}
Which has length:
In[82]:= Length[AConnectingList]
Out[82]= 14
Why is this not just one single list of elements that should have a much higher length than 14? Also is there any way to make sure that I can do this - which means the Union should also remove the duplicate elements as well??
Thanks.
Do
only one time? Why useFor
?AConnectingList=DeleteDuplicates@PlayerACountries[[All,4]]
should do the job in one line. $\endgroup$Do[expression,{1}]
at top level is a reliable way to prevent the value of the expression not only from output, but also from being stored inOut
(if you just put a semicolon at the end, it doesn't get printed, but still gets stored inOut
). Just writingDo[expression]
(without the{1}
) also works, but has the disadvantage that the syntax highlighter tells you there is a second argument missing. However assigning the resultingNull
toConnectingList
doesn't seem meaningful. $\endgroup$