如何在不重复数据集中的字母的情况下为大于 0 的字母制作列表?
How to make a list for letters with great than 0 without repeation for that letter from a dataset?
我有一个包含 3 列的数据集。一列有特定的字母代码(可能重复),另一列有相应的值(可以是正数或负数)。
如何同时计算大于 0 和/或小于 0 的字母,而不重复这些字母代码。
我放置了我的数据集及其外观,因为我是编程新手,也许有人可以帮助我。
1 G 360.0
2 M -67.2
3 K -78.0
4 R 57.7
5 V -129.4
6 L -98.5
7 L -109.6
8 L -111.2
9 F -121.3
10 D -58.2
11 V -96.1
12 D -61.7
13 G 81.5
14 T -107.7
15 L -104.1
16 T -135.2
17 P -63.7
18 P -56.9
19 R 48.2
20 L -137.9
21 C -55.9
22 Q -58.8
23 T -88.4
24 D -56.9
25 E -68.8
26 M -68.1
27 R -59.4
28 A -65.4
29 L -56.1
30 I -52.3
31 K -62.6
32 R -59.7
33 A -65.9
34 R -65.6
35 G -63.5
36 A -83.5
37 G 98.0
38 F -87.4
39 C -86.2
40 V -129.2
41 G -148.2
42 T -108.0
43 V -142.0
44 G -147.4
45 G -78.2
46 S -85.9
47 D -80.1
48 F -57.9
49 A -52.7
50 K -66.2
51 Q -67.9
52 V -56.0
53 E -66.7
54 Q -89.2
55 L -114.1
56 G 127.3
57 R -53.8
58 D -97.3
59 V -56.5
60 L -67.2
61 T -83.0
62 Q -76.5
63 F -121.9
64 D -70.3
65 Y -122.5
66 V -129.3
67 F -111.5
68 A -97.9
69 E 48.1
70 N 59.2
71 G 94.5
72 L -67.1
73 L -124.6
74 A -131.0
75 Y -124.9
76 R -124.0
77 N 41.4
78 G 90.0
79 L -98.8
80 E -74.8
81 I -108.0
82 H -161.7
83 R -151.8
84 Q -86.3
85 S -127.4
86 L -65.1
87 L -53.8
88 N -63.8
89 A -93.8
90 L -121.2
91 G 94.3
92 N -63.7
93 D -50.7
94 R -65.5
95 I -60.7
96 V -65.8
97 K -53.3
98 F -63.4
99 V -68.9
100 K -60.3
101 K -62.8
102 T -58.6
103 L -58.8
104 R -64.2
105 L -60.2
106 I -65.0
107 A -57.0
108 D -84.0
109 L -73.1
110 D -125.5
111 I -116.1
112 P -67.6
113 V -134.4
114 Q -140.6
115 R -132.5
116 G -169.7
117 T 59.6
118 F -85.7
119 V -125.8
120 E -109.0
121 Y -94.4
122 R -100.6
123 N -85.9
124 G -114.2
125 M -149.6
126 I -119.7
127 N -111.6
128 V -121.1
129 S -124.8
130 P -57.6
131 I -82.7
132 G 96.0
133 R -60.6
134 N -79.8
135 C -74.3
136 S -75.5
137 Q -59.3
138 A -63.4
139 E -59.6
140 R -64.6
141 D -57.7
142 E -71.2
143 F -56.8
144 E -54.7
145 V -62.4
146 Y -53.6
147 D -66.6
148 N -54.7
149 E -74.5
150 H -99.5
151 R 45.5
152 V -73.3
153 R -61.4
154 A -67.5
155 S -55.5
156 L -62.4
157 I -58.4
158 A -57.4
159 E -64.7
160 L -63.7
161 E -57.5
162 N -67.1
163 S -72.3
164 F -131.2
165 P -60.2
166 D -75.0
167 F -66.6
168 G 63.2
169 L -81.0
170 K -100.9
171 Y -124.2
172 S -133.9
173 I -65.8
174 G -164.2
175 G 60.4
176 Q -84.4
177 I -137.4
178 S -148.6
179 F -143.5
180 D -90.7
181 V -132.0
182 F -161.0
183 P -59.4
184 V -51.1
185 G 91.8
186 W -101.3
187 D -74.8
188 K -59.5
189 T -62.5
190 Y -53.7
191 C -72.7
192 L -70.3
193 Q -54.4
194 F -90.1
195 V -123.2
196 E -56.3
197 D -86.6
198 D -117.1
199 F -132.3
200 E -82.0
201 E -139.3
202 I -127.8
203 H -98.3
204 F -109.4
205 F -125.3
206 G -166.1
207 D -111.6
208 K -106.3
209 T -95.0
210 Q -111.3
211 E -44.9
212 G 85.2
213 G -94.1
214 N -69.6
215 D -107.0
216 Y -55.3
217 E -60.1
218 I -71.7
219 Y -54.6
220 T -88.2
221 D -72.2
222 K -60.9
223 R -87.0
224 T -121.2
225 I -92.0
226 G -90.9
227 H -131.4
228 K -78.3
229 V -117.0
230 T -113.3
231 S -168.2
232 Y -68.5
233 K -66.5
234 D -58.4
235 T -55.9
236 I -49.6
237 A -59.3
238 E -75.3
239 V -67.9
240 E -58.2
241 K -72.3
242 I -61.4
243 I -58.4
244 A -57.1
245 M -91.2
246 K -5.7
1 G 360.0
2 M -62.2
3 K -77.2
4 R 58.6
5 V -136.3
6 L -101.2
7 L -110.2
8 L -114.7
9 F -121.9
10 D -64.0
11 V -100.6
12 D -62.8
13 G 82.6
14 T -109.8
15 L -98.0
16 T -134.7
17 P -58.1
18 P -59.7
19 R 67.0
20 L -118.3
21 C -60.1
22 Q -63.3
23 T -87.1
24 D -52.2
25 E -54.0
26 M -64.9
27 R -61.2
28 A -65.2
29 L -57.7
30 I -57.9
31 K -60.1
32 R -61.0
33 A -60.2
34 R -69.2
35 G -58.0
36 A -76.5
37 G 90.8
38 F -90.4
39 C -84.8
40 V -124.4
41 G -150.4
42 T -104.7
43 V -141.9
44 G -148.2
45 G -76.1
46 S -71.4
47 D -81.9
48 F -63.6
49 A -59.7
50 K -64.4
51 Q -66.0
52 V -56.6
53 E -65.2
54 Q -85.8
55 L -116.2
56 G 123.8
57 R -58.9
58 D -96.8
59 V -51.8
60 L -60.8
61 T -85.4
62 Q -71.1
63 F -124.2
64 D -62.5
65 Y -124.4
66 V -126.5
67 F -110.7
68 A -102.0
69 E 47.2
70 N 69.9
71 G 100.0
72 L -63.2
73 L -119.7
74 A -129.8
75 Y -124.6
76 R -127.0
77 N 40.9
78 G 91.6
79 L -99.8
80 E -71.1
81 I -103.0
82 H -161.3
83 R -146.6
84 Q -93.5
85 S -130.0
86 L -57.6
87 L -59.3
88 N -58.0
89 A -85.8
90 L -108.3
91 G 78.8
92 N -54.9
93 D -60.1
94 R -67.0
95 I -57.2
96 V -62.5
97 K -60.2
98 F -64.6
99 V -66.4
100 K -62.4
101 K -63.9
102 T -63.1
103 L -55.9
104 R -60.3
105 L -62.4
106 I -60.3
107 A -56.5
108 D -88.5
109 L -77.2
110 D -118.0
111 I -115.8
112 P -53.9
113 V -132.3
114 Q -143.3
115 R -127.0
116 G -173.2
117 T 61.9
118 F -89.4
119 V -128.1
120 E -107.0
121 Y -86.0
122 R -93.1
123 N -91.2
124 G -118.9
125 M -150.7
126 I -116.4
127 N -109.3
128 V -122.9
129 S -124.5
130 P -58.5
131 I -81.9
132 G 97.2
133 R -64.4
134 N -76.6
135 C -80.7
136 S -74.9
137 Q -58.9
138 A -62.5
139 E -64.9
140 R -64.5
141 D -63.3
142 E -51.9
143 F -59.1
144 E -56.6
145 V -57.4
146 Y -44.9
147 D -66.3
148 N -64.1
149 E -92.5
150 H -113.4
151 R 49.5
152 V -66.4
153 R -62.0
154 A -64.2
155 S -60.3
156 L -57.9
157 I -57.6
158 A -57.2
159 E -63.3
160 L -62.7
161 E -60.0
162 N -72.5
163 S -72.5
164 F -131.4
165 P -64.1
166 D -72.8
167 F -72.6
168 G 59.4
169 L -95.6
170 K -100.7
171 Y -124.9
172 S -134.2
173 I -64.3
174 G -156.5
175 G 60.6
176 Q -88.1
177 I -133.7
178 S -148.1
179 F -139.0
180 D -93.5
181 V -129.4
182 F -160.8
183 P -62.6
184 V -53.2
185 G 88.8
186 W -100.5
187 D -68.8
188 K -56.4
189 T -59.7
190 Y -54.6
191 C -69.9
192 L -65.6
193 Q -58.0
194 F -88.4
195 V -121.1
196 E -55.7
197 D -85.0
198 D -120.1
199 F -134.0
200 E -83.3
201 E -135.4
202 I -125.1
203 H -101.7
204 F -109.0
205 F -120.5
206 G -161.9
207 D -111.3
208 K -104.0
209 T -100.4
210 Q -105.7
211 E -59.7
212 G 85.7
213 G -86.4
214 N -72.9
215 D -104.1
216 Y -66.3
217 E -61.2
218 I -71.1
219 Y -61.1
220 T -94.4
221 D -75.1
222 K -59.9
223 R -78.5
224 T -122.9
225 I -92.5
226 G -86.7
227 H -131.5
228 K -77.4
229 V -120.2
230 T -115.6
231 S -156.7
232 Y -63.0
233 K -70.1
234 D -59.2
235 T -53.0
236 I -61.6
237 A -63.9
238 E -73.6
239 V -64.8
240 E -55.5
241 K -59.1
242 I -66.5
243 I -60.0
244 A -57.8
245 M -87.0
246 K 163.5
我不确定您 data
的格式。如果看起来像
[[1, 'G', 360.0],
[2, 'M', -67.2],
[3, 'K', -78.0],
...
[244, 'A', -57.8],
[245, 'M', -87.0],
[246, 'K', 163.5]]
您可以执行以下操作:
pos_codes = set()
neg_codes = set()
for row in data:
if row[2] > 0:
pos_codes.add(row[1])
else:
neg_codes.add(row[1])
neg_and_pos_codes = pos_codes.intersection(neg_codes)
print('Double codes:', neg_and_pos_codes)
print('Number of double codes:', len(neg_and_pos_codes))
结果:
Double codes: {'N', 'R', 'G', 'K', 'E', 'T'}
Number of double codes: 6
或者没有显式循环的更短一点:
neg_and_pos_codes = ({row[1] for row in data if row[2] > 0}
.intersection({row[1] for row in data if row[2] < 0}))
如果 data
是按列组织的
[[1, 2, 3, ..., 244, 245, 246],
['G', 'M', 'K', ...,'A', 'M', 'K'],
[360.0, -67.2, -78.0, ..., -57.8, -87.0, 163.5]]
然后
pos_codes = set()
neg_codes = set()
for code, number in zip(data[1], data[2]):
if number > 0:
pos_codes.add(code)
else:
neg_codes.add(code)
neg_and_pos_codes = pos_codes.intersection(neg_codes)
或
neg_and_pos_codes = ({code
for code, number in zip(data[1], data[2])
if number > 0}
.intersection({code
for code, number in zip(data[1], data[2])
if number < 0}))
会产生相同的结果。
我有一个包含 3 列的数据集。一列有特定的字母代码(可能重复),另一列有相应的值(可以是正数或负数)。 如何同时计算大于 0 和/或小于 0 的字母,而不重复这些字母代码。 我放置了我的数据集及其外观,因为我是编程新手,也许有人可以帮助我。
1 G 360.0
2 M -67.2
3 K -78.0
4 R 57.7
5 V -129.4
6 L -98.5
7 L -109.6
8 L -111.2
9 F -121.3
10 D -58.2
11 V -96.1
12 D -61.7
13 G 81.5
14 T -107.7
15 L -104.1
16 T -135.2
17 P -63.7
18 P -56.9
19 R 48.2
20 L -137.9
21 C -55.9
22 Q -58.8
23 T -88.4
24 D -56.9
25 E -68.8
26 M -68.1
27 R -59.4
28 A -65.4
29 L -56.1
30 I -52.3
31 K -62.6
32 R -59.7
33 A -65.9
34 R -65.6
35 G -63.5
36 A -83.5
37 G 98.0
38 F -87.4
39 C -86.2
40 V -129.2
41 G -148.2
42 T -108.0
43 V -142.0
44 G -147.4
45 G -78.2
46 S -85.9
47 D -80.1
48 F -57.9
49 A -52.7
50 K -66.2
51 Q -67.9
52 V -56.0
53 E -66.7
54 Q -89.2
55 L -114.1
56 G 127.3
57 R -53.8
58 D -97.3
59 V -56.5
60 L -67.2
61 T -83.0
62 Q -76.5
63 F -121.9
64 D -70.3
65 Y -122.5
66 V -129.3
67 F -111.5
68 A -97.9
69 E 48.1
70 N 59.2
71 G 94.5
72 L -67.1
73 L -124.6
74 A -131.0
75 Y -124.9
76 R -124.0
77 N 41.4
78 G 90.0
79 L -98.8
80 E -74.8
81 I -108.0
82 H -161.7
83 R -151.8
84 Q -86.3
85 S -127.4
86 L -65.1
87 L -53.8
88 N -63.8
89 A -93.8
90 L -121.2
91 G 94.3
92 N -63.7
93 D -50.7
94 R -65.5
95 I -60.7
96 V -65.8
97 K -53.3
98 F -63.4
99 V -68.9
100 K -60.3
101 K -62.8
102 T -58.6
103 L -58.8
104 R -64.2
105 L -60.2
106 I -65.0
107 A -57.0
108 D -84.0
109 L -73.1
110 D -125.5
111 I -116.1
112 P -67.6
113 V -134.4
114 Q -140.6
115 R -132.5
116 G -169.7
117 T 59.6
118 F -85.7
119 V -125.8
120 E -109.0
121 Y -94.4
122 R -100.6
123 N -85.9
124 G -114.2
125 M -149.6
126 I -119.7
127 N -111.6
128 V -121.1
129 S -124.8
130 P -57.6
131 I -82.7
132 G 96.0
133 R -60.6
134 N -79.8
135 C -74.3
136 S -75.5
137 Q -59.3
138 A -63.4
139 E -59.6
140 R -64.6
141 D -57.7
142 E -71.2
143 F -56.8
144 E -54.7
145 V -62.4
146 Y -53.6
147 D -66.6
148 N -54.7
149 E -74.5
150 H -99.5
151 R 45.5
152 V -73.3
153 R -61.4
154 A -67.5
155 S -55.5
156 L -62.4
157 I -58.4
158 A -57.4
159 E -64.7
160 L -63.7
161 E -57.5
162 N -67.1
163 S -72.3
164 F -131.2
165 P -60.2
166 D -75.0
167 F -66.6
168 G 63.2
169 L -81.0
170 K -100.9
171 Y -124.2
172 S -133.9
173 I -65.8
174 G -164.2
175 G 60.4
176 Q -84.4
177 I -137.4
178 S -148.6
179 F -143.5
180 D -90.7
181 V -132.0
182 F -161.0
183 P -59.4
184 V -51.1
185 G 91.8
186 W -101.3
187 D -74.8
188 K -59.5
189 T -62.5
190 Y -53.7
191 C -72.7
192 L -70.3
193 Q -54.4
194 F -90.1
195 V -123.2
196 E -56.3
197 D -86.6
198 D -117.1
199 F -132.3
200 E -82.0
201 E -139.3
202 I -127.8
203 H -98.3
204 F -109.4
205 F -125.3
206 G -166.1
207 D -111.6
208 K -106.3
209 T -95.0
210 Q -111.3
211 E -44.9
212 G 85.2
213 G -94.1
214 N -69.6
215 D -107.0
216 Y -55.3
217 E -60.1
218 I -71.7
219 Y -54.6
220 T -88.2
221 D -72.2
222 K -60.9
223 R -87.0
224 T -121.2
225 I -92.0
226 G -90.9
227 H -131.4
228 K -78.3
229 V -117.0
230 T -113.3
231 S -168.2
232 Y -68.5
233 K -66.5
234 D -58.4
235 T -55.9
236 I -49.6
237 A -59.3
238 E -75.3
239 V -67.9
240 E -58.2
241 K -72.3
242 I -61.4
243 I -58.4
244 A -57.1
245 M -91.2
246 K -5.7
1 G 360.0
2 M -62.2
3 K -77.2
4 R 58.6
5 V -136.3
6 L -101.2
7 L -110.2
8 L -114.7
9 F -121.9
10 D -64.0
11 V -100.6
12 D -62.8
13 G 82.6
14 T -109.8
15 L -98.0
16 T -134.7
17 P -58.1
18 P -59.7
19 R 67.0
20 L -118.3
21 C -60.1
22 Q -63.3
23 T -87.1
24 D -52.2
25 E -54.0
26 M -64.9
27 R -61.2
28 A -65.2
29 L -57.7
30 I -57.9
31 K -60.1
32 R -61.0
33 A -60.2
34 R -69.2
35 G -58.0
36 A -76.5
37 G 90.8
38 F -90.4
39 C -84.8
40 V -124.4
41 G -150.4
42 T -104.7
43 V -141.9
44 G -148.2
45 G -76.1
46 S -71.4
47 D -81.9
48 F -63.6
49 A -59.7
50 K -64.4
51 Q -66.0
52 V -56.6
53 E -65.2
54 Q -85.8
55 L -116.2
56 G 123.8
57 R -58.9
58 D -96.8
59 V -51.8
60 L -60.8
61 T -85.4
62 Q -71.1
63 F -124.2
64 D -62.5
65 Y -124.4
66 V -126.5
67 F -110.7
68 A -102.0
69 E 47.2
70 N 69.9
71 G 100.0
72 L -63.2
73 L -119.7
74 A -129.8
75 Y -124.6
76 R -127.0
77 N 40.9
78 G 91.6
79 L -99.8
80 E -71.1
81 I -103.0
82 H -161.3
83 R -146.6
84 Q -93.5
85 S -130.0
86 L -57.6
87 L -59.3
88 N -58.0
89 A -85.8
90 L -108.3
91 G 78.8
92 N -54.9
93 D -60.1
94 R -67.0
95 I -57.2
96 V -62.5
97 K -60.2
98 F -64.6
99 V -66.4
100 K -62.4
101 K -63.9
102 T -63.1
103 L -55.9
104 R -60.3
105 L -62.4
106 I -60.3
107 A -56.5
108 D -88.5
109 L -77.2
110 D -118.0
111 I -115.8
112 P -53.9
113 V -132.3
114 Q -143.3
115 R -127.0
116 G -173.2
117 T 61.9
118 F -89.4
119 V -128.1
120 E -107.0
121 Y -86.0
122 R -93.1
123 N -91.2
124 G -118.9
125 M -150.7
126 I -116.4
127 N -109.3
128 V -122.9
129 S -124.5
130 P -58.5
131 I -81.9
132 G 97.2
133 R -64.4
134 N -76.6
135 C -80.7
136 S -74.9
137 Q -58.9
138 A -62.5
139 E -64.9
140 R -64.5
141 D -63.3
142 E -51.9
143 F -59.1
144 E -56.6
145 V -57.4
146 Y -44.9
147 D -66.3
148 N -64.1
149 E -92.5
150 H -113.4
151 R 49.5
152 V -66.4
153 R -62.0
154 A -64.2
155 S -60.3
156 L -57.9
157 I -57.6
158 A -57.2
159 E -63.3
160 L -62.7
161 E -60.0
162 N -72.5
163 S -72.5
164 F -131.4
165 P -64.1
166 D -72.8
167 F -72.6
168 G 59.4
169 L -95.6
170 K -100.7
171 Y -124.9
172 S -134.2
173 I -64.3
174 G -156.5
175 G 60.6
176 Q -88.1
177 I -133.7
178 S -148.1
179 F -139.0
180 D -93.5
181 V -129.4
182 F -160.8
183 P -62.6
184 V -53.2
185 G 88.8
186 W -100.5
187 D -68.8
188 K -56.4
189 T -59.7
190 Y -54.6
191 C -69.9
192 L -65.6
193 Q -58.0
194 F -88.4
195 V -121.1
196 E -55.7
197 D -85.0
198 D -120.1
199 F -134.0
200 E -83.3
201 E -135.4
202 I -125.1
203 H -101.7
204 F -109.0
205 F -120.5
206 G -161.9
207 D -111.3
208 K -104.0
209 T -100.4
210 Q -105.7
211 E -59.7
212 G 85.7
213 G -86.4
214 N -72.9
215 D -104.1
216 Y -66.3
217 E -61.2
218 I -71.1
219 Y -61.1
220 T -94.4
221 D -75.1
222 K -59.9
223 R -78.5
224 T -122.9
225 I -92.5
226 G -86.7
227 H -131.5
228 K -77.4
229 V -120.2
230 T -115.6
231 S -156.7
232 Y -63.0
233 K -70.1
234 D -59.2
235 T -53.0
236 I -61.6
237 A -63.9
238 E -73.6
239 V -64.8
240 E -55.5
241 K -59.1
242 I -66.5
243 I -60.0
244 A -57.8
245 M -87.0
246 K 163.5
我不确定您 data
的格式。如果看起来像
[[1, 'G', 360.0],
[2, 'M', -67.2],
[3, 'K', -78.0],
...
[244, 'A', -57.8],
[245, 'M', -87.0],
[246, 'K', 163.5]]
您可以执行以下操作:
pos_codes = set()
neg_codes = set()
for row in data:
if row[2] > 0:
pos_codes.add(row[1])
else:
neg_codes.add(row[1])
neg_and_pos_codes = pos_codes.intersection(neg_codes)
print('Double codes:', neg_and_pos_codes)
print('Number of double codes:', len(neg_and_pos_codes))
结果:
Double codes: {'N', 'R', 'G', 'K', 'E', 'T'}
Number of double codes: 6
或者没有显式循环的更短一点:
neg_and_pos_codes = ({row[1] for row in data if row[2] > 0}
.intersection({row[1] for row in data if row[2] < 0}))
如果 data
是按列组织的
[[1, 2, 3, ..., 244, 245, 246],
['G', 'M', 'K', ...,'A', 'M', 'K'],
[360.0, -67.2, -78.0, ..., -57.8, -87.0, 163.5]]
然后
pos_codes = set()
neg_codes = set()
for code, number in zip(data[1], data[2]):
if number > 0:
pos_codes.add(code)
else:
neg_codes.add(code)
neg_and_pos_codes = pos_codes.intersection(neg_codes)
或
neg_and_pos_codes = ({code
for code, number in zip(data[1], data[2])
if number > 0}
.intersection({code
for code, number in zip(data[1], data[2])
if number < 0}))
会产生相同的结果。