Create list of duplicate values from list of lists - python-2.7

I have a list of lists called "indexList" below. I'm trying to create a new list that just contains the values from indexList that appear in more than one of the lists contained in indexList. Is there a simple way to do this, or do I need to create a hash or maybe a for loop with a counter?
print(indexList)
[[14132732, 17507054, 20154219, 57866667, 64995031, 73339549, 76622670, 77054124, 88266242, 95011712, 97504763, 105453976, 119673246, 121145411, 122730527, 136408685, 145004137, 153916914, 160541689, 167759940, 194678471], [14035852, 14239713, 14806084, 28600799, 55796354, 77054124, 80085145, 89842020, 105453976, 105615905, 112819974, 141740256, 141741893, 174759505, 175188439, 189388409, 197055847, 1027845469], [14059532, 55196567, 55855922, 66556068, 89842020, 93011066, 95458113, 105431163, 105615905, 110681306, 160016156, 163230536, 164783106, 175188439, 186797934, 191913967, 331858466, 337685623, 1011598174], [11666669, 12927826, 14049967, 17986728, 30613784, 38761955, 49501165, 52663092, 58344403, 65417742, 75290545, 76769480, 80461647, 81348271, 83741632, 95458113, 95869938, 97144680, 97238886, 108444865, 132547936, 137180880, 144814077, 150365263, 164783106, 166136003, 166380144, 167759940, 189388409, 191913967, 206079517, 239840607, 248336776, 332225104, 1003902828, 1006421644], [14205773, 14239713, 19124994, 27306691, 38457025, 64561619, 76622670, 80290444, 89217656, 100959649, 107140360, 151048919, 167759940, 198478294], [], [15951676, 17986728, 19875521, 30613784, 38761955, 39401305, 39977884, 56603666, 56612203, 58344403, 78354186, 78934707, 89892972, 97320117, 106311786, 126649974, 132547936, 144501061, 147452410, 163699000, 163823179, 167759940, 172118849, 176086128, 232340666, 235833558], [11666669, 14236027, 14806084, 15563629, 15683187, 19124994, 20154219, 24741733, 24788445, 27186241, 27306691, 30613784, 31025020, 38457025, 39280718, 55796354, 55879097, 57136468, 57866667, 62554743, 78354186, 79559892, 79727956, 80689158, 81136486, 83065898, 89842020, 90679924, 92248343, 97144680, 97320117, 105615905, 117722840, 121145411, 121817275, 132547936, 136181420, 136755608, 150697319, 151048919, 151795031, 153916914, 157299696, 163466152, 164783106, 167759940, 192654998, 193361704, 276378790, 277316518, 337517789], [], [24707679, 39555826, 59380375, 76003587, 108444865, 122730527, 162992192, 166380144, 172149940, 175188439, 184932536, 235833558, 235906336, 244838688, 247663297, 277319959, 288845420, 292437922, 311590450, 337094084, 337644502]]

I'm going to assume that there are not repeat values in each sub list.
from collections import Counter
from itertools import chain
counts = Counter(chain(*indexList))
repeats = [key for key,value in counts.items() if value>1]

Related

How To Retrieve Group Of Elements From IEnumerable Without Iterating

I have the following:
IEnumerable<Personel> personel= page.Retrieve<Personel>....
Then I have List which contains only personelIDs
List<int> personelIDs....
I need to retrived all 'personels' from the IEnumerable and assign it into a new List which matches the personelIDs from 'personelIDs ' list.
I can do it my iterating and having verify the IDs and if they're equal assign it into another List,
but is there a short here where I can retrieve it without iterating or having multiple lines of code?
Basically Is there a way on how to shortened this
List<int> pIds = ....// contains only specific personellID's
IEnumerable personelIEn = // contains Personel data like personel IDs, name..etc
List<Personel> personel = personelIEn.ToList();
List<Personel> personelByTag = new List<Personel>();
foreach (Personel b in personel ) {
if (pIds.Contains(b.DocumentID)) {
personelByTag .Add(b);
}
}
return personelByTag ;
basically I'm trying to find ways how to shortened the above code
You can use a predicate:
public List<Personel> search(String documentId, List<Personel> list)
{
Predicate<Personel> predicate = (Personel personel) => (personel.Id== documentId);
return list.FindAll(predicate);
}
Could that help?

Django orm: moving selected records from a column to another and save to database

I have the following list:
list = [
"WP1788/1",
"WP1810/1",
"WP1810/2",
"WP1812/1",
"WP1815/1",
"WP1818/1",
"WP1823/1",
"WP1827/1",
"WP1828/1",
"WP1828/2",
"WP1828/3",
"WP1828/4",
"WP1828/5",
"WP1828/6",
"WP1837/1",
"WP1840/1",
"WP1841/1",
"WP1855/1",
"WP1860/1",
"WP1861/1",
"WP1863/1",
"WP1872/1",
"WP1873/1",
"WP1873/2",
"WP1879/1",
"WP1884/1",
"WP1888/1",
"WP1895/1",
"WP1895/2",
"WP1906/1",
"WP1906/2",
"WP1908/1",
"WP1908/2",
"WP1909/1",
"WP1909/2",
"WP1913/1",
"WP1918/1",
"WP1919/1",
"WP1919/2",
"WP1919/3",
"WP1922/1",
"WP1928/1",
"WP1928/3",
"WP1928/4",
"WP1928/5",
"WP1928/6",
"WP1944/1",
"WP1944/2",
"WP1945/1",
"WP1946/1",
"WP1947/1",
"WP1955/1",
"WP1962/1",
"WP1965/1",
"WP1965/2",
"WP1967/1",
"WP1969/1",
"WP1977/1",
"WP1988/1",
"WP1991/1",
"WP1991/5",
"WP1995/1",
"WP2002/1",
"WP2012/1",
"WP2015/1",
"WP2017/1",
"WP2021/1",
"WP2022/1",
"WP2024/1",
"WP2033/1",
"WP2033/2",
"WP2044/1",
"WP2050/1",
"WP1585/1",
"WP1585/2",
"WP1585/4",
"WP1585/5",
"WP1585/6",
"WP1585/7",
"WP1585/8",
"WP1585/9",
"WP1624/103",
"WP1624/105",
"WP1624/108",
"WP1624/109",
"WP1624/118",
"WP1624/119",
"WP1624/120",
"WP1624/121",
"WP1624/123",
"WP1624/129",
"WP1624/130",
"WP1624/137",
"WP1624/145",
"WP1624/165",
"WP1624/83",
"WP1624/85",
"WP1624/91",
"WP1624/93",
"WP1670/1",
"WP1708/10",
"WP1708/12",
"WP1708/13",
"WP1708/14",
"WP1708/15",
"WP1708/17",
"WP1708/20",
"WP1708/22",
"WP1708/26",
"WP1708/27",
"WP1708/28",
"WP1779/26",
"WP1838/1",
"WP1844/1",
"WP1876/1",
"WP1882/1",
]
I would like to select the wps (Wp model) in the database with ID in list, something like:
wps_selected = Wp.objects.filter(ID in list)
and then copy the value from the column working_hours to the column non_profitable and save it to database. Is that possible using ORM. I know how to do it in SQL but I am still a bit comfuased about using ORM
You can do that in a simple for loop:
wps_selected = Wp.objects.filter(ID__in=list)
for wps in wps_selected:
wps.non_profitable = wps.working_hours
wps.save()
You can update in bulk with:
from django.db.models import F
Wp.objects.filter(
ID__in=mylist
).update(non_profitable=F('working_hours'))
Note: Please do not name a variable list, it overrides the reference to the list builtin function [Python-doc]. Use for example mylist.

sorted() list not working as expected. Python 2.7

Trying to sort this list from lowest (399||1) to highest value (11064||2) while conserving the provided data format to be reused in an API loop request.
As you can see below, sorted() is not working as (I) expected. This is Python 2.7.
It looks like it sorts in pieces. Why would 1000-1100 come before 300-700, and then 8000? I cannot find this same issue posted anywhere.
sorted_d = sorted(d)
print sorted_d
Run:
[u'1053||1', u'1092||2', u'1093||1', u'1094||1', u'1094||2', u'1095||1',
u'1095||2', u'1096||7', u'1096||8', u'1097||7', u'1097||8', u'11064||1',
u'11064||2', u'399||1', u'412||1', u'412||2', u'413||1', u'414||1',
u'434||2', u'616||1', u'617||1', u'618||1', u'619||1', u'620||1', u'621||1',
u'622||1', u'727||1', u'8096||1', u'8097||1', u'8099||1', u'8101||1',
u'8105||1', u'8112||1', u'8113||1', u'8140||1', u'8141||1', u'8142||1',
u'8143||1', u'8144||1', u'8146||2', u'8150||1', u'8152||1', u'8153||1',
u'8154||1', u'8157||1', u'8158||1', u'8159||1', u'8160||1', u'8161||1',
u'8162||1', u'8163||1', u'8164||1', u'8165||1', u'8166||1', u'8167||1',
u'8168||1', u'8169||1', u'8170||1', u'8171||1', u'8172||1', u'8173||1',
u'8174||1', u'8175||1', u'8184||2', u'8184||3', u'8185||2', u'8185||3',
u'8186||5', u'8186||6', u'8187||1', u'8188||2', u'8190||2', u'8191||1']
Assistance greatly appreciated.
Thx
You could also split the strings on the || and specify the first part as the key parameter
sorted_d = sorted(d, key = lambda x: int(x.split('||')[0]))
print sorted_d
[u'399||1', u'412||1', u'412||2', u'413||1', u'414||1', u'434||2', u'616||1', u'617||1', u'618||1', u'619||1', u'620||1', u'621||1', u'622||1', u'727||1', u'1053||1', u'1092||2', u'1093||1', u'1094||1', u'1094||2', u'1095||1', u'1095||2', u'1096||7', u'1096||8', u'1097||7', u'1097||8', u'8096||1', u'8097||1', u'8099||1', u'8101||1', u'8105||1', u'8112||1', u'8113||1', u'8140||1', u'8141||1', u'8142||1', u'8143||1', u'8144||1', u'8146||2', u'8150||1', u'8152||1', u'8153||1', u'8154||1', u'8157||1', u'8158||1', u'8159||1', u'8160||1', u'8161||1', u'8162||1', u'8163||1', u'8164||1', u'8165||1', u'8166||1', u'8167||1', u'8168||1', u'8169||1', u'8170||1', u'8171||1', u'8172||1', u'8173||1', u'8174||1', u'8175||1', u'8184||2', u'8184||3', u'8185||2', u'8185||3', u'8186||5', u'8186||6', u'8187||1', u'8188||2', u'8190||2', u'8191||1', u'11064||1', u'11064||2']
Because it's treating your '1053||1' data as strings and sorting as a string type instead of as a numeric value. So it effectively is sorting in this type of manner, ascending:
1
10
100
1000
2
20
200
2000

Python - get parent index from child index, child level and parent level

I need help to figure out how to get parent index from child index, child-level and parent-level using Python.
I have dataset with three columns: index, child-level and parent-level.
The records are in order of hierarchy.
Index is just the line number of record.
Child-level is number indicating level in hierarchy of nested parent child records.
Parent-level = child-level - 1
My challenge is, for each record, I want to use Python to get each record's parent index.
I suspect a list comprehension might be used to get the max index value where the self join index < child.index and the self join level = child.level
This is a visual representation of the data set.
This is sample data and expected result. Goal is to get parent index.
Index, Child-Level,Parent-Level,Parent-Index
1,1,1,1
2,2,1,1
4,4,3,3
9,9,8,8
3,3,2,2
5,5,4,4
8,8,7,7
6,6,5,5
7,7,6,6
10,10,9,9
11,11,10,10
12,12,11,11
13,13,12,12
14,14,13,13
15,14,13,13
16,14,13,13
17,14,13,13
18,14,13,13
19,14,13,13
20,14,13,13
21,13,12,12
22,13,12,12
23,13,12,12
24,14,13,23
25,14,13,23
26,14,13,23
27,11,10,10
28,9,8,8
29,9,8,8
30,9,8,8
31,9,8,8
32,9,8,8
33,9,8,8
34,9,8,8
35,8,7,7
36,9,8,35
37,10,9,36
38,11,10,37
39,11,10,37
40,12,11,39
41,12,11,39
42,13,12,41
43,13,12,41
44,13,12,41
45,11,10,37
46,12,11,45
47,13,12,46
48,14,13,47
49,14,13,47
50,14,13,47
51,14,13,47
52,14,13,47
53,14,13,47
54,14,13,47
55,13,12,46
56,13,12,46
57,13,12,46
58,9,8,35
59,9,8,35
60,9,8,35
61,9,8,35
62,8,7,7
63,8,7,7
64,8,7,7
65,8,7,7
66,8,7,7
67,8,7,7
68,8,7,7

iOS-NSArray filter having condition

I'm new here!
I have an NSArray like this ("file01_header","file01_body", "file01_xxx", ... ,"file02_header", ...).
I filter it and get 2 distinct NSArrays:
Array_header ("file01_header","file02_header",...)
Array_body
("file01_body", "file02_body", ...)
now I have to subtract from Array_header all the items that have a corresponding one in Array_body, because not all items have a fileNN_body for the fileNN_header in the original array, and I have to give the Array_header as output with only items that has no corresponding body.
How can I do this?
Thanks!!
If you're using swift you can filter pretty easily using a closure and some minor string-fu.
Here's an example:
let funkyArray = ["file01_header","file01_body", "file01_xxx", "file02_header","file03_xxx"]
let headerFilterClosure : (String) -> Bool = {fileName in
if fileName.containsString("header") {
let fileBase = fileName.componentsSeparatedByString("_")[0]
let fileBodyName = "\(fileBase)_body"
if funkyArray.contains(fileBodyName) {
return true
}
}
return false
}
funkyArray.filter(headerFilterClosure)
I think a better long term solution would be to not have such a funky array and use a data structure or at least a tuple with options to manage your file info.
Best,
Josh